Class: LambdaLoadout::Logger
- Inherits:
-
Object
- Object
- LambdaLoadout::Logger
- Defined in:
- lib/lambda_loadout/logger.rb
Overview
Structured logger with Lambda context enrichment
Provides JSON structured logging with automatic Lambda context injection, correlation IDs, and support for custom fields.
Constant Summary collapse
- LOG_LEVELS =
{ debug: 0, info: 1, warn: 2, error: 3, fatal: 4 }.freeze
Instance Attribute Summary collapse
-
#lambda_context ⇒ Object
Returns the value of attribute lambda_context.
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#service ⇒ Object
readonly
Returns the value of attribute service.
Instance Method Summary collapse
-
#append_keys(**fields) ⇒ void
Add persistent fields that will be included in all log entries.
-
#debug(message, exception = nil, **fields) ⇒ void
Log debug message.
-
#error(message, exception = nil, **fields) ⇒ void
Log error message.
-
#exception(exception, message: nil, **fields) ⇒ void
Log an exception with full details.
-
#fatal(message, **fields) ⇒ void
Log fatal message.
-
#info(message, exception = nil, **fields) ⇒ void
Log info message.
-
#initialize(service: nil, level: :info, output: $stdout, sampling_rate: nil) ⇒ Logger
constructor
Initialize a new structured logger.
-
#inject_lambda_context(context) ⇒ void
Inject Lambda context into logs.
-
#remove_keys(*keys) ⇒ void
Remove persistent fields.
-
#warn(message, exception = nil, **fields) ⇒ void
Log warning message.
Constructor Details
#initialize(service: nil, level: :info, output: $stdout, sampling_rate: nil) ⇒ Logger
Initialize a new structured logger
42 43 44 45 46 47 48 49 50 |
# File 'lib/lambda_loadout/logger.rb', line 42 def initialize(service: nil, level: :info, output: $stdout, sampling_rate: nil) @service = service || ENV.fetch('POWERTOOLS_SERVICE_NAME', nil) @level = LOG_LEVELS[level.to_sym] || LOG_LEVELS[:info] @output = output @sampling_rate = sampling_rate&.to_f @lambda_context = nil @persistent_fields = {} @persistent_fields['service'] = @service if @service end |
Instance Attribute Details
#lambda_context ⇒ Object
Returns the value of attribute lambda_context.
26 27 28 |
# File 'lib/lambda_loadout/logger.rb', line 26 def lambda_context @lambda_context end |
#level ⇒ Object (readonly)
Returns the value of attribute level.
25 26 27 |
# File 'lib/lambda_loadout/logger.rb', line 25 def level @level end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
25 26 27 |
# File 'lib/lambda_loadout/logger.rb', line 25 def output @output end |
#service ⇒ Object (readonly)
Returns the value of attribute service.
25 26 27 |
# File 'lib/lambda_loadout/logger.rb', line 25 def service @service end |
Instance Method Details
#append_keys(**fields) ⇒ void
This method returns an undefined value.
Add persistent fields that will be included in all log entries
64 65 66 |
# File 'lib/lambda_loadout/logger.rb', line 64 def append_keys(**fields) @persistent_fields.merge!(fields.transform_keys(&:to_s)) end |
#debug(message, exception = nil, **fields) ⇒ void
This method returns an undefined value.
Log debug message
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/lambda_loadout/logger.rb', line 82 def debug(, exception = nil, **fields) return unless should_log?(:debug) if exception.is_a?(Exception) fields[:error] = exception. fields[:error_class] = exception.class.name fields[:backtrace] = exception.backtrace&.first(10) end log(:debug, , **fields) end |
#error(message, exception = nil, **fields) ⇒ void
This method returns an undefined value.
Log error message
136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/lambda_loadout/logger.rb', line 136 def error(, exception = nil, **fields) return unless should_log?(:error) if exception.is_a?(Exception) fields[:error] = exception. fields[:error_class] = exception.class.name fields[:backtrace] = exception.backtrace&.first(10) end log(:error, , **fields) end |
#exception(exception, message: nil, **fields) ⇒ void
This method returns an undefined value.
Log an exception with full details
165 166 167 168 169 170 171 172 173 |
# File 'lib/lambda_loadout/logger.rb', line 165 def exception(exception, message: nil, **fields) error( || exception., exception: exception.class.name, error_message: exception., backtrace: exception.backtrace&.first(10), **fields ) end |
#fatal(message, **fields) ⇒ void
This method returns an undefined value.
Log fatal message
153 154 155 156 157 |
# File 'lib/lambda_loadout/logger.rb', line 153 def fatal(, **fields) return unless should_log?(:fatal) log(:fatal, , **fields) end |
#info(message, exception = nil, **fields) ⇒ void
This method returns an undefined value.
Log info message
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/lambda_loadout/logger.rb', line 100 def info(, exception = nil, **fields) return unless should_log?(:info) if exception.is_a?(Exception) fields[:error] = exception. fields[:error_class] = exception.class.name fields[:backtrace] = exception.backtrace&.first(10) end log(:info, , **fields) end |
#inject_lambda_context(context) ⇒ void
This method returns an undefined value.
Inject Lambda context into logs
56 57 58 |
# File 'lib/lambda_loadout/logger.rb', line 56 def inject_lambda_context(context) @lambda_context = context end |
#remove_keys(*keys) ⇒ void
This method returns an undefined value.
Remove persistent fields
72 73 74 |
# File 'lib/lambda_loadout/logger.rb', line 72 def remove_keys(*keys) keys.each { |key| @persistent_fields.delete(key.to_s) } end |
#warn(message, exception = nil, **fields) ⇒ void
This method returns an undefined value.
Log warning message
118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/lambda_loadout/logger.rb', line 118 def warn(, exception = nil, **fields) return unless should_log?(:warn) if exception.is_a?(Exception) fields[:error] = exception. fields[:error_class] = exception.class.name fields[:backtrace] = exception.backtrace&.first(10) end log(:warn, , **fields) end |