Class: Heathrow::Logger

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

Overview

Logger - Structured logging for Heathrow

Usage:

log = Heathrow::Logger.instance
log.info("Application started")
log.error("Failed to connect", error: e, source_id: 123)
log.debug("Processing message", message_id: 456)

Log Levels: DEBUG < INFO < WARN < ERROR < FATAL

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log_file = nil, level: :info) ⇒ Logger

Returns a new instance of Logger.



26
27
28
29
30
31
32
# File 'lib/heathrow/logger.rb', line 26

def initialize(log_file = nil, level: :info)
  @log_file = log_file || default_log_file
  ensure_log_directory
  @logger = ::Logger.new(@log_file, 'daily')
  @logger.level = LEVELS[level] || ::Logger::INFO
  @logger.formatter = method(:format_message)
end

Instance Attribute Details

#log_fileObject (readonly)

Returns the value of attribute log_file.



24
25
26
# File 'lib/heathrow/logger.rb', line 24

def log_file
  @log_file
end

#loggerObject (readonly)

Returns the value of attribute logger.



24
25
26
# File 'lib/heathrow/logger.rb', line 24

def logger
  @logger
end

Class Method Details

.configure(log_file: nil, level: :info) ⇒ Object

Configure the singleton instance



116
117
118
119
# File 'lib/heathrow/logger.rb', line 116

def configure(log_file: nil, level: :info)
  reset_instance!
  @instance = new(log_file, level: level)
end

.instanceObject



106
107
108
# File 'lib/heathrow/logger.rb', line 106

def instance
  @instance ||= new
end

.reset_instance!Object



110
111
112
113
# File 'lib/heathrow/logger.rb', line 110

def reset_instance!
  @instance&.close
  @instance = nil
end

Instance Method Details

#closeObject

Close logger



83
84
85
# File 'lib/heathrow/logger.rb', line 83

def close
  @logger.close if @logger
end

#debug(message, context = {}) ⇒ Object

Log methods



35
36
37
# File 'lib/heathrow/logger.rb', line 35

def debug(message, context = {})
  log(:debug, message, context)
end

#error(message, context = {}) ⇒ Object



47
48
49
# File 'lib/heathrow/logger.rb', line 47

def error(message, context = {})
  log(:error, message, context)
end

#fatal(message, context = {}) ⇒ Object



51
52
53
# File 'lib/heathrow/logger.rb', line 51

def fatal(message, context = {})
  log(:fatal, message, context)
end

#info(message, context = {}) ⇒ Object



39
40
41
# File 'lib/heathrow/logger.rb', line 39

def info(message, context = {})
  log(:info, message, context)
end

#levelObject

Get current log level as symbol



78
79
80
# File 'lib/heathrow/logger.rb', line 78

def level
  LEVELS.key(@logger.level) || :info
end

#level=(level) ⇒ Object

Change log level



73
74
75
# File 'lib/heathrow/logger.rb', line 73

def level=(level)
  @logger.level = LEVELS[level] || ::Logger::INFO
end

#log(level, message, context = {}) ⇒ Object

Generic log method



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/heathrow/logger.rb', line 56

def log(level, message, context = {})
  return unless @logger

  # Extract error if present
  if context[:error].is_a?(Exception)
    error = context.delete(:error)
    context[:error_class] = error.class.name
    context[:error_message] = error.message
    context[:backtrace] = error.backtrace&.first(5)
  end

  @logger.send(level, message) do
    context.empty? ? message : "#{message} #{context.inspect}"
  end
end

#warn(message, context = {}) ⇒ Object



43
44
45
# File 'lib/heathrow/logger.rb', line 43

def warn(message, context = {})
  log(:warn, message, context)
end