Version: v0.0.0-...-a2d18c2 Latest Latest

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

Go to latest
Published: Dec 4, 2022 License: Apache-2.0 Imports: 5 Imported by: 5



This is a highly opinionated wrapper around the zap logger.


Create a new logger near the entrypoint of your code.

logger, err := logger.New(ctx, false) // Without debug

Make sure to call the Clean function when you're done with the usage.

defer logger.Clean()

You can now add this logger to a context.

ctx = NewContextWithLogger(ctx, logger)

If you pass this context correctly to the call site, you can fetch the logger from the context and use its functions.

logger = NewLoggerFromContext(ctx)
logger.Info("This is an info message")

One of the primary reasons for creating this package is to easily append fields to a logger for additional debugging.

You can set global fields to the logger.

logger = logger.WithField("name", name) // All subsequent calls to this logger will log the name field.

You can also set local fields only to the log message.

logger.WithField("name", name).Info("Login user")

The logger also supports multiple fields.

		"test-key", "test-value",
		"test-other-key", 1,
	)).Info("This is an info message with multiple field")



Package logger is a highly opinionated wrapper around the Zap logger



This section is empty.


This section is empty.


func Fields

func Fields(a ...any) (fields []zapcore.Field)

Fields accepts the values as a string:interface pair. If the pairs don't match, they will be skipped

func NewContextWithLogger

func NewContextWithLogger(parentCtx context.Context, logger *Logger) context.Context

NewContextWithLogger returns a new context with a logger and panics if it doesn't match the interface.


type Field

type Field struct {
	Key   string
	Value interface{}

Field represents a logger field.

type Logger

type Logger struct {
	// contains filtered or unexported fields

Logger wraps zap.Logger.

func LoggerFromContext

func LoggerFromContext(ctx context.Context) *Logger

LoggerFromContext retrieves a logger from a context and panics if there isn't one.

func New

func New(ctx context.Context, debug bool) (*Logger, error)

New creates a new logger. Make sure to call defer logger.Clean() after calling this. Log messages are JSON Encoded and timestamps are RFC3339 encoded.

func (*Logger) Clean

func (l *Logger) Clean()

Clean cleans up the log states. Make sure to call this after creating a new logger.

func (*Logger) Debug

func (l *Logger) Debug(msg string)

Debug logs a Debug level message.

func (*Logger) Error

func (l *Logger) Error(msg string)

Error logs a Error level message.

func (*Logger) Fatal

func (l *Logger) Fatal(msg string)

Fatal logs a Fatal message.

func (*Logger) Info

func (l *Logger) Info(msg string)

Info logs a Info level message.

func (*Logger) Middleware

func (logger *Logger) Middleware() func(http.Handler) http.Handler

Middleware is a middleware that adds the logger to the request context.

func (*Logger) Warn

func (l *Logger) Warn(msg string)

Warn logs a Warning level message.

func (*Logger) WithError

func (l *Logger) WithError(err error) *Logger

WithError returns a new logger with the provided error.

func (*Logger) WithField

func (l *Logger) WithField(key string, val interface{}) *Logger

WithField returns a new logger with the provided field.

func (*Logger) WithFields

func (l *Logger) WithFields(f []zapcore.Field) *Logger

WithFields returns a logger with the providedfields.

type Options

type Options struct {

Options is the logger options.

Jump to

Keyboard shortcuts

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