logger

package
v2.16.3 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2024 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package logger provides helper functions that can be used in combination with slog to increase functionality or make working with slog easier.

1. Logging in unit tests

To log in unit tests you can create a new slog logger that uses logger.testWriter as its writer. This can be constructed by creating a logger like this: `logger.NewTest(t)`.

2. Creating a new logger with an increased log level based on another logger

You can create a new logger with a new log level by creating a new slog.Logger with the LevelHandler in this package and passing the handler of the other logger. As an example, if you have a slog.Logger named `log` you can create a new logger with an increased log level (here slog.LevelWarn) like this:

slog.New(logger.NewLevelHandler(slog.LevelWarn, log.Handler()))

Index

Constants

View Source
const CmdLineVerbosityDescription = "log verbosity: Use -1 for debug information, 0 for info, 1 for warn, 2 for error"

CmdLineVerbosityDescription explains numeric log levels.

Variables

This section is empty.

Functions

func GetClientStreamInterceptor added in v2.16.0

func GetClientStreamInterceptor(l *slog.Logger) grpc.DialOption

GetClientStreamInterceptor returns a gRPC client option for intercepting stream gRPC logs.

func GetClientUnaryInterceptor added in v2.16.0

func GetClientUnaryInterceptor(l *slog.Logger) grpc.DialOption

GetClientUnaryInterceptor returns a gRPC client option for intercepting unary gRPC logs.

func GetServerStreamInterceptor added in v2.16.0

func GetServerStreamInterceptor(l *slog.Logger) grpc.ServerOption

GetServerStreamInterceptor returns a gRPC server option for intercepting streaming gRPC logs.

func GetServerUnaryInterceptor added in v2.16.0

func GetServerUnaryInterceptor(l *slog.Logger) grpc.ServerOption

GetServerUnaryInterceptor returns a gRPC server option for intercepting unary gRPC logs.

func NewJSONLogger added in v2.16.0

func NewJSONLogger(level slog.Level) *slog.Logger

NewJSONLogger creates a new slog.Logger that writes JSON formatted log messages to os.Stderr.

func NewTest

func NewTest(t *testing.T) *slog.Logger

NewTest creates a new slog.Logger that writes to a testing.T.

func NewTextLogger added in v2.16.0

func NewTextLogger(level slog.Level) *slog.Logger

NewTextLogger creates a new slog.Logger that writes text formatted log messages to os.Stderr.

func ReplaceGRPCLogger added in v2.16.0

func ReplaceGRPCLogger(l *slog.Logger)

ReplaceGRPCLogger replaces grpc's internal logger with the given logger.

func VerbosityFromInt

func VerbosityFromInt(verbosity int) slog.Level

VerbosityFromInt converts a verbosity level from an integer to a slog.Level.

Types

type LevelHandler added in v2.16.0

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

LevelHandler wraps a Handler with an Enabled method that returns false for levels below a minimum.

func NewLevelHandler added in v2.16.0

func NewLevelHandler(level slog.Leveler, h slog.Handler) *LevelHandler

NewLevelHandler returns a LevelHandler with the given level. All methods except Enabled delegate to h.

func (*LevelHandler) Enabled added in v2.16.0

func (h *LevelHandler) Enabled(_ context.Context, level slog.Level) bool

Enabled implements Handler.Enabled by reporting whether level is at least as large as h's level.

func (*LevelHandler) Handle added in v2.16.0

func (h *LevelHandler) Handle(ctx context.Context, r slog.Record) error

Handle implements Handler.Handle.

func (*LevelHandler) Handler added in v2.16.0

func (h *LevelHandler) Handler() slog.Handler

Handler returns the Handler wrapped by h.

func (*LevelHandler) WithAttrs added in v2.16.0

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

WithAttrs implements Handler.WithAttrs.

func (*LevelHandler) WithGroup added in v2.16.0

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

WithGroup implements Handler.WithGroup.

Jump to

Keyboard shortcuts

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