Documentation | Contributing | Code of Conduct
import "atomicgo.dev/chslog"
Package chslog provides a simple way to set up a logger that automatically chooses between a production and a development logger.
Example:
func main() {
// Uses a text logger in development and a JSON logger in production.
prodHandler := slog.NewJSONHandler(os.Stdout, nil)
devHandler := slog.NewTextHandler(os.Stdout, nil)
handler := slogch.Choose(prodHandler, devHandler)
logger := slog.New(handler)
logger.Info("Hello, World!", "foo", "bar")
// Prod: {"time":"2023-08-03T01:31:27.6681464+02:00","level":"INFO","msg":"Hello, World!","foo":"bar"}
// Dev: time=2023-08-03T01:30:23.438+02:00 level=INFO msg="Hello, World!" foo=bar
}
Conditions holds the environment conditions that are checked. By default, it checks for the most common environments with https://atomicgo.dev/isprod. See https://pkg.go.dev/atomicgo.dev/isprod#Conditions for more info, on how to configure them (if needed).
var Conditions = isprod.DefaultConditions
func Choose
func Choose(prodHandler, devHandler slog.Handler) slog.Handler
Choose automatically chooses between prodHandler and devHandler.
Example
package main
import (
slogch "atomicgo.dev/chslog"
"log/slog"
"os"
)
func main() {
// Uses a text logger in development and a JSON logger in production.
prodHandler := slog.NewJSONHandler(os.Stdout, nil)
devHandler := slog.NewTextHandler(os.Stdout, nil)
handler := slogch.Choose(prodHandler, devHandler)
logger := slog.New(handler)
logger.Info("Hello, World!", "foo", "bar")
}
Generated by gomarkdoc
AtomicGo.dev · with ❤️ by @MarvinJWendt | MarvinJWendt.com