gpt4 book ai didi

ruby-on-rails - 禁用 IP 地址记录 Rails

转载 作者:数据小太阳 更新时间:2023-10-29 08:47:41 25 4
gpt4 key购买 nike

我正在尝试在处理请求时禁用 IP 地址的记录。但我找不到办法做到这一点。我想为用户尚未通过身份验证的应用程序的有限部分禁用 IP 记录。

所以我的问题是

How to disable logging of IP in rails log for specific pages(so the IP will not be saved in any log)

我正在使用 Rails 3.2.17

编辑:这是示例日志(来​​自 environment.log)

Started GET "/my_path" for 192.168.0.109 at 2014-03-28 11:53:20 +0530

我不想在日志文件中保存 192.168.0.109

最佳答案

在 config/initializers 中,添加文件 log_fomat.rb :

class ActiveSupport::BufferedLogger
def formatter=(formatter)
@log.formatter = formatter
end
end

class Formatter
SEVERITY_TO_TAG_MAP = {'DEBUG'=>'meh', 'INFO'=>'fyi', 'WARN'=>'hmm', 'ERROR'=>'wtf', 'FATAL'=>'omg', 'UNKNOWN'=>'???'}
SEVERITY_TO_COLOR_MAP = {'DEBUG'=>'0;37', 'INFO'=>'32', 'WARN'=>'33', 'ERROR'=>'31', 'FATAL'=>'31', 'UNKNOWN'=>'37'}
USE_HUMOROUS_SEVERITIES = true

def call(severity, time, progname, msg)
if USE_HUMOROUS_SEVERITIES
formatted_severity = sprintf("%-3s","#{SEVERITY_TO_TAG_MAP[severity]}")
else
formatted_severity = sprintf("%-5s","#{severity}")
end

formatted_time = time.strftime("%Y-%m-%d %H:%M:%S.") << time.usec.to_s[0..2].rjust(3)
color = SEVERITY_TO_COLOR_MAP[severity]

"\033[0;37m#{formatted_time}\033[0m [\033[#{color}m#{formatted_severity}\033[0m] #{msg.strip} (pid:#{$$})\n"
end

end

Rails.logger.formatter = Formatter.new

引用资料:

  1. http://rubyjunky.com/cleaning-up-rails-4-production-logging.html
  2. http://cbpowell.wordpress.com/2012/04/05/beautiful-logging-for-ruby-on-rails-3-2/
  3. Rails logger format string configuration

关于ruby-on-rails - 禁用 IP 地址记录 Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22705823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com