mirror of
https://github.com/samsonjs/grape_logging.git
synced 2026-04-27 14:57:40 +00:00
Add documentation
This commit is contained in:
parent
da5d32ca68
commit
2a81b65de6
1 changed files with 32 additions and 0 deletions
32
README.md
32
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 = 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
|
||||||
|
|
||||||
|
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
|
### Logging exceptions
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue