Class: DeadBro::Logger

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

Constant Summary collapse

SEVERITY_LEVELS =
{
  debug: 0,
  info: 1,
  warn: 2,
  error: 3,
  fatal: 4
}.freeze
COLOR_RESET =

ANSI color codes

"\033[0m"
COLOR_DEBUG =

Cyan

"\033[36m"
COLOR_INFO =

Green

"\033[32m"
COLOR_WARN =

Yellow

"\033[33m"
COLOR_ERROR =

Red

"\033[31m"
COLOR_FATAL =

Magenta

"\033[35m"
MAX_LOG_ENTRIES =

Hard cap per-thread buffer size. Prevents unbounded growth when a request/job logs a lot, or when tracking never gets a chance to flush (e.g. code running outside a request lifecycle).

500

Instance Method Summary collapse

Constructor Details

#initializeLogger

Returns a new instance of Logger.



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

def initialize
  @thread_logs_key = :dead_bro_logs
  @thread_logs_dropped_key = :dead_bro_logs_dropped
end

Instance Method Details

#clearObject

Clear logs for the current thread



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

def clear
  Thread.current[@thread_logs_key] = []
  Thread.current[@thread_logs_dropped_key] = 0
end

#debug(message) ⇒ Object



31
32
33
# File 'lib/dead_bro/logger.rb', line 31

def debug(message)
  log(:debug, message)
end

#error(message) ⇒ Object



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

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

#fatal(message) ⇒ Object



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

def fatal(message)
  log(:fatal, message)
end

#info(message) ⇒ Object



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

def info(message)
  log(:info, message)
end

#logsObject

Get all logs for the current thread. If the buffer was capped, append a synthetic marker entry so downstream consumers know entries were dropped.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/dead_bro/logger.rb', line 53

def logs
  entries = Thread.current[@thread_logs_key] || []
  dropped = Thread.current[@thread_logs_dropped_key] || 0
  return entries if dropped.zero?

  entries + [{
    sev: "warn",
    msg: "[DeadBro::Logger] #{dropped} log entries dropped (buffer cap #{MAX_LOG_ENTRIES})",
    time: Time.now.utc.iso8601(3)
  }]
end

#warn(message) ⇒ Object



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

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