logging

package
v0.7.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2026 License: Apache-2.0 Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

func Debugging(ctx context.Context) bool

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

func From(ctx context.Context) *slog.Logger

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

func New(w io.Writer, level slog.Level, asJSON bool) slog.Handler

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.

func With

func With(ctx context.Context, l *slog.Logger) context.Context

With returns a context carrying l, for From to find downstream.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL