我试图在处理请求时禁用IP地址的记录。但我找不到这样做的方法。我想禁用IP日志的有限部分,我的应用程序,其中用户尚未认证。
所以我的问题是
如何在rails日志中禁用特定页面的IP记录(因此IP将不会保存在任何日志中)
我使用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_format文件。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]
" 33[0;37m#{formatted_time} 33[0m [ 33[#{color}m#{formatted_severity} 33[0m] #{msg.strip} (pid:#{$$})n"
end
end
Rails.logger.formatter = Formatter.new
引用:
- http://rubyjunky.com/cleaning-up-rails-4-production-logging.html
- http://cbpowell.wordpress.com/2012/04/05/beautiful-logging-for-ruby-on-rails-3-2/
- Rails记录器格式字符串配置
最后使用emaillenin的答案thanx emaillenin: d
解决方案
# Overriding Rails logger to not save IP addresses for specific paths
# Put this file in <app_root>/config/initializers
# defining setter for Rails default log formatter, so later we can set our custom logger using '='
class ActiveSupport::BufferedLogger
def formatter=(formatter)
@log.formatter = formatter
end
end
# Modified Formatter Class with custom 'call' method
class Formatter
Format = "%sn"
# Remove IP while getting request on below specified Path
FilteredActionRegexp = /app_path|another_path/i
# reference for regexp of IP address
# http://answers.oreilly.com/topic/318-how-to-match-ipv4-addresses-with-regular-expressions/
IPRegexp = /b(?:[0-9]{1,3}.){3}[0-9]{1,3}b/
FilteredString = '**FILTERED**'
def call(severity, time, progname, msg)
Format % [msg2str(filter_ip(msg))]
end
private
def msg2str(msg)
case msg
when ::String
msg
when ::Exception
"#{ msg.message } (#{ msg.class })n" <<
(msg.backtrace || []).join("n")
else
msg.inspect
end
end
# Replace IP Address with custom string if action is filtered
def filter_ip(msg)
# Replace only if message contains filtered action
if msg =~ FilteredActionRegexp
# If log string contains IP address then remove it with custom string
msg.gsub(IPRegexp, FilteredString )
else
msg
end
end
end
# Override Rails default logger formatter
Rails.logger.formatter = Formatter.new
我使用logage
驯服Rails的默认请求日志
而不是像这样有大量不可解析的日志输出:
Started GET "/" for 127.0.0.1 at 2012-03-10 14:28:14 +0100
Processing by HomeController#index as HTML
Rendered text template within layouts/application (0.0ms)
Rendered layouts/_assets.html.erb (2.0ms)
Rendered layouts/_top.html.erb (2.6ms)
Rendered layouts/_about.html.erb (0.3ms)
Rendered layouts/_google_analytics.html.erb (0.4ms)
Completed 200 OK in 79ms (Views: 78.8ms | ActiveRecord: 0.0ms)
你得到一行所有重要的信息,像这样:
method=GET path=/jobs/833552.json format=json controller=jobs action=show status=200 duration=58.33 view=40.43 db=15.26