Documentation ¶
Overview ¶
Package log provides simple functions to build an application logger based on the logr.Logger interface and the zap logging library.
Use New to build the logger and Sync to flush buffered logs, e.g.;
logger := logr.New(os.Stderr) defer log.Sync(logger) logger.Info("Hello!", "count", 10)
New accepts multiple functional options, e.g. use WithLevel to specify the verbosity of the logger:
logger := logr.New(os.Stderr, log.WithLevel(10))
Index ¶
- func New(w io.Writer, opts ...option) logr.Logger
- func Sync(logger logr.Logger)
- func Underlying(logger logr.Logger) (*zap.Logger, bool)
- func WithClock(clock zapcore.Clock) option
- func WithDebug(enabled bool) option
- func WithLevel(level int) option
- func WithName(name string) option
- func WithStacktrace(enabled bool) option
- func WithVerbosity() option
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func New ¶
New returns a new logger based on the logr interface and the zap logging library.
Example ¶
package main import ( "os" "go.artefactual.dev/tools/log" ) func main() { logger := log.New(os.Stderr) defer log.Sync(logger) logger.Info("Test", "count", 1) }
Output:
func Underlying ¶
Underlying returns the zap logger used as a logr sink.
func WithClock ¶ added in v0.3.0
WithClock configures the clock used by the logger to determine the current time for logged entries. Defaults to the system clock with time.Now.
Example ¶
package main import ( "os" "time" "go.artefactual.dev/tools/log" ) func main() { logger := log.New(os.Stdout, log.WithName("example"), log.WithLevel(2), log.WithClock(&fakeClock{}), ) defer log.Sync(logger) logger.Info("Test.", "key", "val") logger.V(4).Info("This should be ignored as per the level configured.") } type fakeClock struct{} func (c *fakeClock) Now() time.Time { return time.Unix(626572800, 0) } // NewTicker returns a time.Ticker that ticks at the specified frequency. func (c *fakeClock) NewTicker(d time.Duration) *time.Ticker { return nil }
Output: {"level":"0","ts":626572800,"logger":"example","caller":"log/log_example_test.go:25","msg":"Test.","key":"val"}
func WithDebug ¶
func WithDebug(enabled bool) option
WithDebug configures the logger for development environments.
func WithLevel ¶
func WithLevel(level int) option
WithLevel defines the verbosity of the logger (0 is the least verbose).
func WithStacktrace ¶ added in v0.4.0
func WithStacktrace(enabled bool) option
WithStacktrace configures the logger to record error stacktraces. It is enabled by default.
Types ¶
This section is empty.