zeroslog

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2023 License: MIT Imports: 11 Imported by: 1

README

zeroslog

Go Reference license Build codecov

A zerolog handler for slog

Example

package main

import (
	"log/slog"
	"github.com/phsym/zeroslog"
)

func main() {
	logger := slog.New(
        zeroslog.NewJsonHandler(os.Stderr, &zeroslog.HandlerOptions{Level: slog.LevelDebug}),
    )
	slog.SetDefault(logger)
    slog.Info("Hello world!", "foo", "bar")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler added in v0.0.2

type Handler struct {
	// contains filtered or unexported fields
}

Handler is an slog.Handler implementation that uses zerolog to process slog.Record.

func NewConsoleHandler added in v0.0.2

func NewConsoleHandler(out io.Writer, opts *HandlerOptions) *Handler

NewConsoleHandler creates a new zerolog handler, wrapping out into a zerolog.ConsoleWriter. It's a shortcut to calling

NewHandler(zerolog.New(&zerolog.ConsoleWriter{Out: out, TimeFormat: time.DateTime}).Level(zerolog.InfoLevel), opts)

func NewHandler added in v0.0.2

func NewHandler(logger zerolog.Logger, opts *HandlerOptions) *Handler

NewHandler creates a *ZerologHandler implementing slog.Handler. It wraps a zerolog.Logger to which log records will be sent.

Unlesse opts.Level is not nil, the logger level is used to filter out records, otherwise opts.Level is used.

The provided logger instance must be configured to not send timestamps or caller information.

If opts is nil, it assumes default options values.

func NewJsonHandler added in v0.0.2

func NewJsonHandler(out io.Writer, opts *HandlerOptions) *Handler

NewJsonHandler is a shortcut to calling

NewHandler(zerolog.New(out).Level(zerolog.InfoLevel), opts)

func (*Handler) Enabled added in v0.0.2

func (h *Handler) Enabled(_ context.Context, lvl slog.Level) bool

Enabled implements slog.Handler.

func (*Handler) Handle added in v0.0.2

func (h *Handler) Handle(_ context.Context, rec slog.Record) error

Handle implements slog.Handler.

func (*Handler) WithAttrs added in v0.0.2

func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs implements slog.Handler.

func (*Handler) WithGroup added in v0.0.2

func (h *Handler) WithGroup(name string) slog.Handler

WithGroup implements slog.Handler.

type HandlerOptions

type HandlerOptions struct {
	// AddSource causes the handler to compute the source code position
	// of the log statement and add a SourceKey attribute to the output.
	AddSource bool

	// Level reports the minimum record level that will be logged.
	// The handler discards records with lower levels.
	// If Level is nil, the handler assumes the level set in the logger.
	// The handler calls Level.Level if it's not nil for each record processed;
	// to adjust the minimum level dynamically, use a LevelVar.
	Level slog.Leveler
}

HandlerOptions are options for a ZerologHandler. A zero HandlerOptions consists entirely of default values.

Jump to

Keyboard shortcuts

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