File: message_attributes.rb

package info (click to toggle)
ruby-lumberjack 2.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 956 kB
  • sloc: ruby: 7,957; makefile: 2
file content (38 lines) | stat: -rw-r--r-- 1,111 bytes parent folder | download
1
2
3
4
5
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
37
38
# frozen_string_literal: true

module Lumberjack
  # This class can be used as the return value from a formatter +call+ method to
  # extract additional attributes from an object being logged. This can be useful when there
  # using structured logging to include important metadata in the log entry in addition
  # to the message.
  #
  # @example
  #  # Automatically add attributes with error details when logging an exception.
  #  logger.add_formatter(Exception, ->(e) {
  #    Lumberjack::MessageAttributes.new(e.inspect, {
  #      error: {
  #        message: e.message,
  #        class: e.class.name,
  #        stack: e.backtrace
  #      }
  #    })
  #  })
  class MessageAttributes
    attr_reader :message, :attributes

    # @param message [Object] The message to be logged.
    # @param attributes [Hash] The attributes to be associated with the message.
    def initialize(message, attributes)
      @message = message
      @attributes = attributes || {}
    end

    def to_s
      inspect
    end

    def inspect
      {message: @message, attributes: @attributes}.inspect
    end
  end
end