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.



38
39
40
# File 'lib/semantic_logger/appender/base.rb', line 38

def formatter
  @formatter
end

Class Method Details

.colorized_formatterObject

Optional log formatter to colorize log output To use this formatter

SemanticLogger.add_appender($stdout, &SemanticLogger::Appender::Base.colorized_formatter)

 2011-07-19 14:36:15.660 D [1149:ScriptThreadProcess] Rails -- Hello World


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/semantic_logger/appender/base.rb', line 80

def self.colorized_formatter
  Proc.new do |log|
    colors      = SemanticLogger::Appender::AnsiColors
    level_color = colors::LEVEL_MAP[log.level]

    # Header with date, time, log level and process info
    entry = "#{log.formatted_time} #{level_color}#{log.level_to_s}#{colors::CLEAR} [#{log.process_info}]"

    # Tags
    entry << ' ' << log.tags.collect { |tag| "[#{level_color}#{tag}#{colors::CLEAR}]" }.join(' ') if log.tags && (log.tags.size > 0)

    # Duration
    entry << " (#{colors::BOLD}#{log.duration_human}#{colors::CLEAR})" if log.duration

    # Class / app name
    entry << " #{level_color}#{log.name}#{colors::CLEAR}"

    # Log message
    entry << " -- #{log.message}" if log.message

    # Payload
    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
      entry << ' -- ' << payload
    end

    # Exceptions
    log.each_exception do |exception, i|
      entry << (i == 0 ? ' -- Exception: ' : "\nCause: ")
      entry << "#{colors::BOLD}#{exception.class}: #{exception.message}#{colors::CLEAR}\n#{(exception.backtrace || []).join("\n")}"
    end
    entry
  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


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/semantic_logger/appender/base.rb', line 44

def default_formatter
  Proc.new do |log|
    # Header with date, time, log level and process info
    entry = "#{log.formatted_time} #{log.level_to_s} [#{log.process_info}]"

    # Tags
    entry << ' ' << log.tags.collect { |tag| "[#{tag}]" }.join(' ') if log.tags && (log.tags.size > 0)

    # Duration
    entry << " (#{log.duration_human})" if log.duration

    # Class / app name
    entry << " #{log.name}"

    # Log message
    entry << " -- #{log.message}" if log.message

    # Payload
    unless log.payload.nil? || (log.payload.respond_to?(:empty?) && log.payload.empty?)
      entry << ' -- ' << log.payload.inspect
    end

    # Exceptions
    log.each_exception do |exception, i|
      entry << (i == 0 ? ' -- Exception: ' : "\nCause: ")
      entry << "#{exception.class}: #{exception.message}\n#{(exception.backtrace || []).join("\n")}"
    end
    entry
  end
end

#flushObject



116
117
118
# File 'lib/semantic_logger/appender/base.rb', line 116

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



122
123
124
# File 'lib/semantic_logger/appender/base.rb', line 122

def level
  @level || :trace
end