diff --git a/lib/gems/baw-app/lib/baw_app.rb b/lib/gems/baw-app/lib/baw_app.rb index 9b527964..9335817a 100644 --- a/lib/gems/baw-app/lib/baw_app.rb +++ b/lib/gems/baw-app/lib/baw_app.rb @@ -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 diff --git a/spec/lib/gems/baw_app/baw_app_spec.rb b/spec/lib/gems/baw_app/baw_app_spec.rb new file mode 100644 index 00000000..7d08bb2d --- /dev/null +++ b/spec/lib/gems/baw_app/baw_app_spec.rb @@ -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