Class: Stipa::Logger

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

Overview

Structured, leveled logger that writes one logfmt line per event.

Format (parseable by Splunk, Datadog, Loki, grep):

time=2026-03-18T12:00:00.123Z level=INFO req_id=a1b2c3d4 method=GET
path=/users status=200 bytes_in=0 bytes_out=412

Thread-safe via Monitor (reentrant mutex — safe when a log call triggers another log call from a rescue block in the same thread).

Constant Summary collapse

LEVELS =
{ debug: 0, info: 1, warn: 2, error: 3 }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(output: $stdout, level: :info) ⇒ Logger

Returns a new instance of Logger.



15
16
17
18
19
# File 'lib/stipa/logger.rb', line 15

def initialize(output: $stdout, level: :info)
  @output = output
  @level  = LEVELS.fetch(level, 1)
  @lock   = Monitor.new
end

Instance Method Details

#debug(msg, **fields) ⇒ Object



39
# File 'lib/stipa/logger.rb', line 39

def debug(msg, **fields); log(:debug, msg, **fields); end

#error(msg, **fields) ⇒ Object



38
# File 'lib/stipa/logger.rb', line 38

def error(msg, **fields); log(:error, msg, **fields); end

#info(req: nil, res: nil, bytes_in: 0, bytes_out: 0, **extra) ⇒ Object

Log a completed request/response cycle. Called by Connection.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/stipa/logger.rb', line 22

def info(req: nil, res: nil, bytes_in: 0, bytes_out: 0, **extra)
  return if @level > LEVELS[:info]
  fields = {
    time:      utc_now,
    level:     'INFO',
    req_id:    req&.id    || '-',
    method:    req&.method || '-',
    path:      req&.path   || '-',
    status:    res&.status || '-',
    bytes_in:  bytes_in,
    bytes_out: bytes_out,
  }.merge(extra)
  write(logfmt(fields))
end

#warn(msg, **fields) ⇒ Object



37
# File 'lib/stipa/logger.rb', line 37

def warn(msg, **fields);  log(:warn,  msg, **fields); end