Class: Lumberjack::MessageAttributes

Inherits:
Object
  • Object
show all
Defined in:
lib/lumberjack/message_attributes.rb

Overview

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.

Examples:

# 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
    }
  })
})

Direct Known Subclasses

Formatter::TaggedMessage

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, attributes) ⇒ MessageAttributes

Returns a new instance of MessageAttributes.

Parameters:

  • message (Object)

    The message to be logged.

  • attributes (Hash)

    The attributes to be associated with the message.



25
26
27
28
# File 'lib/lumberjack/message_attributes.rb', line 25

def initialize(message, attributes)
  @message = message
  @attributes = attributes || {}
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



21
22
23
# File 'lib/lumberjack/message_attributes.rb', line 21

def attributes
  @attributes
end

#messageObject (readonly)

Returns the value of attribute message.



21
22
23
# File 'lib/lumberjack/message_attributes.rb', line 21

def message
  @message
end

Instance Method Details

#inspectObject



34
35
36
# File 'lib/lumberjack/message_attributes.rb', line 34

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

#to_sObject



30
31
32
# File 'lib/lumberjack/message_attributes.rb', line 30

def to_s
  inspect
end