Categorygithub.com/askeladdk/slog
repositorypackage
0.0.6
Repository: https://github.com/askeladdk/slog.git
Documentation: pkg.go.dev

# README

slog - structured logging for lazy gophers

GoDoc Go Report Card

Overview

Slog parses and converts log messages produced by the standard logger to JSON objects. Any key=value text fragments found in the message are extracted as separate JSON fields. No boilerplate, just Printf.

Install

go get -u github.com/askeladdk/slog

Quickstart

Use the function slog.New to create a log.Logger that produces structured logs. It has the same signature as log.New in the standard library and is backwards compatible.

Enable all features and create a logger:

logger := slog.New(os.StdErr, "level=info ", slog.LstdFlags)

Log an event:

logger.Printf("requested url=%s with method=%s with response status=%d", "/index.html", "GET", 200)

Result:

{"time":"2021-08-08T19:06:35.252044Z","mesg":"level=info requested url=/index.html with method=GET with response status=200","level":"info","url":"/index.html","method":"GET","status":200}

Use slog.NewWriter to create a new structured writer and attach it to the default logger with SetOutput:

log.SetFlags(slog.LstdFlags)
log.SetOutput(slog.NewWriter(os.StdErr, log.Default()))
log.Println("hello world")

Note that the logger flags and prefix must not be changed after a writer has been created.

Read the rest of the documentation on pkg.go.dev. It's easy-peasy!

Performance

Unscientific benchmarks on my laptop suggest that slog is about 50% more memory intensive and 250% more CPU intensive than the standard logger by itself.

% go test -bench=. -benchmem -benchtime=1000000x
goos: darwin
goarch: amd64
pkg: github.com/askeladdk/slog
cpu: Intel(R) Core(TM) i5-5287U CPU @ 2.90GHz
BenchmarkStdLogger-4         	 1000000	      1614 ns/op	     544 B/op	       3 allocs/op
BenchmarkSlog-4              	 1000000	      2808 ns/op	     812 B/op	       3 allocs/op
BenchmarkSlogParseFields-4   	 1000000	      3851 ns/op	     812 B/op	       3 allocs/op
PASS
ok  	github.com/askeladdk/slog	8.439s

License

Package slog is released under the terms of the ISC license.