Class: LanguageOperator::Logger
- Inherits:
-
Object
- Object
- LanguageOperator::Logger
- Defined in:
- lib/language_operator/logger.rb
Overview
Structured logger with configurable output formats and levels
Supports multiple output formats:
- :pretty (default): Human-readable with emojis and colors
- :text: Plain text with timestamps
- :json: Structured JSON output
Environment variables:
- LOG_LEVEL: DEBUG, INFO, WARN, ERROR (default: INFO)
- LOG_FORMAT: pretty, text, json (default: pretty)
- LOG_TIMING: true/false - Include operation timing (default: true)
Constant Summary collapse
- LEVELS =
{ 'DEBUG' => ::Logger::DEBUG, 'INFO' => ::Logger::INFO, 'WARN' => ::Logger::WARN, 'ERROR' => ::Logger::ERROR }.freeze
Instance Attribute Summary collapse
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#show_timing ⇒ Object
readonly
Returns the value of attribute show_timing.
Instance Method Summary collapse
- #debug(message, **metadata) ⇒ Object
- #error(message, **metadata) ⇒ Object
- #info(message, **metadata) ⇒ Object
-
#initialize(component: 'Langop', format: nil, level: nil) ⇒ Logger
constructor
A new instance of Logger.
-
#timed(message, **metadata) ⇒ Object
Log with timing information.
- #warn(message, **metadata) ⇒ Object
Constructor Details
#initialize(component: 'Langop', format: nil, level: nil) ⇒ Logger
Returns a new instance of Logger.
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/language_operator/logger.rb', line 28 def initialize(component: 'Langop', format: nil, level: nil) @component = component @format = format || ENV.fetch('LOG_FORMAT', 'pretty').to_sym @show_timing = ENV.fetch('LOG_TIMING', 'true') == 'true' log_level_name = level || ENV.fetch('LOG_LEVEL', 'INFO') log_level = LEVELS[log_level_name.upcase] || ::Logger::INFO @logger = ::Logger.new($stdout) @logger.level = log_level @logger.formatter = method(:format_message) end |
Instance Attribute Details
#format ⇒ Object (readonly)
Returns the value of attribute format.
26 27 28 |
# File 'lib/language_operator/logger.rb', line 26 def format @format end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
26 27 28 |
# File 'lib/language_operator/logger.rb', line 26 def logger @logger end |
#show_timing ⇒ Object (readonly)
Returns the value of attribute show_timing.
26 27 28 |
# File 'lib/language_operator/logger.rb', line 26 def show_timing @show_timing end |
Instance Method Details
#debug(message, **metadata) ⇒ Object
41 42 43 |
# File 'lib/language_operator/logger.rb', line 41 def debug(, **) log(:debug, , **) end |
#error(message, **metadata) ⇒ Object
53 54 55 |
# File 'lib/language_operator/logger.rb', line 53 def error(, **) log(:error, , **) end |
#info(message, **metadata) ⇒ Object
45 46 47 |
# File 'lib/language_operator/logger.rb', line 45 def info(, **) log(:info, , **) end |
#timed(message, **metadata) ⇒ Object
Log with timing information
58 59 60 61 62 63 64 65 |
# File 'lib/language_operator/logger.rb', line 58 def timed(, **) start_time = Time.now result = yield if block_given? duration = Time.now - start_time info(, **, duration_s: duration.round(3)) result end |
#warn(message, **metadata) ⇒ Object
49 50 51 |
# File 'lib/language_operator/logger.rb', line 49 def warn(, **) log(:warn, , **) end |