Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
224 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
require 'logger' | ||
require 'lotus/utils/string' | ||
|
||
module Lotus | ||
# Lotus default logger | ||
# | ||
# Implement with the same interface of ruby std ::Logger | ||
# Opts out the logdev parameter and always use STDOUT as outout device | ||
# | ||
# Lotus logger also has the app tag concept, which used to identify | ||
# which application the log come from. | ||
# | ||
# Lotus Logger default comes with Lotus application | ||
# and uses name of highest module namespace as app_tag | ||
# | ||
# When stands alone, Lotus Logger tries to infer app tag from highest namespace | ||
# When has no namespace, Lotus Logger takes [Shared] as default app tag | ||
# | ||
# @example | ||
# #1 Logger with namespace | ||
# module TestApp | ||
# class AppLogger << Lotus::Logger; end | ||
# def log | ||
# Applogger.new.info('foo') | ||
# #=> output: I, [2015-01-10T21:55:12.727259 #80487] INFO -- [TestApp] : foo | ||
# end | ||
# end | ||
# | ||
# #2 Logger without namespace | ||
# class AppLogger < Lotus::Logger | ||
# end | ||
# Applogger.new.info('foo') | ||
# #=> output: I, [2015-01-10T21:55:12.727259 #80487] INFO -- [Shared] : foo | ||
# | ||
# #3 Logger inside a lotus application | ||
# module LotusModule | ||
# class App < Lotus::Application | ||
# load! | ||
# end | ||
# end | ||
# LotusModule::Logger.info('foo') | ||
# => output: I, [2015-01-10T21:55:12.727259 #80487] INFO -- [LotusModule] : foo | ||
# | ||
# @see ::Logger | ||
# | ||
# @since 0.2.1 | ||
class Logger < ::Logger | ||
|
||
attr_accessor :app_tag | ||
|
||
# Override Ruby's Logger#initialize | ||
# | ||
# @param logdev is default to STDOUT | ||
# | ||
# @since 0.2.1 | ||
def initialize(app_tag=nil, *args) | ||
super(STDOUT, *args) | ||
@app_tag = app_tag | ||
@formatter = Lotus::Logger::Formatter.new.tap { |f| f.app_tag = self.app_tag } | ||
end | ||
|
||
# app_tag is the identification of current app | ||
# app_tag is default to use highest namespace if current namespace | ||
# if app_tag is blank, lotus use default app_tag 'shared' | ||
# @param logdev is default to STDOUT | ||
# | ||
# @since 0.2.1 | ||
def app_tag | ||
@app_tag || _app_tag_from_namespace || _default_app_tag | ||
end | ||
|
||
class Formatter < ::Logger::Formatter | ||
attr_accessor :app_tag | ||
|
||
def call(severity, time, progname, msg) | ||
time = time.utc | ||
progname = "[#{@app_tag}] #{progname}" | ||
super(severity, time, progname, msg) | ||
end | ||
end | ||
|
||
private | ||
def _app_tag_from_namespace | ||
class_name = self.class.name | ||
return nil unless class_name.index('::') | ||
|
||
Utils::String.new(class_name).namespace | ||
end | ||
|
||
def _default_app_tag | ||
'Shared' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
require 'test_helper' | ||
|
||
describe Lotus::Logger do | ||
|
||
before do | ||
#clear defined class | ||
Object.send(:remove_const, :TestLogger) if Object.constants.include?(:TestLogger) | ||
end | ||
|
||
it 'like std logger, sets log level to info by default' do | ||
class TestLogger < Lotus::Logger; end | ||
TestLogger.new.info?.must_equal true | ||
end | ||
|
||
it 'always use STDOUT' do | ||
output = | ||
stub_stdout_constant do | ||
class TestLogger < Lotus::Logger; end | ||
logger = TestLogger.new | ||
logger.info('foo') | ||
end | ||
output.must_match /foo/ | ||
end | ||
|
||
it 'has app_tag when log' do | ||
output = | ||
stub_stdout_constant do | ||
module App; class TestLogger < Lotus::Logger; end; end | ||
logger = App::TestLogger.new | ||
logger.info('foo') | ||
end | ||
output.must_match /App/ | ||
end | ||
|
||
it 'has default app tag when not in any namespace' do | ||
class TestLogger < Lotus::Logger; end | ||
TestLogger.new.send(:app_tag).must_equal 'Shared' | ||
end | ||
|
||
it 'infers apptag from namespace' do | ||
module App2 | ||
class TestLogger < Lotus::Logger;end | ||
class Bar | ||
def hoge | ||
TestLogger.new.send(:app_tag).must_equal 'App2' | ||
end | ||
end | ||
end | ||
App2::Bar.new.hoge | ||
end | ||
|
||
it 'uses custom app_tag from override class' do | ||
class TestLogger < Lotus::Logger; def app_tag; 'bar'; end; end | ||
output = | ||
stub_stdout_constant do | ||
TestLogger.new.info('') | ||
end | ||
output.must_match /bar/ | ||
end | ||
|
||
it 'has format "#{Severity}, [%Y-%m-%dT%H:%M:%S.%6N #{Pid}] #{Severity} -- [#{app_tag}] : #{message}\n"' do | ||
format = "%Y-%m-%dT%H:%M:%S.%6N " | ||
stub_time_now do | ||
strtime = Time.now.strftime(format) | ||
output = | ||
stub_stdout_constant do | ||
class TestLogger < Lotus::Logger;end | ||
TestLogger.new.info('foo') | ||
end | ||
output.must_equal "I, [1988-09-01T00:00:00.000000 ##{Process.pid}] INFO -- [Shared] : foo\n" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters