- -This example shows how to use the gokit backend. - -Deprecated: Use the slog backend instead \(loggers/slog\). The go\-kit/log library is in maintenance mode and no longer actively developed. - -```go -package main - -import ( - "context" - - "github.com/go-coldbrew/log" - "github.com/go-coldbrew/log/loggers" - "github.com/go-coldbrew/log/loggers/gokit" -) - -func main() { - logger := gokit.NewLogger( - loggers.WithJSONLogs(true), - loggers.WithCallerInfo(true), - ) - log.SetLogger(log.NewLogger(logger)) - - ctx := context.Background() - log.Info(ctx, "msg", "request handled", "method", "GET", "path", "/api/v1") -} -``` - -
-- -This example shows how to use the slog backend with JSON output. The slog backend is the default and recommended backend. - -```go -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) -} -``` - -
-- -This example shows how to configure the slog backend with text output and a custom log level. - -```go -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") -} -``` - -
-- -This example shows how to use the stdlog backend. It uses Go's standard "log" package for output — simple but no structured formatting. - -```go -package main - -import ( - "context" - - "github.com/go-coldbrew/log" - "github.com/go-coldbrew/log/loggers/stdlog" -) - -func main() { - logger := stdlog.NewLogger() - log.SetLogger(log.NewLogger(logger)) - - ctx := context.Background() - log.Info(ctx, "msg", "server started", "port", 8080) -} -``` - -
-- -This example shows how to use the zap backend with JSON output. - -```go -package main - -import ( - "context" - - "github.com/go-coldbrew/log" - "github.com/go-coldbrew/log/loggers" - "github.com/go-coldbrew/log/loggers/zap" -) - -func main() { - logger := zap.NewLogger( - loggers.WithJSONLogs(true), - loggers.WithCallerInfo(true), - ) - log.SetLogger(log.NewLogger(logger)) - - ctx := context.Background() - log.Info(ctx, "msg", "request handled", "method", "POST", "latency_ms", 12) -} -``` - -
-