Skip to content

Commit

Permalink
Use environment variable for log level (#769)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkadel authored Mar 14, 2022
1 parent ffb7628 commit 42d336c
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/services/ensure_environment_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def self.shared
HYRAX_DATABASE_PASSWORD
HYRAX_HOST
IMAGE_PROCESSOR
LOG_LEVEL
LONGLEAF_BASE_COMMAND
LONGLEAF_STORAGE_PATH
SECRET_KEY_BASE
Expand Down
18 changes: 18 additions & 0 deletions app/services/log_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module LogService
def self.log_level
log_level = ENV['LOG_LEVEL']&.to_sym
if log_level && valid_log_level?(log_level)
log_level
else
:warn
end
end

def self.valid_log_level?(log_level)
valid_log_levels.include?(log_level)
end

def self.valid_log_levels
[:debug, :info, :warn, :error, :fatal, :unknown]
end
end
2 changes: 1 addition & 1 deletion config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,5 @@
# config.web_console.permissions = ['10.0.2.2']
config.web_console.whitelisted_ips = ['10.0.2.2']

config.log_level = :debug
config.log_level = LogService.log_level
end
2 changes: 1 addition & 1 deletion config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

# Use the lowest log level to ensure availability of diagnostic information
# when problems arise.
config.log_level = :warn
config.log_level = LogService.log_level

# Use a different cache store in production.
# config.cache_store = :mem_cache_store
Expand Down
2 changes: 1 addition & 1 deletion config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@
# config.action_view.raise_on_missing_translations = true

config.active_job.queue_adapter = :inline
config.log_level = :debug
config.log_level = LogService.log_level
end
55 changes: 55 additions & 0 deletions spec/services/log_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require 'rails_helper'

RSpec.describe LogService do
it 'knows the valid log levels' do
expect(described_class.valid_log_levels).to eq([:debug, :info, :warn, :error, :fatal, :unknown])
end
context 'with the environment variable set to debug' do
around do |example|
cached_log_level = ENV['LOG_LEVEL']
ENV['LOG_LEVEL'] = 'debug'
example.run
ENV['LOG_LEVEL'] = cached_log_level
end

it 'sets the log level' do
expect(described_class.log_level).to eq(:debug)
end
end
context 'with the environment variable set to warn' do
around do |example|
cached_log_level = ENV['LOG_LEVEL']
ENV['LOG_LEVEL'] = 'warn'
example.run
ENV['LOG_LEVEL'] = cached_log_level
end

it 'sets the log level' do
expect(described_class.log_level).to eq(:warn)
end
end
context 'with the environment variable unset' do
around do |example|
cached_log_level = ENV['LOG_LEVEL']
ENV.delete('LOG_LEVEL')
example.run
ENV['LOG_LEVEL'] = cached_log_level
end

it 'sets the log level' do
expect(described_class.log_level).to eq(:warn)
end
end
context 'with the environment variable set to an illegal value' do
around do |example|
cached_log_level = ENV['LOG_LEVEL']
ENV['LOG_LEVEL'] = 'potato_chips'
example.run
ENV['LOG_LEVEL'] = cached_log_level
end

it 'sets the log level' do
expect(described_class.log_level).to eq(:warn)
end
end
end

0 comments on commit 42d336c

Please sign in to comment.