Add structured logging for Rails processes
This introduces JSON logging for Rails views saved to a file called
`development_json.log`, `production_json.log`, etc.
For example, instead of this unparsable log:
```
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)
```
We get a single line with this:
```
{"method":"GET","path":"/,"format":"html","controller":"HomeController","action":"index","status":200,"duration":79,"view":78.8,"db":0.0,"location":"http://localhost/","time":"2017-07-18 09:35:17 -0700"}
```
Part of #20060
2017-07-17 18:54:13 -04:00
|
|
|
# Only use Lograge for Rails
|
|
|
|
unless Sidekiq.server?
|
|
|
|
filename = File.join(Rails.root, 'log', "#{Rails.env}_json.log")
|
|
|
|
|
|
|
|
Rails.application.configure do
|
|
|
|
config.lograge.enabled = true
|
|
|
|
# Store the lograge JSON files in a separate file
|
|
|
|
config.lograge.keep_original_rails_log = true
|
|
|
|
# Don't use the Logstash formatter since this requires logstash-event, an
|
|
|
|
# unmaintained gem that monkey patches `Time`
|
|
|
|
config.lograge.formatter = Lograge::Formatters::Json.new
|
|
|
|
config.lograge.logger = ActiveSupport::Logger.new(filename)
|
|
|
|
# Add request parameters to log output
|
|
|
|
config.lograge.custom_options = lambda do |event|
|
|
|
|
{
|
2017-07-20 10:58:01 -04:00
|
|
|
time: event.time.utc.iso8601(3),
|
2017-07-28 01:48:03 -04:00
|
|
|
params: event.payload[:params].except(*%w(controller action format)),
|
|
|
|
remote_ip: event.payload[:remote_ip],
|
|
|
|
user_id: event.payload[:user_id],
|
|
|
|
username: event.payload[:username]
|
Add structured logging for Rails processes
This introduces JSON logging for Rails views saved to a file called
`development_json.log`, `production_json.log`, etc.
For example, instead of this unparsable log:
```
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)
```
We get a single line with this:
```
{"method":"GET","path":"/,"format":"html","controller":"HomeController","action":"index","status":200,"duration":79,"view":78.8,"db":0.0,"location":"http://localhost/","time":"2017-07-18 09:35:17 -0700"}
```
Part of #20060
2017-07-17 18:54:13 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|