Documentation ¶
Overview ¶
Package devlog implements a structured logging (slog) handler, with a human-readable output format designed for development builds.
A devlog.Handler can be configured as follows:
logger := slog.New(devlog.NewHandler(os.Stdout, nil)) slog.SetDefault(logger)
Following calls to log/slog's logging functions will use this handler, giving output on the following format:
slog.Info("Server started", slog.Int("port", 8000), slog.String("environment", "DEV")) // [2023-10-21 10:31:09] INFO: Server started // - port: 8000 // - environment: DEV
Check the README to see the output format with colors.
To complement devlog's output handling, the devlog/log subpackage provides input handling. It is a thin wrapper over the slog package, with utility functions for log message formatting.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsColorTerminal ¶ added in v0.4.0
IsColorTerminal checks if the given writer is a terminal with ANSI color support. It respects NO_COLOR, FORCE_COLOR and TERM=dumb environment variables.
Types ¶
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler is a slog.Handler that outputs log records in a human-readable format, designed for development builds. See the package-level documentation for more on the output format.
func NewHandler ¶
NewHandler creates a log Handler that writes to output, using the given options. If options is nil, the default options are used.
func (*Handler) Enabled ¶
Enabled reports whether the handler is configured to log records at the given level.
func (*Handler) Handle ¶
Handle writes the given log record to the handler's output. See the package-level documentation for more on the output format.
type Options ¶
type Options struct { // Level is the minimum log record level that will be logged. // If nil, defaults to slog.LevelInfo. Level slog.Leveler // AddSource adds a 'source' attribute to every log record, with the file name and line number // where the log record was produced. // Defaults to false. AddSource bool // DisableColors removes colors from log output. // Defaults to false (i.e. colors enabled), but if [color.IsColorTerminal] returns false, then // colors are disabled. DisableColors bool // ForceColors skips checking [color.IsColorTerminal] for color support, and includes colors // in log output regardless. It overrides DisableColors. ForceColors bool }
Options configure a log Handler.