Documentation ¶
Overview ¶
Package devlog implements a structured log (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", "port", 8000, "environment", "DEV") // [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 InitDefaultLogHandler ¶ added in v0.5.0
InitDefaultLogHandler is shorthand for:
- Calling NewHandler with the given arguments
- Setting it as the default log handler with slog.SetDefault
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. // // Colors are enabled by default when the [io.Writer] given to [NewHandler] is a terminal with // color support (see [IsColorTerminal]). DisableColors bool // ForceColors skips checking [IsColorTerminal] for color support, and includes colors in log // output regardless. It overrides [Options.DisableColors]. ForceColors bool // TimeFormat controls how time is formatted for each log entry. It defaults to // [TimeFormatShort], showing just the time and not the date, but can be set to [TimeFormatFull] // to include the date as well. TimeFormat TimeFormat }
Options configure a log Handler.
type TimeFormat ¶ added in v0.5.0
type TimeFormat int8
See [Options.TimeFormat].
const ( // TimeFormatShort includes just the time, not the date, formatted as: [10:57:30] // // This is the default time format. TimeFormatShort TimeFormat = iota // TimeFormatFull includes both date and time, formatted as: [2024-09-29 10:57:30] TimeFormatFull )