Skip to content

Commit

Permalink
Create level option for logger config instance
Browse files Browse the repository at this point in the history
  • Loading branch information
davydovanton committed Feb 24, 2016
1 parent 66da40f commit 026ed41
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lib/hanami/config/logger.rb
Expand Up @@ -8,8 +8,20 @@ module Config
class Logger
attr_reader :application_module

# @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
}

def initialize
@device = STDOUT
@level = Hanami::Logger::DEBUG
end

# Log device.
Expand Down Expand Up @@ -59,6 +71,25 @@ def engine(value = nil)
end
end

# Logger level
#
# @overload level(value)
# Sets the given value
# @param value [Object] a level
#
# @overload level
# Gets the value
# @return [Object] returns the level
#
# @since x.x.x
def level(value = nil)
if value.nil?
@level
else
@level = detect_level(value)
end
end

# Application name value.
#
# @overload stream(value)
Expand Down Expand Up @@ -92,6 +123,12 @@ def build
@engine ||
::Hanami::Logger.new(@app_name, device: @device)
end

private

def detect_level(level)
level.is_a?(Integer) ? level : LEVEL_HASH[level.to_sym.downcase]
end
end
end
end
26 changes: 26 additions & 0 deletions test/config/logger_test.rb
Expand Up @@ -35,6 +35,32 @@
end
end

describe '#level' do
it 'takes logger level as a symbol' do
logger.level(:debug)
logger.level.must_equal Hanami::Logger::DEBUG
end

it 'takes logger level as a string' do
logger.level('debug')
logger.level.must_equal Hanami::Logger::DEBUG
end

it 'takes logger level as a uppercased string' do
logger.level('DEBUG')
logger.level.must_equal Hanami::Logger::DEBUG
end

it 'takes logger level as a constant' do
logger.level(Hanami::Logger::DEBUG)
logger.level.must_equal Hanami::Logger::DEBUG
end

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

describe '#build' do
it 'returns new Hanami::Logger instance' do
logger.build.must_be_instance_of Hanami::Logger
Expand Down

0 comments on commit 026ed41

Please sign in to comment.