Class: Gaskit::Logger
- Inherits:
-
Object
- Object
- Gaskit::Logger
- Defined in:
- lib/gaskit/logger.rb
Overview
A logger class designed for structured logging with support for JSON, contextual data, and environment-aware formatting.
This logger wraps a configurable Logger instance and supports:
- Structured or human-readable log formatting
- Inclusion of global and per-call context (with filtering of sensitive keys)
- Support for custom log levels, formatters, and disabling output via
Gaskit.config
By default, logs are pretty-printed in development and JSON-formatted in production. These defaults can be overridden via configuration.
Constant Summary collapse
- SENSITIVE_KEYS =
i[email ip_address password auth_token secret ssn jwt token].freeze
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
Class Method Summary collapse
-
.formatter(formatter = :pretty) ⇒ Proc
Returns a built-in log formatter.
-
.json_formatter ⇒ Proc
JSON formatter (for production or structured logs).
-
.pretty_formatter ⇒ Proc
Pretty formatter (for dev logs).
Instance Method Summary collapse
-
#debug(message = nil, context: {}) { ... } ⇒ Object
Logs a debug-level message.
-
#error(message = nil, context: {}) { ... } ⇒ Object
Logs an error-level message.
-
#info(message = nil, context: {}) { ... } ⇒ Object
Logs an info-level message.
-
#initialize(klass, context: {}) ⇒ Logger
constructor
Initializes a new logger instance.
-
#log(level, message, context: {}) { ... } ⇒ Object
Logs a message at the specified level.
-
#warn(message = nil, context: {}) { ... } ⇒ Object
Logs a warning-level message.
-
#with_context(extra_context) ⇒ Gaskit::Logger
Creates a new logger instance with additional merged context.
Constructor Details
#initialize(klass, context: {}) ⇒ Logger
Initializes a new logger instance.
180 181 182 183 184 185 186 187 188 |
# File 'lib/gaskit/logger.rb', line 180 def initialize(klass, context: {}) @class_name = Gaskit::Helpers.resolve_name(klass) @context = apply_context(context).merge(class: @class_name) @logger = Gaskit.configuration.logger || ::Logger.new($stdout) rescue StandardError ::Logger.new($stdout).error "Failed to initialize logger: #{$ERROR_INFO}" @logger = ::Logger.new(nil) # fallback null logger end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
174 175 176 |
# File 'lib/gaskit/logger.rb', line 174 def context @context end |
Class Method Details
.formatter(formatter = :pretty) ⇒ Proc
Returns a built-in log formatter.
Use this method to obtain either the JSON or pretty formatter for logs.
This is useful when customizing the logger via Gaskit.config.
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/gaskit/logger.rb', line 87 def formatter(formatter = :pretty) case formatter when :json json_formatter when :pretty pretty_formatter else raise ArgumentError, "Invalid log formatter: #{formatter}" end end |
.json_formatter ⇒ Proc
JSON formatter (for production or structured logs)
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/gaskit/logger.rb', line 101 def json_formatter lambda do |severity, time, _progname, msg| , context = (msg) log_entry = { timestamp: time.utc.iso8601, level: severity.downcase, class: context[:class], message: , context: context&.reject { |k| k == :duration } }.compact log_entry[:duration] = context[:duration] if context&.key?(:duration) "#{JSON.dump(log_entry)}\n" end end |
.pretty_formatter ⇒ Proc
Pretty formatter (for dev logs)
122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/gaskit/logger.rb', line 122 def pretty_formatter lambda do |severity, time, _progname, msg| , context = (msg) context ||= {} class_name = context.delete(:class) = %W[[#{time.utc.iso8601}] [#{severity}]] << "[#{class_name}]" if class_name += flatten_context(context).map { |k, v| "[#{k}=#{v}]" } "#{tags.join(" ")} #{message}\n" end end |
Instance Method Details
#debug(message = nil, context: {}) { ... } ⇒ Object
Logs a debug-level message.
195 196 197 |
# File 'lib/gaskit/logger.rb', line 195 def debug( = nil, context: {}, &block) log(:debug, , context: context, &block) end |
#error(message = nil, context: {}) { ... } ⇒ Object
Logs an error-level message.
222 223 224 |
# File 'lib/gaskit/logger.rb', line 222 def error( = nil, context: {}, &block) log(:error, , context: context, &block) end |
#info(message = nil, context: {}) { ... } ⇒ Object
Logs an info-level message.
204 205 206 |
# File 'lib/gaskit/logger.rb', line 204 def info( = nil, context: {}, &block) log(:info, , context: context, &block) end |
#log(level, message, context: {}) { ... } ⇒ Object
Logs a message at the specified level.
232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/gaskit/logger.rb', line 232 def log(level, , context: {}, &block) return if Gaskit.configuration.disable_logging @logger.public_send(level) do combined_context = filtered_context(@context.merge(context)) combined_context[:class] ||= @class_name msg = || block&.call [msg, combined_context] end end |
#warn(message = nil, context: {}) { ... } ⇒ Object
Logs a warning-level message.
213 214 215 |
# File 'lib/gaskit/logger.rb', line 213 def warn( = nil, context: {}, &block) log(:warn, , context: context, &block) end |
#with_context(extra_context) ⇒ Gaskit::Logger
Creates a new logger instance with additional merged context.
249 250 251 |
# File 'lib/gaskit/logger.rb', line 249 def with_context(extra_context) self.class.new(@class_name, context: @context.merge(extra_context)) end |