Documentation
¶
Overview ¶
Package slog provides a BaseLogger implementation for log/slog.
Example ¶
This example shows how to use the slog backend with JSON output. The slog backend is the default and recommended backend.
package main
import (
"context"
"github.com/go-coldbrew/log"
"github.com/go-coldbrew/log/loggers"
cbslog "github.com/go-coldbrew/log/loggers/slog"
)
func main() {
// Create a slog-backed logger with JSON output
logger := cbslog.NewLogger(
loggers.WithJSONLogs(true),
loggers.WithCallerInfo(true),
)
// Set as the global logger
log.SetLogger(log.NewLogger(logger))
// Log normally — output goes through slog's JSONHandler
ctx := context.Background()
log.Info(ctx, "msg", "service started", "port", 8080)
}
Output:
Example (TextOutput) ¶
This example shows how to configure the slog backend with text output and a custom log level.
package main
import (
"context"
"github.com/go-coldbrew/log"
"github.com/go-coldbrew/log/loggers"
cbslog "github.com/go-coldbrew/log/loggers/slog"
)
func main() {
logger := cbslog.NewLogger(
loggers.WithJSONLogs(false),
loggers.WithLevel(loggers.DebugLevel),
)
log.SetLogger(log.NewLogger(logger))
ctx := context.Background()
log.Debug(ctx, "msg", "debug message", "detail", "verbose")
}
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewLogger ¶
func NewLogger(options ...loggers.Option) loggers.BaseLogger
NewLogger returns a BaseLogger implementation backed by log/slog.
func NewLoggerWithHandler ¶
NewLoggerWithHandler returns a BaseLogger implementation backed by the provided slog.Handler. Use this when you need a custom handler (e.g., for testing or custom output formats). Note: SetLevel updates the internally tracked level. Both this level and the provided handler's own level filtering apply; the stricter one wins.
Types ¶
This section is empty.