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 GRPCLogger(l *slog.Logger) *slog.Logger
- 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
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 GRPCLogger ¶
GRPCLogger returns a logger at warn level for gRPC logging.
func GetClientStreamInterceptor ¶
func GetClientStreamInterceptor(l *slog.Logger) grpc.DialOption
GetClientStreamInterceptor returns a gRPC client option for intercepting stream gRPC logs.
func GetClientUnaryInterceptor ¶
func GetClientUnaryInterceptor(l *slog.Logger) grpc.DialOption
GetClientUnaryInterceptor returns a gRPC client option for intercepting unary gRPC logs.
func GetServerStreamInterceptor ¶
func GetServerStreamInterceptor(l *slog.Logger) grpc.ServerOption
GetServerStreamInterceptor returns a gRPC server option for intercepting streaming gRPC logs.
func GetServerUnaryInterceptor ¶
func GetServerUnaryInterceptor(l *slog.Logger) grpc.ServerOption
GetServerUnaryInterceptor returns a gRPC server option for intercepting unary gRPC logs.
func NewJSONLogger ¶
NewJSONLogger creates a new slog.Logger that writes JSON formatted log messages to os.Stderr.
func NewTextLogger ¶
NewTextLogger creates a new slog.Logger that writes text formatted log messages to os.Stderr.
func ReplaceGRPCLogger ¶
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 ¶
This section is empty.