Documentation ¶
Overview ¶
Package log provides sane default loggers using slog.
Index ¶
- Constants
- func JSONHandler(w io.Writer, opts *slog.HandlerOptions) slog.Handler
- func NewDefaultEnvLogger() *slog.Logger
- func ParseLogLevel(level string) slog.Level
- func ParseLogLevelFromEnv() slog.Level
- func RefreshLogLevel(l *slog.LevelVar, t *time.Ticker)
- func TextHandler(w io.Writer, opts *slog.HandlerOptions) slog.Handler
- type HandlerFn
Examples ¶
Constants ¶
const DefaultRefreshInterval = time.Second * 5
Variables ¶
This section is empty.
Functions ¶
func JSONHandler ¶
JSONHandler is a LogHandlerFn shim for slog.NewJSONHandler.
func NewDefaultEnvLogger ¶
NewDefaultEnvLogger creates new slog.Logger using sane default configuration and sets it as a default logger. Environment variables can be used to configure loggers format and level. Changing log level at runtime is also supported.
Name: Value: LOG_LEVEL DEBUG|INFO|WARN|ERROR LOG_FORMAT JSON|TEXT
Note: LOG_FORMAT can't be changed at runtime.
Example ¶
package main import ( "log/slog" "github.com/elisasre/go-common/log" ) func main() { log.NewDefaultEnvLogger() slog.Info("Hello world") slog.Error("Some error") }
Output:
func ParseLogLevel ¶
ParseFormat turns string into slog.Level using case-insensitive parser. If the input doesn't match to any slog.Level then slog.LevelInfo is used.
func ParseLogLevelFromEnv ¶
ParseLogLevelFromEnv turns LOG_LEVEL env variable into slog.Level using logic from ParseLogLevel.
func RefreshLogLevel ¶
RefreshLogLevel updates l's value from env with given interval until ticker is stopped.
func TextHandler ¶
TextHandler is a LogHandlerFn shim for slog.NewTextHandler.
Types ¶
type HandlerFn ¶
HandlerFn is a shim type for slog's NewHandler functions.
func ParseFormat ¶
ParseFormat parses string into supported log handler function. If the input doesn't match to any supported format then JSON is used.
func ParseFormatEnv ¶
func ParseFormatEnv() HandlerFn
ParseFormatEnv turns LOG_FORMAT env variable into slog.Handler function using ParseLogFormat.