Class: Makit::Logging::Formatters::JsonFormatter
- Defined in:
- lib/makit/logging/formatters/json_formatter.rb
Overview
JSON formatter for structured logging
Formats log requests as JSON objects with timestamp, level, message, and context. This is ideal for log aggregation systems and structured data analysis.
Instance Attribute Summary collapse
-
#options ⇒ Hash
readonly
Additional options for JSON formatting.
Instance Method Summary collapse
-
#config ⇒ Hash
Get formatter configuration.
-
#format(log_request) ⇒ String
Format log request as JSON.
-
#initialize(options: {}) ⇒ JsonFormatter
constructor
Initialize JSON formatter.
Constructor Details
#initialize(options: {}) ⇒ JsonFormatter
Initialize JSON formatter
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/makit/logging/formatters/json_formatter.rb', line 25 def initialize(options: {}) @options = { indent: nil, space: nil, space_before: nil, object_nl: nil, array_nl: nil, allow_nan: false, max_nesting: 100, }.merge() end |
Instance Attribute Details
#options ⇒ Hash (readonly)
Returns additional options for JSON formatting.
20 21 22 |
# File 'lib/makit/logging/formatters/json_formatter.rb', line 20 def @options end |
Instance Method Details
#config ⇒ Hash
Get formatter configuration
57 58 59 60 61 |
# File 'lib/makit/logging/formatters/json_formatter.rb', line 57 def config super.merge( json_options: @options, ) end |
#format(log_request) ⇒ String
Format log request as JSON
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/makit/logging/formatters/json_formatter.rb', line 41 def format(log_request) log_data = { timestamp: log_request..iso8601, level: log_request.level.to_s.upcase, message: log_request., } # Add context if present log_data[:context] = log_request.context unless log_request.context.empty? JSON.generate(log_data, @options) end |