Class: EnhanceSwarm::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/enhance_swarm/logger.rb

Constant Summary collapse

LOG_LEVELS =
{
  debug: ::Logger::DEBUG,
  info: ::Logger::INFO,
  warn: ::Logger::WARN,
  error: ::Logger::ERROR,
  fatal: ::Logger::FATAL
}.freeze

Class Method Summary collapse

Class Method Details

.create_loggerObject



21
22
23
24
25
26
# File 'lib/enhance_swarm/logger.rb', line 21

def self.create_logger
  logger = ::Logger.new($stdout)
  logger.level = log_level
  logger.formatter = method(:format_message)
  logger
end

.debug(msg, component: nil) ⇒ Object

Convenience methods



63
64
65
# File 'lib/enhance_swarm/logger.rb', line 63

def self.debug(msg, component: nil)
  logger.debug(msg) { component }
end

.error(msg, component: nil) ⇒ Object



75
76
77
# File 'lib/enhance_swarm/logger.rb', line 75

def self.error(msg, component: nil)
  logger.error(msg) { component }
end

.fatal(msg, component: nil) ⇒ Object



79
80
81
# File 'lib/enhance_swarm/logger.rb', line 79

def self.fatal(msg, component: nil)
  logger.fatal(msg) { component }
end

.format_human(severity, timestamp, progname, msg) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/enhance_swarm/logger.rb', line 51

def self.format_human(severity, timestamp, progname, msg)
  color = case severity
           when 'ERROR', 'FATAL' then :red
           when 'WARN' then :yellow
           when 'INFO' then :blue
           else :white
           end
  
  "[#{timestamp.strftime('%Y-%m-%d %H:%M:%S')}] #{severity.ljust(5)} #{msg}".colorize(color) + "\n"
end

.format_json(severity, timestamp, progname, msg) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/enhance_swarm/logger.rb', line 41

def self.format_json(severity, timestamp, progname, msg)
  {
    timestamp: timestamp.iso8601,
    level: severity,
    component: progname || 'enhance_swarm',
    message: msg.to_s,
    pid: Process.pid
  }.to_json + "\n"
end

.format_message(severity, timestamp, progname, msg) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/enhance_swarm/logger.rb', line 33

def self.format_message(severity, timestamp, progname, msg)
  if ENV['ENHANCE_SWARM_JSON_LOGS'] == 'true'
    format_json(severity, timestamp, progname, msg)
  else
    format_human(severity, timestamp, progname, msg)
  end
end

.info(msg, component: nil) ⇒ Object



67
68
69
# File 'lib/enhance_swarm/logger.rb', line 67

def self.info(msg, component: nil)
  logger.info(msg) { component }
end

.log_levelObject



28
29
30
31
# File 'lib/enhance_swarm/logger.rb', line 28

def self.log_level
  level = ENV['ENHANCE_SWARM_LOG_LEVEL']&.downcase&.to_sym || :info
  LOG_LEVELS[level] || ::Logger::INFO
end

.log_operation(operation, status, details = {}) ⇒ Object

Structured logging for automation



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/enhance_swarm/logger.rb', line 84

def self.log_operation(operation, status, details = {})
  log_data = {
    operation: operation,
    status: status,
    details: details,
    timestamp: Time.now.iso8601
  }

  case status
  when 'success', 'completed'
    info("Operation #{operation} completed successfully", component: 'operation')
  when 'failed', 'error'
    error("Operation #{operation} failed: #{details[:error]}", component: 'operation')
  when 'started', 'in_progress'
    info("Operation #{operation} started", component: 'operation')
  else
    debug("Operation #{operation}: #{status}", component: 'operation')
  end

  log_data
end

.loggerObject



17
18
19
# File 'lib/enhance_swarm/logger.rb', line 17

def self.logger
  @logger ||= create_logger
end

.warn(msg, component: nil) ⇒ Object



71
72
73
# File 'lib/enhance_swarm/logger.rb', line 71

def self.warn(msg, component: nil)
  logger.warn(msg) { component }
end