devlog

package module
v0.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2023 License: MIT Imports: 14 Imported by: 4

README

devlog

A structured logging handler for Go, with a human-readable output format designed for development builds.

Run go get hermannm.dev/devlog to add it to your project!

Usage

devlog.Handler implements slog.Handler, so it can handle output for slog's logging functions. It can be configured as follows:

logHandler := devlog.NewHandler(os.Stdout, nil)
slog.SetDefault(slog.New(logHandler))

Logging with slog will now use this handler:

slog.Warn("no value found for 'PORT' in env, defaulting to 8000")
slog.Info("server started", slog.Int("port", 8000), slog.String("environment", "DEV"))
slog.Error(
	"database query failed",
	slog.Group("dbError", slog.Int("code", 60), slog.String("message", "UNKNOWN_TABLE")),
)

...giving the following output (using a gruvbox terminal color scheme):

Screenshot of log messages in a terminal

This output is meant to be easily read by a developer working locally. However, you may want a more structured format for production, such as JSON, to make log analysis easier. You can get both by conditionally choosing the log handler for your application, e.g.:

var logHandler slog.Handler
switch os.Getenv("ENVIRONMENT") {
case "PROD":
	logHandler = slog.NewJSONHandler(os.Stdout, nil)
case "DEV":
	logHandler = devlog.NewHandler(os.Stdout, nil)
}

slog.SetDefault(slog.New(logHandler))

devlog/log

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.

Example using devlog and devlog/log together:

import (
	"errors"
	"log/slog"
	"os"

	"hermannm.dev/devlog"
	"hermannm.dev/devlog/log"
)

func main() {
	logHandler := devlog.NewHandler(os.Stdout, nil)
	slog.SetDefault(slog.New(logHandler))

	user := map[string]any{"id": 2, "username": "hermannm"}
	err := errors.New("username taken")
	log.ErrorCause(err, "failed to create user", log.JSON("user", user))
}

This gives the following output:

Screenshot of log messages in a terminal

Credits

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

func IsColorTerminal(output io.Writer) bool

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

func NewHandler(output io.Writer, options *Options) *Handler

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

func (handler *Handler) Enabled(_ context.Context, level slog.Level) bool

Enabled reports whether the handler is configured to log records at the given level.

func (*Handler) Handle

func (handler *Handler) Handle(_ context.Context, record slog.Record) error

Handle writes the given log record to the handler's output. See the package-level documentation for more on the output format.

func (*Handler) WithAttrs

func (handler *Handler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs returns a new Handler which adds the given attributes to every log record.

func (*Handler) WithGroup

func (handler *Handler) WithGroup(name string) slog.Handler

WithGroup returns a new Handler where all future log record attributes are nested under the given group name.

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.

Directories

Path Synopsis
Package log provides a thin wrapper over the log/slog package, with utility functions for log message formatting.
Package log provides a thin wrapper over the log/slog package, with utility functions for log message formatting.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL