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 ...interface{})
- func (l *Logger) Error(ctx context.Context, msg string, keysAndValues ...interface{})
- func (l *Logger) Important(ctx context.Context, msg string, keysAndValues ...interface{})
- func (l *Logger) Info(ctx context.Context, msg string, keysAndValues ...interface{})
- func (l *Logger) SkipCaller() *Logger
- func (l *Logger) Warn(ctx context.Context, msg string, keysAndValues ...interface{})
- 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 { 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" "github.com/bool64/zapctxd" "go.uber.org/zap" ) 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.AtomicLevel.SetLevel(zap.DebugLevel) logger.Info(ctx, "logged because logger level was changed to DEBUG") }
Output: <stripped> ERROR zapctxd/example_test.go:24 something failed {"baz": 1, "quux": 2.2, "foo": "bar"} <stripped> INFO zapctxd/example_test.go:29 logged because is important {"foo": "bar"} <stripped> INFO zapctxd/example_test.go:30 logged because of forced DEBUG mode {"foo": "bar"} <stripped> INFO zapctxd/example_test.go:33 logged because logger level was changed to DEBUG {"foo": "bar"}
func (*Logger) CtxdLogger ¶
CtxdLogger provides contextualized logger.
func (*Logger) SkipCaller ¶
SkipCaller adapts logger for wrapping by increasing skip caller counter.
Click to show internal directories.
Click to hide internal directories.