mirror of
https://github.com/samsonjs/grape_logging.git
synced 2026-03-25 08:55:47 +00:00
Merge pull request #26 from scauglog/master
add logger for client ip and ua
This commit is contained in:
commit
14f16c21c0
3 changed files with 23 additions and 8 deletions
21
README.md
21
README.md
|
|
@ -19,7 +19,7 @@ Or install it yourself as:
|
||||||
## Basic Usage
|
## Basic Usage
|
||||||
|
|
||||||
In your api file (somewhere on the top)
|
In your api file (somewhere on the top)
|
||||||
|
|
||||||
require 'grape_logging'
|
require 'grape_logging'
|
||||||
logger.formatter = GrapeLogging::Formatters::Default.new
|
logger.formatter = GrapeLogging::Formatters::Default.new
|
||||||
use GrapeLogging::Middleware::RequestLogger, { logger: logger }
|
use GrapeLogging::Middleware::RequestLogger, { logger: logger }
|
||||||
|
|
@ -50,19 +50,24 @@ You can include logging of other parts of the request / response cycle by includ
|
||||||
use GrapeLogging::Middleware::RequestLogger,
|
use GrapeLogging::Middleware::RequestLogger,
|
||||||
logger: logger,
|
logger: logger,
|
||||||
include: [ GrapeLogging::Loggers::Response.new,
|
include: [ GrapeLogging::Loggers::Response.new,
|
||||||
GrapeLogging::Loggers::FilterParameters.new ]
|
GrapeLogging::Loggers::FilterParameters.new,
|
||||||
|
GrapeLogging::Loggers::ClientEnv.new ]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
#### FilterParameters
|
||||||
The `FilterParameters` logger will filter out sensitive parameters from your logs. If mounted inside rails, will use the `Rails.application.config.filter_parameters` by default. Otherwise, you must specify a list of keys to filter out.
|
The `FilterParameters` logger will filter out sensitive parameters from your logs. If mounted inside rails, will use the `Rails.application.config.filter_parameters` by default. Otherwise, you must specify a list of keys to filter out.
|
||||||
|
|
||||||
|
#### ClientEnv
|
||||||
|
The `ClientEnv` logger will add `ip` and user agent `ua` in your log.
|
||||||
|
|
||||||
### Logging to file and STDOUT
|
### Logging to file and STDOUT
|
||||||
|
|
||||||
You can log to file and STDOUT at the same time, you just need to assign new logger
|
You can log to file and STDOUT at the same time, you just need to assign new logger
|
||||||
|
|
||||||
log_file = File.open('path/to/your/logfile.log', 'a')
|
log_file = File.open('path/to/your/logfile.log', 'a')
|
||||||
log_file.sync = true
|
log_file.sync = true
|
||||||
logger Logger.new GrapeLogging::MultiIO.new(STDOUT, log_file)
|
logger Logger.new GrapeLogging::MultiIO.new(STDOUT, log_file)
|
||||||
|
|
||||||
### Logging via Rails instrumentation
|
### Logging via Rails instrumentation
|
||||||
|
|
||||||
You can choose to not pass the logger to ```grape_logging``` but instead send logs to Rails instrumentation in order to let Rails and its configured Logger do the log job, for example.
|
You can choose to not pass the logger to ```grape_logging``` but instead send logs to Rails instrumentation in order to let Rails and its configured Logger do the log job, for example.
|
||||||
|
|
@ -74,16 +79,16 @@ First, config ```grape_logging```, like that:
|
||||||
include: [ GrapeLogging::Loggers::Response.new,
|
include: [ GrapeLogging::Loggers::Response.new,
|
||||||
GrapeLogging::Loggers::FilterParameters.new ]
|
GrapeLogging::Loggers::FilterParameters.new ]
|
||||||
end
|
end
|
||||||
|
|
||||||
and then add an initializer in your Rails project:
|
and then add an initializer in your Rails project:
|
||||||
|
|
||||||
# config/initializers/instrumentation.rb
|
# config/initializers/instrumentation.rb
|
||||||
|
|
||||||
# Subscribe to grape request and log with Rails.logger
|
# Subscribe to grape request and log with Rails.logger
|
||||||
ActiveSupport::Notifications.subscribe('grape_key') do |name, starts, ends, notification_id, payload|
|
ActiveSupport::Notifications.subscribe('grape_key') do |name, starts, ends, notification_id, payload|
|
||||||
Rails.logger.info payload
|
Rails.logger.info payload
|
||||||
end
|
end
|
||||||
|
|
||||||
The idea come from here: https://gist.github.com/teamon/e8ae16ffb0cb447e5b49
|
The idea come from here: https://gist.github.com/teamon/e8ae16ffb0cb447e5b49
|
||||||
|
|
||||||
### Logging exceptions
|
### Logging exceptions
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ require 'grape_logging/formatters/json'
|
||||||
require 'grape_logging/loggers/base'
|
require 'grape_logging/loggers/base'
|
||||||
require 'grape_logging/loggers/response'
|
require 'grape_logging/loggers/response'
|
||||||
require 'grape_logging/loggers/filter_parameters'
|
require 'grape_logging/loggers/filter_parameters'
|
||||||
|
require 'grape_logging/loggers/client_env'
|
||||||
require 'grape_logging/reporters/active_support_reporter'
|
require 'grape_logging/reporters/active_support_reporter'
|
||||||
require 'grape_logging/reporters/logger_reporter'
|
require 'grape_logging/reporters/logger_reporter'
|
||||||
require 'grape_logging/timings'
|
require 'grape_logging/timings'
|
||||||
|
|
|
||||||
9
lib/grape_logging/loggers/client_env.rb
Normal file
9
lib/grape_logging/loggers/client_env.rb
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
module GrapeLogging
|
||||||
|
module Loggers
|
||||||
|
class ClientEnv < GrapeLogging::Loggers::Base
|
||||||
|
def parameters(request, _)
|
||||||
|
{ ip: request.env["HTTP_X_FORWARDED_FOR"] || request.env["REMOTE_ADDR"], ua: request.env["HTTP_USER_AGENT"] }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in a new issue