Skip to content
generated from atomicgo/template

♻️ Automatically change the underlying slog handler, based on the environment.

License

Notifications You must be signed in to change notification settings

atomicgo/chslog

Repository files navigation

AtomicGo | chslog

Downloads Latest Release Tests Coverage Unit test count License: MIT Go report


Documentation | Contributing | Code of Conduct


AtomicGo

go get atomicgo.dev/chslog

chslog

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
}

Index

Variables

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

About

♻️ Automatically change the underlying slog handler, based on the environment.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published