Class: SemanticLogger::Appender::Base

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

Direct Known Subclasses

Bugsnag, File, MongoDB, NewRelic, Splunk, Syslog, Wrapper

Instance Attribute Summary collapse

Attributes inherited from Base

#filter, #name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#benchmark, default_level, default_level=, #level=, #payload, #pop_tags, #push_tags, #silence, #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


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
    tags   = log.tags.collect { |tag| "[#{colors::CYAN}#{tag}#{colors::CLEAR}]" }.join(' ') + ' ' if log.tags && (log.tags.size > 0)

    message = log.message.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
      message << ' -- ' << payload
    end
    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

    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_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
46
47
48
49
50
51
52
53
# 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 unless log.payload.nil? || (log.payload.respond_to?(:empty?) && log.payload.empty?)
    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) " : ''

    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

#flushObject



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

#levelObject

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