Class: Pangea::Logging::StructuredLogger
- Includes:
- Formatters
- Defined in:
- lib/pangea/logging/structured_logger.rb
Constant Summary collapse
- LEVELS =
{ debug: 0, info: 1, warn: 2, error: 3, fatal: 4 }.freeze
Constants included from Formatters
Formatters::EXCLUDED_CONTEXT_KEYS, Formatters::LEVEL_ROLES
Instance Attribute Summary collapse
-
#correlation_id ⇒ Object
readonly
Returns the value of attribute correlation_id.
-
#log_level ⇒ Object
readonly
Returns the value of attribute log_level.
-
#output_format ⇒ Object
readonly
Returns the value of attribute output_format.
Instance Method Summary collapse
- #add_metadata(key, value) ⇒ Object
- #child(**context) ⇒ Object
- #clear_metadata ⇒ Object
-
#initialize(output: $stdout, level: :info, format: nil, correlation_id: nil) ⇒ StructuredLogger
constructor
A new instance of StructuredLogger.
- #measure(operation_name, level: :info, **context) ⇒ Object
- #metric(name, value, unit: nil, **tags) ⇒ Object
Methods included from Formatters
#format_json, #format_logfmt, #format_pretty, #format_simple
Constructor Details
#initialize(output: $stdout, level: :info, format: nil, correlation_id: nil) ⇒ StructuredLogger
Returns a new instance of StructuredLogger.
37 38 39 40 41 42 43 |
# File 'lib/pangea/logging/structured_logger.rb', line 37 def initialize(output: $stdout, level: :info, format: nil, correlation_id: nil) @output = output @log_level = LEVELS[level] || LEVELS[:info] @output_format = determine_format(format) @correlation_id = correlation_id || SecureRandom.uuid @metadata = {} end |
Instance Attribute Details
#correlation_id ⇒ Object (readonly)
Returns the value of attribute correlation_id.
35 36 37 |
# File 'lib/pangea/logging/structured_logger.rb', line 35 def correlation_id @correlation_id end |
#log_level ⇒ Object (readonly)
Returns the value of attribute log_level.
35 36 37 |
# File 'lib/pangea/logging/structured_logger.rb', line 35 def log_level @log_level end |
#output_format ⇒ Object (readonly)
Returns the value of attribute output_format.
35 36 37 |
# File 'lib/pangea/logging/structured_logger.rb', line 35 def output_format @output_format end |
Instance Method Details
#add_metadata(key, value) ⇒ Object
51 52 53 |
# File 'lib/pangea/logging/structured_logger.rb', line 51 def (key, value) @metadata[key] = value end |
#child(**context) ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/pangea/logging/structured_logger.rb', line 59 def child(**context) child_logger = self.class.new( output: @output, level: LEVELS.key(@log_level), format: @output_format, correlation_id: @correlation_id ) child_logger.instance_variable_set(:@metadata, @metadata.merge(context)) child_logger end |
#clear_metadata ⇒ Object
55 56 57 |
# File 'lib/pangea/logging/structured_logger.rb', line 55 def @metadata.clear end |
#measure(operation_name, level: :info, **context) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/pangea/logging/structured_logger.rb', line 70 def measure(operation_name, level: :info, **context) start_time = Time.now log_context = context.merge(operation: operation_name, status: 'started') log(level, "Operation started: #{operation_name}", **log_context) begin result = yield duration = Time.now - start_time log_context.merge!(status: 'completed', duration_ms: (duration * 1000).round(2)) log(level, "Operation completed: #{operation_name}", **log_context) result rescue => e duration = Time.now - start_time log_context.merge!( status: 'failed', duration_ms: (duration * 1000).round(2), error: e.class.name, error_message: e. ) error("Operation failed: #{operation_name}", **log_context) raise end end |
#metric(name, value, unit: nil, **tags) ⇒ Object
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/pangea/logging/structured_logger.rb', line 95 def metric(name, value, unit: nil, **) context = { metric_name: name, metric_value: value, metric_unit: unit, metric_tags: }.compact info("Metric recorded: #{name}", **context) end |