Class: Makit::Logging::Sinks::Structured

Inherits:
Base
  • Object
show all
Defined in:
lib/makit/logging/sinks/structured.rb

Overview

Structured sink for adding metadata to log requests

This sink enriches log requests with structured metadata like process information, thread details, and other contextual data that can be useful for debugging and monitoring. It follows the same pattern as other sinks but focuses on data enrichment rather than output.

Examples:

Basic usage

structured_sink = Structured.new
logger = Logger.new(sinks: [structured_sink])
logger.info("Processing started")
# Adds metadata like process_id, thread_id, etc.

With custom metadata

structured_sink = Structured.new(
  custom_metadata: { environment: "production" }
)

Instance Method Summary collapse

Methods inherited from Base

#applicable?, #name

Constructor Details

#initialize(include_process_info: true, include_thread_info: true, include_timing: true, custom_metadata: {}) ⇒ Structured

Initialize structured sink

Parameters:

  • include_process_info (Boolean) (defaults to: true)

    whether to include process information

  • include_thread_info (Boolean) (defaults to: true)

    whether to include thread information

  • include_timing (Boolean) (defaults to: true)

    whether to include timing information

  • custom_metadata (Hash) (defaults to: {})

    custom metadata to add to all log requests



32
33
34
35
36
37
# File 'lib/makit/logging/sinks/structured.rb', line 32

def initialize(include_process_info: true, include_thread_info: true, include_timing: true, custom_metadata: {})
  @include_process_info = include_process_info
  @include_thread_info = include_thread_info
  @include_timing = include_timing
  @custom_metadata = .dup
end

Instance Method Details

#call(log_request) {|LogRequest| ... } ⇒ LogRequest

Execute sink logic to add structured metadata

Parameters:

  • log_request (LogRequest)

    the log request to process

Yields:

  • (LogRequest)

    yields the processed log request to the next sink

Yield Returns:

Returns:

  • (LogRequest)

    the final processed log request



45
46
47
48
49
# File 'lib/makit/logging/sinks/structured.rb', line 45

def call(log_request, &block)
  (log_request)
  block&.call(log_request) if block_given?
  log_request
end

#configHash

Get sink configuration

Returns:

  • (Hash)

    sink configuration



54
55
56
57
58
59
60
61
62
# File 'lib/makit/logging/sinks/structured.rb', line 54

def config
  {
    name: self.class.name.split("::").last,
    include_process_info: @include_process_info,
    include_thread_info: @include_thread_info,
    include_timing: @include_timing,
    custom_metadata: @custom_metadata,
  }
end