Documentation ¶
Overview ¶
Package log provides sane default loggers using slog.
Index ¶
- func JSONHandler(w io.Writer, opts *slog.HandlerOptions) slog.Handler
- func NewDefaultEnvLogger(opts ...Opt) *slog.Logger
- func ParseLogLevel(level string) slog.Level
- func ParseLogLevelFromEnv() slog.Level
- func ParseSource(source string) bool
- func ParseSourceFromEnv() bool
- func TextHandler(w io.Writer, opts *slog.HandlerOptions) slog.Handler
- type HandlerFn
- type Opt
Examples ¶
Constants ¶
This section is empty.
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. Options can be provided to overwrite defaults.
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 ParseSource ¶ added in v1.3.5
func ParseSourceFromEnv ¶ added in v1.3.5
func ParseSourceFromEnv() bool
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 ParseFormatFromEnv ¶ added in v1.3.5
func ParseFormatFromEnv() HandlerFn
ParseFormatFromEnv turns LOG_FORMAT env variable into slog.Handler function using ParseLogFormat.
type Opt ¶ added in v1.3.1
type Opt func(*builder)
func WithHandlerFn ¶ added in v1.3.1
WithHandlerFn can be used to provide slog.Handler lazily.
func WithLeveler ¶ added in v1.3.1
WithLeveler sets slog.HandlerOptions.Level.
func WithReplacer ¶ added in v1.3.1
WithReplacer sets slog.HandlerOptions.ReplaceAttr.
func WithShortSource ¶ added in v1.3.7
WithShortSource sets slog.ReplaceAttr source file as short format.
func WithSource ¶ added in v1.3.1
WithSource sets slog.HandlerOptions.AddSource.