Documentation
¶
Overview ¶
Package zapctxd implements contextualized logger with go.uber.org/zap.
Index ¶
- type Config
- type Logger
- func (l *Logger) CtxdLogger() ctxd.Logger
- func (l *Logger) Debug(ctx context.Context, msg string, keysAndValues ...any)
- func (l *Logger) Error(ctx context.Context, msg string, keysAndValues ...any)
- func (l *Logger) Important(ctx context.Context, msg string, keysAndValues ...any)
- func (l *Logger) Info(ctx context.Context, msg string, keysAndValues ...any)
- func (l *Logger) SetLevelEnabler(enabler zapcore.LevelEnabler)
- func (l *Logger) SkipCaller() *Logger
- func (l *Logger) Warn(ctx context.Context, msg string, keysAndValues ...any)
- func (l *Logger) ZapLogger() *zap.Logger
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
Level zapcore.Level `split_words:"true" default:"error"`
DevMode bool `split_words:"true"`
FieldNames ctxd.FieldNames `split_words:"true"`
Output io.Writer
ZapOptions []zap.Option
// ColoredOutput enables colored output in development mode.
ColoredOutput bool
// StripTime disables time variance in logger.
StripTime bool
}
Config is log configuration.
type Logger ¶
type Logger struct {
// Deprecated: Use SetLevelEnabler instead.
AtomicLevel zap.AtomicLevel
// contains filtered or unexported fields
}
Logger is a contextualized zap logger.
func New ¶
New creates contextualized logger with zap backend.
Example ¶
package main
import (
"context"
"github.com/bool64/ctxd"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/bool64/zapctxd"
)
func main() {
logger := zapctxd.New(zapctxd.Config{
Level: zap.WarnLevel,
DevMode: true,
StripTime: true,
})
ctx := ctxd.AddFields(context.Background(),
"foo", "bar",
)
logger.Info(ctx, "not logged due to WARN level config")
logger.Error(ctx, "something failed",
"baz", 1,
"quux", 2.2,
)
logger.Important(ctx, "logged because is important")
logger.Info(ctxd.WithDebug(ctx), "logged because of forced DEBUG mode")
logger.SetLevelEnabler(zapcore.DebugLevel)
logger.Info(ctx, "logged because logger level was changed to DEBUG")
}
Output: <stripped> ERROR zapctxd/example_test.go:26 something failed {"baz": 1, "quux": 2.2, "foo": "bar"} <stripped> INFO zapctxd/example_test.go:31 logged because is important {"foo": "bar"} <stripped> INFO zapctxd/example_test.go:32 logged because of forced DEBUG mode {"foo": "bar"} <stripped> INFO zapctxd/example_test.go:35 logged because logger level was changed to DEBUG {"foo": "bar"}
func WrapZapLoggers ¶ added in v1.2.0
func WrapZapLoggers(sugared, debug *zap.Logger, encoder zapcore.Encoder, options ...zap.Option) *Logger
WrapZapLoggers creates contextualized logger with provided zap loggers.
func (*Logger) CtxdLogger ¶
CtxdLogger provides contextualized logger.
func (*Logger) SetLevelEnabler ¶ added in v1.2.0
func (l *Logger) SetLevelEnabler(enabler zapcore.LevelEnabler)
SetLevelEnabler sets level enabler.
func (*Logger) SkipCaller ¶
SkipCaller adapts logger for wrapping by increasing skip caller counter.
Click to show internal directories.
Click to hide internal directories.