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
- func GetClientStreamInterceptor(l *slog.Logger) grpc.DialOption
- func GetClientUnaryInterceptor(l *slog.Logger) grpc.DialOption
- func GetServerStreamInterceptor(l *slog.Logger) grpc.ServerOption
- func GetServerUnaryInterceptor(l *slog.Logger) grpc.ServerOption
- func NewJSONLogger(level slog.Level) *slog.Logger
- func NewTest(t *testing.T) *slog.Logger
- func NewTextLogger(level slog.Level) *slog.Logger
- func ReplaceGRPCLogger(l *slog.Logger)
- func VerbosityFromInt(verbosity int) slog.Level
- type LevelHandler
- func (h *LevelHandler) Enabled(_ context.Context, level slog.Level) bool
- func (h *LevelHandler) Handle(ctx context.Context, r slog.Record) error
- func (h *LevelHandler) Handler() slog.Handler
- func (h *LevelHandler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (h *LevelHandler) WithGroup(name string) slog.Handler
Constants ¶
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
NewJSONLogger creates a new slog.Logger that writes JSON formatted log messages to os.Stderr.
func NewTextLogger ¶ added in v2.16.0
NewTextLogger creates a new slog.Logger that writes text formatted log messages to os.Stderr.
func ReplaceGRPCLogger ¶ added in v2.16.0
ReplaceGRPCLogger replaces grpc's internal logger with the given logger.
func VerbosityFromInt ¶
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
Enabled implements Handler.Enabled by reporting whether level is at least as large as h's level.
func (*LevelHandler) Handler ¶ added in v2.16.0
func (h *LevelHandler) Handler() slog.Handler
Handler returns the Handler wrapped by h.