Module: ActiveSupport::TaggedLogging::Formatter
- Defined in:
- lib/activesupport/taggedlogging/formatter.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#call(severity, timestamp, progname, message) ⇒ Object
This method is invoked when a log event occurs.
-
#clean_backtrace(exception) ⇒ Object
Taken from stackoverflow.com/a/237846.
- #user_defined_attributes ⇒ Object
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, , progname, ) # 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 .is_a? StandardError e = data['type'] = e.class.to_s data['message'] = e. data['backtrace'] = clean_backtrace(e).join("\n ") elsif .respond_to?(:to_hash) data.merge!(.to_hash) else data['message'] = end data['timestamp'] = .iso8601(6) data.merge!(user_defined_attributes) super(severity, , progname, data.to_json) end |
#clean_backtrace(exception) ⇒ Object
Taken from stackoverflow.com/a/237846
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_attributes ⇒ Object
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" => [] } .each do |t| if t.respond_to?(:to_hash) attrs.merge!(t.to_hash) else attrs["tags"].push(t) end end attrs end |