Class: SemanticLogger::Appender::Base

Inherits:
Base
  • Object
show all
Defined in:
lib/semantic_logger/appender/base.rb

Direct Known Subclasses

File, MongoDB, Syslog, Wrapper

Instance Attribute Summary collapse

Attributes inherited from Base

#level, #name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

default_level, default_level=, #payload, #pop_tags, #push_tags, #tagged, #tags, #with_payload

Instance Attribute Details

#formatterObject

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_formatterObject

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


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/semantic_logger/appender/base.rb', line 52

def self.colorized_formatter
  Proc.new do |log|
    colors = SemanticLogger::Appender::AnsiColors
    tags = log.tags.collect { |tag| "[#{colors::CYAN}#{tag}#{colors::CLEAR}]" }.join(' ') + ' ' if log.tags && (log.tags.size > 0)

    message = log.message.to_s.dup
    message << " -- " << log.payload.inspect if log.payload
    message << " -- 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

    "#{SemanticLogger::Appender::Base.formatted_time(log.time)} #{level_color}#{colors::BOLD}#{log.level.to_s[0..0].upcase}#{colors::CLEAR} [#{$$}:#{'%.30s' % log.thread_name}] #{tags}#{duration_str}#{level_color}#{log.name}#{colors::CLEAR} -- #{message}"
  end
end

Instance Method Details

#default_formatterObject

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


33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/semantic_logger/appender/base.rb', line 33

def default_formatter
  Proc.new do |log|
    tags = log.tags.collect { |tag| "[#{tag}]" }.join(" ") + " " if log.tags && (log.tags.size > 0)

    message = log.message.to_s.dup
    message << " -- " << log.payload.inspect if log.payload
    message << " -- Exception: " << "#{log.exception.class}: #{log.exception.message}\n#{(log.exception.backtrace || []).join("\n")}" if log.exception

    duration_str = log.duration ? "(#{'%.1f' % log.duration}ms) " : ''

    "#{SemanticLogger::Appender::Base.formatted_time(log.time)} #{log.level.to_s[0..0].upcase} [#{$$}:#{'%.50s' % log.thread_name}] #{tags}#{duration_str}#{log.name} -- #{message}"
  end
end