Class: Shojiku::Log

Inherits:
Object
  • Object
show all
Defined in:
lib/shojiku/log.rb

Overview

The optional host-side log channel.

Silent unless an application supplies a logger, and deliberately narrow: it reports what the BINDING did — which library it loaded, which ABI revision it found, which lifecycle step ran and for how long — and never what the document contained. Params, rendered bytes, diagnostics and key material are all outside this channel BY RULE, because a log line is the easiest way for a secret to leave a process, and because a diagnostic belongs to the Result the caller already has.

What does cross is bounded first (Echo), so a hostile template name cannot smuggle control characters into a log file.

Any object answering debug is accepted — Logger, Rails.logger, or an application's own — so the gem's runtime dependency list stays at exactly one entry. The cross-language rule the other six mirror: each SDK accepts its ecosystem's standard logger interface, optionally.

Instance Method Summary collapse

Constructor Details

#initialize(logger = nil) ⇒ Log

Returns a new instance of Log.



22
23
24
# File 'lib/shojiku/log.rb', line 22

def initialize(logger = nil)
  @logger = logger
end

Instance Method Details

#event(name, **fields) ⇒ Object

Records one host event. The message is built only when someone is listening: a silent log costs a nil check, not string formatting.



28
29
30
31
32
# File 'lib/shojiku/log.rb', line 28

def event(name, **fields)
  return unless @logger

  @logger.debug("shojiku #{name}#{render(fields)}")
end

#timed(name, **fields) ⇒ Object

Times one lifecycle operation and returns what the block returned.

The block is expected to produce a Result, whose verdict is recorded as ok — the one thing worth knowing about an operation that is not its content.



39
40
41
42
43
44
# File 'lib/shojiku/log.rb', line 39

def timed(name, **fields)
  started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  result = yield
  event(name, **fields, ms: elapsed_ms(started), ok: result.success?)
  result
end