Skip to content

Commit

Permalink
Correctly normalize logger levels parsed from the environment
Browse files Browse the repository at this point in the history
Previously the values would not be normalized and some loggers were less able to deal with the possible variants than others.
  • Loading branch information
atruskie committed Dec 17, 2024
1 parent 035fd18 commit d4f505f
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/gems/baw-app/lib/baw_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,23 @@ def log_to_stdout?
true
end

# Get the log level for the application.
# @param default [Integer] the default log level to use if not set
# @return [Integer] the log level
def log_level(default = Logger::DEBUG)
# The default Rails log level is warn in production env and info in any other env.
return ENV['RAILS_LOG_LEVEL'] if ENV.key?('RAILS_LOG_LEVEL')
return Logger::Severity.coerce(ENV['RAILS_LOG_LEVEL']) if ENV.key?('RAILS_LOG_LEVEL')

return Logger::INFO if Rails.env.staging?
return Logger::INFO if Rails.env.production?

default
Logger::Severity.coerce(default)
rescue ArgumentError
# rubocop:disable Rails/Output - the rails logger may not yet be initialized
puts "ERROR:\tInvalid log level: `#{ENV.fetch('RAILS_LOG_LEVEL', nil)}` or invalid default: `#{default}`"
# rubocop:enable Rails/Output

Logger::DEBUG
end

def http_scheme
Expand Down
59 changes: 59 additions & 0 deletions spec/lib/gems/baw_app/baw_app_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true

describe BawApp do
it 'coerces log level warn' do
expect(BawApp.log_level('warn')).to eq(Logger::WARN)
end

it 'coerces log level info' do
expect(BawApp.log_level('info')).to eq(Logger::INFO)
end

it 'coerces log level debug' do
expect(BawApp.log_level('debug')).to eq(Logger::DEBUG)
end

it 'coerces log level error' do
expect(BawApp.log_level('error')).to eq(Logger::ERROR)
end

it 'coerces log level fatal' do
expect(BawApp.log_level('fatal')).to eq(Logger::FATAL)
end

it 'coerces log level unknown' do
expect(BawApp.log_level('unknown')).to eq(Logger::UNKNOWN)
end

it 'coerces log level nil to default' do
expect(BawApp.log_level(nil)).to eq(Logger::DEBUG)
end

it 'coerces log level without argument to default' do
expect(BawApp.log_level).to eq(Logger::DEBUG)
end

it 'coerces log level WARN' do
expect(BawApp.log_level('WARN')).to eq(Logger::WARN)
end

it 'coerces log level INFO' do
expect(BawApp.log_level('INFO')).to eq(Logger::INFO)
end

it 'coerces log level DEBUG' do
expect(BawApp.log_level('DEBUG')).to eq(Logger::DEBUG)
end

it 'coerces log level ERROR' do
expect(BawApp.log_level('ERROR')).to eq(Logger::ERROR)
end

it 'coerces log level FATAL' do
expect(BawApp.log_level('FATAL')).to eq(Logger::FATAL)
end

it 'coerces log level UNKNOWN' do
expect(BawApp.log_level('UNKNOWN')).to eq(Logger::UNKNOWN)
end
end

0 comments on commit d4f505f

Please sign in to comment.