Class: SemanticLogger::Appender::Base
- Defined in:
- lib/semantic_logger/appender/base.rb
Instance Attribute Summary collapse
-
#formatter ⇒ Object
Returns the value of attribute formatter.
Attributes inherited from Base
Class Method Summary collapse
-
.colorized_formatter ⇒ Object
Optional log formatter to colorize log output To use this formatter SemanticLogger.add_appender($stdout, nil, &SemanticLogger::Logger.colorized_formatter).
Instance Method Summary collapse
-
#default_formatter ⇒ Object
Default log formatter Replace this formatter by supplying a Block to the initializer Generates logs of the form: 2011-07-19 14:36:15.660 D [1149:ScriptThreadProcess] Rails – Hello World.
- #flush ⇒ Object
-
#level ⇒ Object
Returns the current log level if set, otherwise it returns the global default log level.
Methods inherited from Base
#benchmark, default_level, default_level=, #level=, #payload, #pop_tags, #push_tags, #silence, #tagged, #tags, #with_payload
Instance Attribute Details
#formatter ⇒ Object
Returns the value of attribute formatter.
27 28 29 |
# File 'lib/semantic_logger/appender/base.rb', line 27 def formatter @formatter end |
Class Method Details
.colorized_formatter ⇒ Object
Optional log formatter to colorize log output To use this formatter
SemanticLogger.add_appender($stdout, nil, &SemanticLogger::Logger.colorized_formatter)
2011-07-19 14:36:15.660 D [1149:ScriptThreadProcess] Rails -- Hello World
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/semantic_logger/appender/base.rb', line 60 def self.colorized_formatter Proc.new do |log| colors = SemanticLogger::Appender::AnsiColors = log..collect { |tag| "[#{colors::CYAN}#{tag}#{colors::CLEAR}]" }.join(' ') + ' ' if log. && (log..size > 0) = log..to_s.dup unless log.payload.nil? || (log.payload.respond_to?(:empty?) && log.payload.empty?) payload = log.payload payload = (defined?(AwesomePrint) && payload.respond_to?(:ai)) ? payload.ai(multiline: false) : payload.inspect << ' -- ' << payload end << ' -- Exception: ' << "#{colors::BOLD}#{log.exception.class}: #{log.exception.message}#{colors::CLEAR}\n#{(log.exception.backtrace || []).join("\n")}" if log.exception duration_str = log.duration ? "(#{colors::BOLD}#{'%.1f' % log.duration}ms#{colors::CLEAR}) " : '' level_color = case log.level when :trace colors::MAGENTA when :debug colors::GREEN when :info colors::CYAN when :warn colors::BOLD when :error, :fatal colors::RED end file_name = if log.backtrace || log.exception backtrace = log.backtrace || log.exception.backtrace location = backtrace[0].split('/').last file, line = location.split(':') " #{level_color}#{file}:#{line}#{colors::CLEAR}" end "#{SemanticLogger::Appender::Base.formatted_time(log.time)} #{level_color}#{colors::BOLD}#{log.level.to_s[0..0].upcase}#{colors::CLEAR} [#{$$}:#{'%.30s' % log.thread_name}#{file_name}] #{tags}#{duration_str}#{level_color}#{log.name}#{colors::CLEAR} -- #{message}" end end |
Instance Method Details
#default_formatter ⇒ Object
Default log formatter
Replace this formatter by a Block to the initializer
Generates logs of the form:
2011-07-19 14:36:15.660 D [1149:ScriptThreadProcess] Rails -- Hello World
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/semantic_logger/appender/base.rb', line 33 def default_formatter Proc.new do |log| = log..collect { |tag| "[#{tag}]" }.join(' ') + ' ' if log. && (log..size > 0) = log..to_s.dup << ' -- ' << log.payload.inspect unless log.payload.nil? || (log.payload.respond_to?(:empty?) && log.payload.empty?) << ' -- Exception: ' << "#{log.exception.class}: #{log.exception.message}\n#{(log.exception.backtrace || []).join("\n")}" if log.exception duration_str = log.duration ? "(#{'%.1f' % log.duration}ms) " : '' file_name = if log.backtrace || log.exception backtrace = log.backtrace || log.exception.backtrace location = backtrace[0].split('/').last file, line = location.split(':') " #{file}:#{line}" end "#{SemanticLogger::Appender::Base.formatted_time(log.time)} #{log.level.to_s[0..0].upcase} [#{$$}:#{'%.50s' % log.thread_name}#{file_name}] #{tags}#{duration_str}#{log.name} -- #{message}" end end |
#flush ⇒ Object
101 102 103 |
# File 'lib/semantic_logger/appender/base.rb', line 101 def flush # An appender can implement a flush method if it supports it. end |
#level ⇒ Object
Returns the current log level if set, otherwise it returns the global default log level
107 108 109 |
# File 'lib/semantic_logger/appender/base.rb', line 107 def level @level || :trace end |