package
1.2.12-dev
Repository: https://github.com/h-bf/corlib.git
Documentation: pkg.go.dev

# README

Logger

This package provides wrapper for Zap logger compatible with our logger convention.
The most important feature is opentracing metadata injection to log records. If passed context contains span then all records will have trace_id & span_id fields.

How to use?

There is default global logger that writes all logs (configured with DEBUG level).

For each level there are three functions that allows to format log record in various ways:

  1. Simple logging
logger.Error(ctx, "hello", "world")
// output: 
// {"level":"error","ts":"2018-12-21T13:14:05.747+0300","message":"helloworld","trace_id":"1a93a60e17efa0bd","span_id":"7cbc711c34a1f44d"}
  1. Formatted logging
logger.Errorf(ctx, "hello: %v", "world")
// output:
// {"level":"error","ts":"2018-12-21T13:16:52.271+0300","message":"hello: world","trace_id":"1a93a60e17efa0bd","span_id":"7cbc711c34a1f44d"}
  1. Key-value logging
logger.ErrorKV(ctx, "hello world", "foo", "bar", "x", "y")
// output:
// {"level":"error","ts":"2018-12-21T13:18:02.536+0300","message":"hello world","foo":"bar","x":"y","trace_id":"1a93a60e17efa0bd","span_id":"7cbc711c34a1f44d"}

Set global logger

Default log level is ERROR, if you want you can use another default log level:

l := logger.New(zapcore.DebugLevel)
logger.SetLogger(l)

// ...

logger.Debug(ctx, "debug message")
logger.Warn(ctx, "warn message")

// output:
// {"level":"debug","ts":"2018-12-21T13:27:04.028+0300","message":"debug message","trace_id":"1a93a60e17efa0bd","span_id":"7cbc711c34a1f44d"}
// {"level":"warn","ts":"2018-12-21T13:27:04.028+0300","message":"warn message","trace_id":"1a93a60e17efa0bd","span_id":"7cbc711c34a1f44d"}

Both DEBUG & WARN messages will be written.

context.Context integration

You can inject another logger to Context and pass it to logger.Debug func and that logger will be used instead of default one.

l := logger.New(zapcore.DebugLevel)
ctx := logger.ToContext(context.Background(), l)

logger.Debug(ctx, "debug message")
logger.Warn(ctx, "warn message")
// output:
// {"level":"debug","ts":"2018-12-21T13:28:49.621+0300","message":"debug message""trace_id":"1a93a60e17efa0bd","span_id":"7cbc711c34a1f44d"}
// {"level":"warn","ts":"2018-12-21T13:28:49.621+0300","message":"warn message""trace_id":"1a93a60e17efa0bd","span_id":"7cbc711c34a1f44d"}

Add caller

If you want to add caller file & line to each log record you can add option to New func

l := logger.New(
    zapcore.DebugLevel,
    zap.AddCaller(),
    zap.AddCallerSkip(1), // <-- required too
)

ctx := logger.ToContext(context.Background(), l)

logger.Debug(ctx, "debug message")
logger.Warn(ctx, "warn message")

// output:
// {"level":"debug","ts":"2018-12-21T13:31:15.272+0300","caller":"example/main.go:25","message":"debug message","trace_id":"1a93a60e17efa0bd","span_id":"7cbc711c34a1f44d"}
// {"level":"warn","ts":"2018-12-21T13:31:15.272+0300","caller":"example/main.go:26","message":"warn message","trace_id":"1a93a60e17efa0bd","span_id":"7cbc711c34a1f44d"}

Add stacktrace

Stacktrace can be automatically added to records starting from some level.

l := logger.New(
    zapcore.DebugLevel,
    zap.AddStacktrace(zapcore.ErrorLevel),
)

ctx := logger.ToContext(context.Background(), l)

logger.Debug(ctx, "debug message")
logger.Warn(ctx, "warn message")
logger.Error(ctx, "error message")

// output: 
// {"level":"debug","ts":"2018-12-21T13:37:12.511+0300","message":"debug message","trace_id":"1a93a60e17efa0bd","span_id":"7cbc711c34a1f44d"}
// {"level":"warn","ts":"2018-12-21T13:37:12.511+0300","message":"warn message","trace_id":"1a93a60e17efa0bd","span_id":"7cbc711c34a1f44d"}
// {"level":"error","ts":"2018-12-21T13:37:12.511+0300","message":"error message","stacktrace":"---stacktrace--here---","trace_id":"1a93a60e17efa0bd","span_id":"7cbc711c34a1f44d"}

So only records will level ERROR and lower will have stacktrace.

# Functions

Debug ...
Debugf ...
DebugKV ...
Error ...
Errorf ...
ErrorKV ...
Fatal ...
Fatalf ...
FatalKV ...
FromContext returns logger from context if set.
Global returns current global logger.
Info ...
Infof ...
InfoKV ...
IsLevelEnabled is log level enabled.
Level returns current global logger level.
New creates new logger with standard EncoderConfig if lvl == nil, global AtomicLevel will be used.
NewWithSink ...
Panic ...
Panicf ...
PanicKV ...
SetLevel sets level for global logger.
SetLogger sets global used logger.
ToContext returns new context with specified sugared logger inside.
Warn ...
Warnf ...
WarnKV ...
WithLevel returns `zap.Option` that can be used to create a new logger from an existing one with a new logging level Usage: logger.Logger().Desugar().WithOptions(logger.WithLevel(level)).Sugar().

# Constants

LoggerLevelDEBUG -.
LoggerLevelERROR -.
LoggerLevelFATAL -.
LoggerLevelINFO -.
LoggerLevelWARN -.

# Structs

No description provided by the author

# Type aliases

No description provided by the author
LoggerLevelConf points on a logger level in config.
No description provided by the author