Class: SemanticLogger::Appender::Base

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

Direct Known Subclasses

Bugsnag, File, Graylog, Http, 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=, #fast_tag, #level=, #payload, #pop_tags, #push_tags, #silence, #tagged, #tags, #with_payload

Instance Attribute Details

#formatterObject

Returns the value of attribute formatter.



45
46
47
# File 'lib/semantic_logger/appender/base.rb', line 45

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


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
115
116
117
118
119
# File 'lib/semantic_logger/appender/base.rb', line 87

def self.colorized_formatter
  Proc.new do |log, logger|
    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
    if payload = log.payload_to_s(true)
      entry << ' -- ' << payload
    end

    # Exceptions
    if log.exception
      entry << " -- Exception: #{colors::BOLD}#{log.exception.class}: #{log.exception.message}#{colors::CLEAR}\n"
      entry << log.backtrace_to_s
    end
    entry
  end
end

.json_formatterObject

Optional log formatter to output data in a hash format To use this formatter

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


124
125
126
127
128
129
130
131
# File 'lib/semantic_logger/appender/base.rb', line 124

def self.json_formatter
  Proc.new do |log, logger|
    h = log.to_h
    h.delete(:time)
    h[:timestamp] = log.time.utc.iso8601(defined?(JRuby) ? 3 : 6)
    h.to_json
  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


51
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
79
80
# File 'lib/semantic_logger/appender/base.rb', line 51

def default_formatter
  Proc.new do |log, logger|
    # 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
    if payload = log.payload_to_s(false)
      entry << ' -- ' << payload
    end

    # Exceptions
    if log.exception
      entry << " -- Exception: #{log.exception.class}: #{log.exception.message}\n"
      entry << log.backtrace_to_s
    end
    entry
  end
end

#flushObject



133
134
135
# File 'lib/semantic_logger/appender/base.rb', line 133

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



139
140
141
# File 'lib/semantic_logger/appender/base.rb', line 139

def level
  @level || :trace
end