zapctxd

package module
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 7, 2024 License: MIT Imports: 8 Imported by: 3

README

Contextualized Logging with Zap

This library implements contextualized logger with zap.

Build Status Coverage Status GoDevDoc time tracker Code lines Comments

Example

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:23	something failed	{"baz": 1, "quux": 2.2, "foo": "bar"}
// <stripped>	INFO	zapctxd/example_test.go:28	logged because is important	{"foo": "bar"}
// <stripped>	INFO	zapctxd/example_test.go:29	logged because of forced DEBUG mode	{"foo": "bar"}
// <stripped>	INFO	zapctxd/example_test.go:32	logged because logger level was changed to DEBUG	{"foo": "bar"} 

See Also

Documentation

Overview

Package zapctxd implements contextualized logger with go.uber.org/zap.

Index

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

func New(cfg Config, options ...zap.Option) *Logger

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

func (l *Logger) CtxdLogger() ctxd.Logger

CtxdLogger provides contextualized logger.

func (*Logger) Debug

func (l *Logger) Debug(ctx context.Context, msg string, keysAndValues ...interface{})

Debug implements ctxd.Logger.

func (*Logger) Error

func (l *Logger) Error(ctx context.Context, msg string, keysAndValues ...interface{})

Error implements ctxd.Logger.

func (*Logger) Important

func (l *Logger) Important(ctx context.Context, msg string, keysAndValues ...interface{})

Important implements ctxd.Logger.

func (*Logger) Info

func (l *Logger) Info(ctx context.Context, msg string, keysAndValues ...interface{})

Info implements ctxd.Logger.

func (*Logger) SkipCaller

func (l *Logger) SkipCaller() *Logger

SkipCaller adapts logger for wrapping by increasing skip caller counter.

func (*Logger) Warn

func (l *Logger) Warn(ctx context.Context, msg string, keysAndValues ...interface{})

Warn implements ctxd.Logger.

func (*Logger) ZapLogger added in v0.1.1

func (l *Logger) ZapLogger() *zap.Logger

ZapLogger returns *zap.Logger that used in Logger.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL