Documentation
¶
Overview ¶
Package logging is context-guru's logging plumbing: the level/format/sink decision, the credential scrubber that sits in front of every handler, and the per-request logger that carries tenant and session onto the lines of one request's lifecycle — including the ones written after the response, off the request path (see With, and the boundary named there).
It is deliberately small, and deliberately built on log/slog with no logging dependency added. Three shapes have to work:
- A LOCAL proxy, run by hand: useful text on stderr, no configuration, no container, no Loki. This is the zero-value behaviour.
- The HOSTED deployment: the same records as JSON in a file, which promtail tails into Loki. Shipping is a file path and nothing more — no HTTP client here, no batching, no retry queue, because promtail already is one, and a log shipper inside the process being debugged is a way to lose logs.
- Somebody who wants NONE of ours: CG_LOG_PLAIN=1 swaps in the stdlib's own slog text handler and disables the file sink.
Environment (all optional):
CG_LOG_LEVEL=debug|info|warn|error default info CG_LOG_FORMAT=text|json default text, or json when CG_LOG_FILE is set CG_LOG_FILE=/path/to/proxy.jsonl ALSO write there, in JSON, for promtail → Loki CG_LOG_PLAIN=1 plain stdlib slog, no scrubbing, no file CONTEXT_GURU_DEBUG=1 legacy alias for CG_LOG_LEVEL=debug
Level vocabulary, so the levels mean something across the codebase:
ERROR we failed, or we did something that is a bug in this program. WARN we degraded but kept serving: fell open, reverted, refused, evicted. INFO the request lifecycle — ONE line per request — and startup facts. DEBUG per-component decisions: which gate declined, with what numbers.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Debugging ¶
Debugging reports whether DEBUG records are being emitted, for guarding a payload that costs something to build. The rule in this codebase: if the arguments to a DEBUG call are more than field reads, guard them with this.
func From ¶
From returns the request's logger, or the process default when there is none — so a library caller who never set one up still gets working logs, and no call site needs a nil check.
func New ¶
New builds a scrubbing handler over w. Exported for tests and for a host that wants the scrubber without the environment.
func Setup ¶
func Setup() string
Setup installs the process-wide slog default from the environment and returns a one-line description of what it chose, so the caller can log it (the choice of sink is the first thing you want to know when the logs are not where you looked).
It never fails the process: a log file that cannot be opened degrades to stderr-only with a WARN, because refusing to boot over an observability sink would make logging less safe than not having it.
Types ¶
This section is empty.