Documentation
¶
Overview ¶
Package zslogfx builds a standard log/slog logger backed by zap and wires its ownership into an Uber Fx application.
Services use a plain *slog.Logger; zap is only the fast JSON backend, through Uber's official go.uber.org/zap/exp/zapslog handler. The output is one fixed JSON format, ECS-aligned for OpenSearch. Module installs the logger as slog.Default so any code can log without receiving it as an argument, and also provides it for explicit injection. The library adds no write buffer: every logging call hands the record to the destination synchronously.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AsOptions ¶
AsOptions provides Option constructors to Module through an Fx value group. Constructors may depend on any other values in the Fx graph.
A value group has no order, so Options from separate AsOptions and SupplyOptions calls are applied in an undefined one: an application must not contribute two Options that set the same thing, such as two WithLevel or two WithWriteSyncer.
func Module ¶
Module installs one application logger as slog.Default, provides *Logger and *slog.Logger, routes Fx events through it, and on application shutdown syncs it and restores the previous slog.Default. Fx events are logged at debug level, the failures among them at error level, in the same JSON as the rest.
The logger is constructed before any application component: both the Fx event logger and an eager fx.Invoke depend on it, so slog.Default is installed during fx.New - before OnStart hooks and application code run. This makes the logging sequence deterministic: no component logs before the logger is ready.
Module must be passed at the fx.New root because fx.WithLogger is scoped to the scope it is declared in. It belongs to one application at a time: two applications alive at once take slog.Default from each other, and an application that replaces the Fx event logger keeps slog.Default installed after a failed provide.
func SupplyOptions ¶ added in v0.4.0
SupplyOptions passes Options that need nothing from the Fx graph to Module. A plain fx.Supply does not reach it: Module reads the value group AsOptions and SupplyOptions contribute to. The Options of one call are applied in the order given; see AsOptions about the order between calls.
Types ¶
type CallerConfig ¶
type CallerConfig struct {
Enabled *bool `env:"ENABLED" envDescription:"Include caller annotations; when unset, enabled at debug level and disabled otherwise."`
}
CallerConfig controls caller annotations. Unless Enabled is explicitly set, caller annotations are enabled at debug level and disabled otherwise.
type Config ¶
type Config struct {
// Level is debug, info, warn (or warning) or error; empty means info.
Level string `env:"LEVEL" envDescription:"Log level: debug, info, warn, warning or error; unset or empty uses info."`
Caller CallerConfig `envPrefix:"CALLER_"`
}
Config is the serializable logger configuration.
The tags are deliberately passive: zslogfx does not read the environment. Applications may fill Config with github.com/uchaloop/confmaker, another configuration library, or plain Go code. Register with the explicit instance name "log" to read LOG_*.
type Logger ¶
Logger wraps the *slog.Logger a service uses and the sync of the destination behind it. zap is only the backend.
type Option ¶
type Option func(*settings)
Option customizes logger construction after Config has been applied.
func WithFields ¶
WithFields adds slog attributes to every record. Options accumulate, so several calls add up.
func WithLevel ¶
WithLevel overrides Config.Level. An empty level, like an empty Config.Level, means info.
func WithStacktraceLevel ¶
WithStacktraceLevel writes error.stack_trace on records at or above level. Without it no stack trace is written, and none is collected.
func WithWriteSyncer ¶
func WithWriteSyncer(ws zapcore.WriteSyncer) Option
WithWriteSyncer replaces stdout as the destination. A nil syncer is ignored (stdout stays the default). The logger serializes writes to it, so ws does not have to be safe for concurrent use.