Module: ActiveSupport::TaggedLogging::Formatter

Defined in:
lib/activesupport/taggedlogging/formatter.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#call(severity, timestamp, progname, message) ⇒ Object

This method is invoked when a log event occurs.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/activesupport/taggedlogging/formatter.rb', line 6

def call(severity, timestamp, progname, message)
  # This scheme of serializing text log messages into a json format is
  # loosely based on the logstasher.log() method found here:
  # https://github.com/shadabahmed/logstasher/blob/master/lib/logstasher.rb#L160
  # The major difference is we use the TaggedLogging formater's tagging ability.
  # As implemented, TaggedLogging expects the proc's it executes return a scalar
  # value which it converts to a string wrapped with brackets. The resulting
  # behavior is logs that take the form of:
  # '[23432342] [234245] error parsing post request'
  # We could have left that intact by say having a tags field in our json doc that
  # points to our list of tags, but this lacks context. So what we've done is change
  # the convention in our application.rb to have our callback procs return hashes
  # instead of scalars. This way, we get the context by letting the proc provide keys
  # that describe each value.
  data = { 'level' => severity }
  if message.is_a? StandardError
    e = message
    data['type'] = e.class.to_s
    data['message'] = e.message
    data['backtrace'] = clean_backtrace(e).join("\n    ")
  elsif message.respond_to?(:to_hash)
    data.merge!(message.to_hash)
  else
    data['message'] = message
  end
  data['timestamp'] = timestamp.iso8601(6)

  data.merge!(user_defined_attributes)

  super(severity, timestamp, progname, data.to_json)
end

#clean_backtrace(exception) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/activesupport/taggedlogging/formatter.rb', line 53

def clean_backtrace(exception)
  if backtrace = exception.backtrace
    if defined?(RAILS_ROOT)
      backtrace.map { |line| line.sub RAILS_ROOT, '' }
    else
      backtrace
    end
  end
end

#user_defined_attributesObject



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/activesupport/taggedlogging/formatter.rb', line 38

def user_defined_attributes
  attrs = {
    "tags" => []
  }
  current_tags.each do |t|
    if t.respond_to?(:to_hash)
      attrs.merge!(t.to_hash)
    else
      attrs["tags"].push(t)
    end
  end
  attrs
end