From d883bb92637c97c0ee07b11dcb8785358e751436 Mon Sep 17 00:00:00 2001 From: "romain.sagean" Date: Tue, 12 Apr 2016 10:45:37 +0200 Subject: [PATCH 1/3] add client ip and ua loggers --- lib/grape_logging.rb | 1 + lib/grape_logging/loggers/client_env.rb | 9 +++++++++ 2 files changed, 10 insertions(+) create mode 100644 lib/grape_logging/loggers/client_env.rb diff --git a/lib/grape_logging.rb b/lib/grape_logging.rb index 0fe9c3e..8c89482 100644 --- a/lib/grape_logging.rb +++ b/lib/grape_logging.rb @@ -5,6 +5,7 @@ require 'grape_logging/formatters/json' require 'grape_logging/loggers/base' require 'grape_logging/loggers/response' require 'grape_logging/loggers/filter_parameters' +require 'grape_logging/loggers/client_env' require 'grape_logging/reporters/active_support_reporter' require 'grape_logging/reporters/logger_reporter' require 'grape_logging/timings' diff --git a/lib/grape_logging/loggers/client_env.rb b/lib/grape_logging/loggers/client_env.rb new file mode 100644 index 0000000..b8cd027 --- /dev/null +++ b/lib/grape_logging/loggers/client_env.rb @@ -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 From 2717b4a3ec8fffff790d64e4fc17cd43362062ff Mon Sep 17 00:00:00 2001 From: "romain.sagean" Date: Tue, 12 Apr 2016 11:21:13 +0200 Subject: [PATCH 2/3] add client env usage --- README.md | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 947abea..8a438d3 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Or install it yourself as: ## Basic Usage In your api file (somewhere on the top) - + require 'grape_logging' logger.formatter = GrapeLogging::Formatters::Default.new 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, logger: logger, include: [ GrapeLogging::Loggers::Response.new, - GrapeLogging::Loggers::FilterParameters.new ] + GrapeLogging::Loggers::FilterParameters.new, + GrapeLogging::Loggers::ClientEnv.new ] 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. +#### ClientEnv +The `FilterParameters` logger will add `ip` and user agent `ua` in your log. + ### Logging to file and STDOUT 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.sync = true logger Logger.new GrapeLogging::MultiIO.new(STDOUT, log_file) - + ### 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. @@ -74,16 +79,16 @@ First, config ```grape_logging```, like that: include: [ GrapeLogging::Loggers::Response.new, GrapeLogging::Loggers::FilterParameters.new ] end - + and then add an initializer in your Rails project: - + # config/initializers/instrumentation.rb - + # Subscribe to grape request and log with Rails.logger ActiveSupport::Notifications.subscribe('grape_key') do |name, starts, ends, notification_id, payload| Rails.logger.info payload end - + The idea come from here: https://gist.github.com/teamon/e8ae16ffb0cb447e5b49 ### Logging exceptions From 7d3461184ecb8e4f0de7b43666e10f367d806da7 Mon Sep 17 00:00:00 2001 From: scauglog Date: Tue, 12 Apr 2016 16:50:35 +0200 Subject: [PATCH 3/3] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8a438d3..3f77159 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ You can include logging of other parts of the request / response cycle by includ 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 `FilterParameters` logger will add `ip` and user agent `ua` in your log. +The `ClientEnv` logger will add `ip` and user agent `ua` in your log. ### Logging to file and STDOUT