Skip to content

Commit

Permalink
Let Utils::Logger::Formatter to fabricate an instance of self
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Jun 23, 2016
1 parent 9c150d6 commit b2761f5
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
35 changes: 33 additions & 2 deletions lib/hanami/logger.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
require 'set'
require 'json'
require 'logger'
require 'hanami/utils/string'
require 'hanami/utils/json'
require 'hanami/utils/class_attribute'

module Hanami
# Hanami logger
Expand Down Expand Up @@ -107,6 +109,31 @@ class Formatter < ::Logger::Formatter
# @api private
NEW_LINE = $/

include Utils::ClassAttribute

class_attribute :subclasses
self.subclasses = Set.new

def self.fabricate(formatter, application_name)
case formatter
when Symbol
(subclasses.find { |s| s.eligible?(formatter) } || self).new
when nil
new
else
formatter
end.tap { |f| f.application_name = application_name }
end

def self.inherited(subclass)
super
subclasses << subclass
end

def self.eligible?(name)
name == :default
end

# @since 0.5.0
# @api private
attr_writer :application_name
Expand Down Expand Up @@ -156,6 +183,10 @@ def _format(hash)
# @since 0.5.0
# @api private
class JSONFormatter < Formatter
def self.eligible?(name)
name == :json
end

private

# @since x.x.x
Expand Down Expand Up @@ -196,13 +227,13 @@ def _format(hash)
# (String) or IO object (typically STDOUT, STDERR, or an open file).
#
# @since 0.5.0
def initialize(application_name = nil, stream: STDOUT, level: DEBUG, formatter: Formatter.new)
def initialize(application_name = nil, stream: STDOUT, level: DEBUG, formatter: nil)
super(stream)

@level = _level(level)
@stream = stream
@application_name = application_name
@formatter = formatter.tap { |f| f.application_name = self.application_name }
@formatter = Formatter.fabricate(formatter, self.application_name)
end

# Returns the current application name, this is used for tagging purposes
Expand Down
35 changes: 35 additions & 0 deletions test/logger_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,31 @@ class TestLogger < Hanami::Logger; def application_name; 'bar'; end; end
output.must_match(/bar/)
end

describe 'with nil formatter' do
it 'falls back to Formatter' do
stub_time_now do
output =
stub_stdout_constant do
class TestLogger < Hanami::Logger;end
TestLogger.new(formatter: nil).info('foo')
end
output.must_equal "app=Hanami severity=INFO time=1988-09-01 00:00:00 UTC message=foo\n"
end
end
end

describe 'with JSON formatter' do
it 'when passed as a symbol, it has JSON format for string messages' do
stub_time_now do
output =
stub_stdout_constant do
class TestLogger < Hanami::Logger;end
TestLogger.new(formatter: :json).info('foo')
end
output.must_equal "{\"app\":\"Hanami\",\"severity\":\"INFO\",\"time\":\"1988-09-01 00:00:00 UTC\",\"message\":\"foo\"}"
end
end

it 'has JSON format for string messages' do
stub_time_now do
output =
Expand Down Expand Up @@ -345,6 +369,17 @@ class TestLogger < Hanami::Logger;end
end

describe 'with default formatter' do
it 'when passed as a symbol, it has key=value format for string messages' do
stub_time_now do
output =
stub_stdout_constant do
class TestLogger < Hanami::Logger;end
TestLogger.new(formatter: :default).info('foo')
end
output.must_equal "app=Hanami severity=INFO time=1988-09-01 00:00:00 UTC message=foo\n"
end
end

it 'has key=value format for string messages' do
stub_time_now do
output =
Expand Down

0 comments on commit b2761f5

Please sign in to comment.