Class: EventLoggerRails::Output

Inherits:
Object
  • Object
show all
Defined in:
lib/event_logger_rails/output.rb

Overview

Merges data from application, request, and logger message for structured output

Instance Method Summary collapse

Constructor Details

#initialize(level:, timestamp:, message:) ⇒ Output

Initializes the output with the given level, timestamp, and message.

Parameters:

  • level (Symbol)

    The level of the event.

  • timestamp (Time)

    The timestamp of the event.

  • message (EventLoggerRails::Message)

    The message of the event.



11
12
13
14
15
16
# File 'lib/event_logger_rails/output.rb', line 11

def initialize(level:, timestamp:, message:)
  @current_request = EventLoggerRails::CurrentRequest
  @level = level
  @timestamp = timestamp.iso8601(3)
  @message = message.respond_to?(:to_hash) ? sanitizer.filter(**message) : { message: }
end

Instance Method Details

#to_hashHash

Converts the output to a hash containing the application, request, and logger details.

Returns:

  • (Hash)

    The hash representation of the output.



28
29
30
# File 'lib/event_logger_rails/output.rb', line 28

def to_hash
  application_data.merge(**current_request_data, **logger_data)
end

#to_json(*args) ⇒ String

Converts the output to a JSON string.

Returns:

  • (String)

    The JSON representation of the output.



21
22
23
# File 'lib/event_logger_rails/output.rb', line 21

def to_json(*args)
  JSON.generate(to_hash, *args)
end