Skip to content

Commit

Permalink
Add level option to logger initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
davydovanton committed Feb 25, 2016
1 parent b0fd910 commit cf1a957
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/hanami/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ def call(severity, time, progname, msg)
# @api private
DEFAULT_APPLICATION_NAME = 'Hanami'.freeze

# @since x.x.x
# @api private
LEVEL_HASH = {
warn: Hanami::Logger::WARN,
info: Hanami::Logger::INFO,
fatal: Hanami::Logger::FATAL,
error: Hanami::Logger::ERROR,
debug: Hanami::Logger::DEBUG,
unknown: Hanami::Logger::UNKNOWN
}.freeze

# @since 0.5.0
# @api private
attr_writer :application_name
Expand All @@ -116,9 +127,10 @@ def call(severity, time, progname, msg)
# (String) or IO object (typically STDOUT, STDERR, or an open file).
#
# @since 0.5.0
def initialize(application_name = nil, device: STDOUT)
def initialize(application_name = nil, device: STDOUT, level: DEBUG)
super(device)

@level = convert_level(level)
@device = device
@application_name = application_name
@formatter = Hanami::Logger::Formatter.new.tap { |f| f.application_name = self.application_name }
Expand Down Expand Up @@ -155,5 +167,10 @@ def _application_name_from_namespace
def _default_application_name
DEFAULT_APPLICATION_NAME
end

def convert_level(level)
level = level.to_sym.downcase if level.is_a?(String)
(DEBUG..UNKNOWN).include?(level) ? level : LEVEL_HASH.fetch(level, DEBUG)
end
end
end
42 changes: 42 additions & 0 deletions test/logger_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,48 @@ class TestLogger < Hanami::Logger; end
output.must_match(/foo/)
end

describe 'custom level option' do
it 'takes a integer' do
logger = Hanami::Logger.new(level: 3)
logger.level.must_equal Hanami::Logger::ERROR
end

it 'takes a integer more than 5' do
logger = Hanami::Logger.new(level: 99)
logger.level.must_equal Hanami::Logger::DEBUG
end

it 'takes a symbol' do
logger = Hanami::Logger.new(level: :error)
logger.level.must_equal Hanami::Logger::ERROR
end

it 'takes a string' do
logger = Hanami::Logger.new(level: 'error')
logger.level.must_equal Hanami::Logger::ERROR
end

it 'takes a string with strange value' do
logger = Hanami::Logger.new(level: 'strange')
logger.level.must_equal Hanami::Logger::DEBUG
end

it 'takes a uppercased string' do
logger = Hanami::Logger.new(level: 'ERROR')
logger.level.must_equal Hanami::Logger::ERROR
end

it 'takes a constant' do
logger = Hanami::Logger.new(level: Hanami::Logger::ERROR)
logger.level.must_equal Hanami::Logger::ERROR
end

it 'contains debug level by default' do
logger = Hanami::Logger.new
logger.level.must_equal ::Logger::DEBUG
end
end

describe 'custom device' do
describe 'file system' do
before do
Expand Down

0 comments on commit cf1a957

Please sign in to comment.