Class: LanguageOperator::Logger

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#formatObject (readonly)

Returns the value of attribute format.



26
27
28
# File 'lib/language_operator/logger.rb', line 26

def format
  @format
end

#loggerObject (readonly)

Returns the value of attribute logger.



26
27
28
# File 'lib/language_operator/logger.rb', line 26

def logger
  @logger
end

#show_timingObject (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(message, **)
  log(:debug, message, **)
end

#error(message, **metadata) ⇒ Object



53
54
55
# File 'lib/language_operator/logger.rb', line 53

def error(message, **)
  log(:error, message, **)
end

#info(message, **metadata) ⇒ Object



45
46
47
# File 'lib/language_operator/logger.rb', line 45

def info(message, **)
  log(:info, message, **)
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(message, **)
  start_time = Time.now
  result = yield if block_given?
  duration = Time.now - start_time

  info(message, **, duration_s: duration.round(3))
  result
end

#warn(message, **metadata) ⇒ Object



49
50
51
# File 'lib/language_operator/logger.rb', line 49

def warn(message, **)
  log(:warn, message, **)
end