Skip to content

Repository files navigation

Log

License

Log is a simple go logging package that is a wrapper aroung slog.

Install

go get github.com/RevittCo/log

Handler Interface

You can create your own handler for use with this package. Just make it implement this interface.

type Handler interface {
    Enabled(ctx context.Context, l slog.Level) bool
    Handle(ctx context.Context, r slog.Record) error
    WithAttrs(attrs []slog.Attr) slog.Handler
    WithGroup(name string) slog.Handler
}

Usage

log.SetDefault(log.NewLogger(log.NewTerminalHandler(os.Stdout, log.LevelInfo)))
log.Info("logger initialised")

Record format

TerminalHandler writes one record per line:

LEVEL [2006-01-02 15:04:05] pkg/file.go:42 message key=value key2="value with spaces"

Attribute values follow logfmt quoting. A value stays bare while it holds no separator; anything containing a space, ", =, a control character or invalid UTF-8 is written with strconv.Quote. That is what lets a value be read back exactly as it was passed in, and it is what stops an embedded newline from ending the record and forging the next one. EncodeKey, EncodeValue, EncodeMessage and EncodeAttrValue are exported so a handler defined elsewhere can emit the same format rather than reimplement the rule.

ANSI colour is written only when the writer is an interactive terminal, and never when NO_COLOR is set, so logs captured to a file, a pipe or a container's stdout stay plain text.


About

simple logging go package

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages