From 2a81b65de63403eff1e2aec9b41a0ceab6522706 Mon Sep 17 00:00:00 2001 From: jules Date: Fri, 23 Oct 2015 18:07:15 +0200 Subject: [PATCH] Add documentation --- README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/README.md b/README.md index 4cac959..9e32cda 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,38 @@ You can 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. +First, config ```grape_logging```, like that: + + class MyAPI < Grape::API + use GrapeLogging::Middleware::RequestLogger, + instrumentation_key: 'grape_key', + include: [ GrapeLogging::Loggers::Response.new, + GrapeLogging::Loggers::DatabaseTime.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') do |name, starts, ends, notification_id, payload| + Rails.logger.info payload + end + +The idea come from here: https://gist.github.com/teamon/e8ae16ffb0cb447e5b49 + +There's some advantage to use this method: + + - You could use a logger that does not implement the ```formatter=```. + Defaults Rails 3 (ActiveSupport::BufferedLogger) does not implement it. + The Logging gem (https://github.com/TwP/logging) does not implement it neither + + - If you use a logger that already format logs (as the Logging gem), the logs will be formatted by your logger ### Logging exceptions