Documentation ¶
Index ¶
- func AddAttrs(ctx context.Context, attrs ...slog.Attr) context.Context
- func LogLevel() slog.Leveler
- func LogOutput() io.Writer
- func NewContextHandler(handler slog.Handler) slog.Handler
- func NewErrorWarnHandler(handler slog.Handler, funcs ...ErrorCheckFunction) slog.Handler
- func NewLogger() *slog.Logger
- func NewOtelHandler(handler slog.Handler) slog.Handler
- func SetErrorWarnHandler(funcs ...ErrorCheckFunction)
- type ContextHandler
- type ErrorCheckFunction
- type ErrorWarnHandler
- type OtelHandler
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewContextHandler ¶
NewContextHandler returns a new ContextHandler.
func NewErrorWarnHandler ¶
func NewErrorWarnHandler(handler slog.Handler, funcs ...ErrorCheckFunction) slog.Handler
NewErrorWarnHandler returns a new ErrorWarnHandler.
func NewLogger ¶
NewLogger sets and returns a new default logger.
Example ¶
// Setup test f, _ := os.CreateTemp("", "out") os.Stderr = f defer f.Close() // Predefine environment os.Setenv("ENVIRONMENT", "test") os.Setenv("HOSTNAME", "local") os.Setenv("LOG_OUTPUT", "stderr") ctx := context.Background() // Example NewLogger() slog.With("key", "value").InfoContext(ctx, "writing logs") slog.With("error", errors.New("error")).ErrorContext(ctx, "writing errors") // Post process output ReplaceTimestamps(f, os.Stdout)
Output: {"time":"TIMESTAMP","level":"INFO","msg":"writing logs","host":"local","environment":"test","key":"value"} {"time":"TIMESTAMP","level":"ERROR","msg":"writing errors","host":"local","environment":"test","error":"error"}
func NewOtelHandler ¶
NewOtelHandler returns a new OtelHandler.
func SetErrorWarnHandler ¶
func SetErrorWarnHandler(funcs ...ErrorCheckFunction)
SetErrorWarnHandler will wrap the default slog.Logger's handler with an ErrorWarnHandler.
Types ¶
type ContextHandler ¶
ContextHandler is a slog.Handler that adds attributes to the log record from the context.
Example ¶
// Setup test f, _ := os.CreateTemp("", "out") os.Stderr = f defer f.Close() // Predefine environment os.Setenv("ENVIRONMENT", "test") os.Setenv("HOSTNAME", "local") os.Setenv("LOG_OUTPUT", "stderr") ctx := context.Background() // Example NewLogger() ctx = AddAttrs(ctx, slog.String("accountID", "12345")) slog.InfoContext(ctx, "writing logs") slog.With("key", "value").InfoContext(ctx, "writing logs with attributes") slog.Default().WithGroup("request").With("path", "/", "verb", "GET").InfoContext(ctx, "writing logs with group") // Post process output ReplaceTimestamps(f, os.Stdout)
Output: {"time":"TIMESTAMP","level":"INFO","msg":"writing logs","host":"local","environment":"test","accountID":"12345"} {"time":"TIMESTAMP","level":"INFO","msg":"writing logs with attributes","host":"local","environment":"test","key":"value","accountID":"12345"} {"time":"TIMESTAMP","level":"INFO","msg":"writing logs with group","host":"local","environment":"test","request":{"path":"/","verb":"GET","accountID":"12345"}}
type ErrorCheckFunction ¶
ErrorCheckFunction is a function that checks if an error matches a condition.
type ErrorWarnHandler ¶
ErrorWarnHandler is a slog.Handler that will downgrade the log level of a record to WARN if there is an "error" attr that match one of the error check functions.
Example ¶
// Setup test f, _ := os.CreateTemp("", "out") os.Stderr = f defer f.Close() // Predefine environment os.Setenv("ENVIRONMENT", "test") os.Setenv("HOSTNAME", "local") os.Setenv("LOG_OUTPUT", "stderr") ctx := context.Background() warnCheck := func(err error) bool { return err != nil && err.Error() == "warn" } // Example NewLogger() SetErrorWarnHandler(warnCheck) slog.With("error", errors.New("error")).ErrorContext(ctx, "writing errors") slog.With("error", errors.New("warn")).ErrorContext(ctx, "writing errors that should be warnings") slog.Default().WithGroup("request").With("path", "/", "verb", "GET").With("error", errors.New("warn")).ErrorContext(ctx, "writing errors that should be warnings with group") // Post process output ReplaceTimestamps(f, os.Stdout)
Output: {"time":"TIMESTAMP","level":"ERROR","msg":"writing errors","host":"local","environment":"test","error":"error"} {"time":"TIMESTAMP","level":"WARN","msg":"writing errors that should be warnings","host":"local","environment":"test","error":"warn"} {"time":"TIMESTAMP","level":"WARN","msg":"writing errors that should be warnings with group","host":"local","environment":"test","request":{"path":"/","verb":"GET","error":"warn"}}
func (ErrorWarnHandler) Handle ¶
Handle checks the log record for an error attr and compares it against the ErrorCheckFunctions. If any of the ErrorCheckFunctions return true, the log level will be overridden to WARN.
type OtelHandler ¶
OtelHandler is a slog.Handler that adds attributes to the log record from the context.