Class: CloudflareAccessGate::StructuredLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudflare_access_gate/structured_logger.rb

Overview

Thin adapter that lets this gem emit structured key/value log payloads without depending on any particular logging library.

SemanticLogger accepts a payload natively (logger.warn('msg', key: 'v')); the stdlib Logger, Rails.logger, lograge, and friends do not. This wrapper detects a SemanticLogger target and passes the payload through, and otherwise appends a key=value suffix to the message so no log data is lost.

Wrap any logger-ish object: it only needs to respond to the standard debug/info/warn/error severity methods.

Constant Summary collapse

LEVELS =
%i[debug info warn error].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ StructuredLogger

Returns a new instance of StructuredLogger.



27
28
29
# File 'lib/cloudflare_access_gate/structured_logger.rb', line 27

def initialize(target)
  @target = target
end

Instance Attribute Details

#targetObject (readonly)

Returns the value of attribute target.



20
21
22
# File 'lib/cloudflare_access_gate/structured_logger.rb', line 20

def target
  @target
end

Class Method Details

.wrap(logger) ⇒ Object

Wraps logger unless it is already wrapped.



23
24
25
# File 'lib/cloudflare_access_gate/structured_logger.rb', line 23

def self.wrap(logger)
  logger.is_a?(self) ? logger : new(logger)
end

Instance Method Details

#tagged(**tags, &block) ⇒ Object

Attaches tags to every log line emitted inside the block.

Only SemanticLogger supports this; for every other logger the block is simply yielded (the same key/values are still emitted in the payload of the log line itself, so nothing is silently dropped).



42
43
44
45
46
# File 'lib/cloudflare_access_gate/structured_logger.rb', line 42

def tagged(**tags, &block)
  return block.call if tags.empty? || !structured?

  target.tagged(**tags, &block)
end