Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add log_level option #39

Merged
merged 1 commit into from
May 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add log_level option
  • Loading branch information
Joel Carlbark committed May 11, 2017
commit 60aa0062f4387aaafee6ccf8ab4c12b33ad6b593
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ log_file.sync = true
logger Logger.new GrapeLogging::MultiIO.new(STDOUT, log_file)
```

### Set the log level

You can control the level used to log. The default is `info`.

```ruby
class MyAPI < Grape::API
use GrapeLogging::Middleware::RequestLogger,
logger: logger,
log_level: 'debug'
end
```

### 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.
Expand Down
2 changes: 1 addition & 1 deletion lib/grape_logging/middleware/request_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def initialize(app, options = {})
@reporter = if options[:instrumentation_key]
Reporters::ActiveSupportReporter.new(@options[:instrumentation_key])
else
Reporters::LoggerReporter.new(@options[:logger], @options[:formatter])
Reporters::LoggerReporter.new(@options[:logger], @options[:formatter], @options[:log_level])
end
end

Expand Down
7 changes: 4 additions & 3 deletions lib/grape_logging/reporters/logger_reporter.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
module Reporters
class LoggerReporter
def initialize(logger, formatter)
def initialize(logger, formatter, log_level)
@logger = logger || Logger.new(STDOUT)
@log_level = log_level || :info
if @logger.respond_to?(:formatter=)
@logger.formatter = formatter || @logger.formatter || GrapeLogging::Formatters::Default.new
end
end

def perform(params)
@logger.info params
@logger.send(@log_level, params)
end
end
end
end
10 changes: 10 additions & 0 deletions spec/lib/grape_logging/middleware/request_logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@
end
end

%w[info error debug].each do |level|
context "with level #{level}" do
it 'should log at correct level' do
options[:log_level] = level
expect(logger).to receive(level)
subject
end
end
end

context 'with a nil response' do
let(:app) { proc{ [500, {} , nil] } }
it 'should log "fail" instead of a status' do
Expand Down