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. Writes are unbuffered by default; buffering is opt-in.
Index ¶
- func AsOptions(ctors ...any) fx.Option
- func Module() fx.Option
- type BufferConfig
- type CallerConfig
- type Config
- type Logger
- type Option
- func WithBuffer(size int, flushInterval time.Duration) Option
- func WithCaller(enabled bool) Option
- func WithFields(attrs ...slog.Attr) Option
- func WithLevel(level string) Option
- func WithStacktraceLevel(level slog.Level) Option
- func WithWriteSyncer(ws zapcore.WriteSyncer) Option
- func WithoutBuffer() Option
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.
func Module ¶
Module installs one application logger as slog.Default, provides *Logger and *slog.Logger, routes Fx events through it, and closes it on application shutdown.
The logger is constructed before any application component: fx.WithLogger and the eager fx.Invoke below both 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.
Types ¶
type BufferConfig ¶
type BufferConfig struct {
Enabled bool `env:"ENABLED"`
Size int `env:"SIZE"`
FlushInterval time.Duration `env:"FLUSH_INTERVAL"`
}
BufferConfig controls optional write buffering. Buffering is disabled by default. Size and FlushInterval use zap defaults when left at zero.
type CallerConfig ¶
type CallerConfig struct {
Enabled *bool `env:"ENABLED"`
}
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 string `env:"LEVEL"`
Caller CallerConfig `envPrefix:"CALLER_"`
Buffer BufferConfig `envPrefix:"BUFFER_"`
}
Config is the serializable logger configuration.
The tags are deliberately passive: zslogfx does not read the environment. Applications may fill Config with confmaker/confx, another configuration library, or plain Go code.
type Logger ¶
Logger owns the slog view of the logging core and the resources behind it. zap is only the backend; the logger a service uses is a plain *slog.Logger.
Close is safe to call more than once. Fx applications normally do not need to call it directly; Module registers it with the application lifecycle.
type Option ¶
type Option func(*settings)
Option customizes logger construction after Config has been applied.
func WithBuffer ¶
WithBuffer enables the built-in buffer. Zero values select the defaults: 4 MiB and a one-second flush interval.
func WithFields ¶
WithFields adds slog attributes to every record.
func WithStacktraceLevel ¶
WithStacktraceLevel adds a stack trace to records at or above level.
func WithWriteSyncer ¶
func WithWriteSyncer(ws zapcore.WriteSyncer) Option
WithWriteSyncer replaces stdout as the destination. A nil syncer is ignored (stdout stays the default). It does not enable the built-in buffer; wrap the destination yourself when custom buffering is required.
func WithoutBuffer ¶
func WithoutBuffer() Option
WithoutBuffer disables buffering even when Config enables it.