Skip to content

Getting started

golog is a handler for the standard library’s log/slog. You log with a *slog.Logger, as you would with slog’s own handlers, and golog writes each record as one line for people to read:

2026-10-24T09:41:07.312+11:00 INFO server started addr=:8080
2026-10-24T09:41:09.004+11:00 WARN slow request http.method=GET http.path=/search http.took=1.204s
2026-10-24T09:41:12.871+11:00 ERROR payment failed order=1042 err="card declined"

golog needs Go 1.26 or newer.

Terminal window
go get github.com/vardius/golog/v2
logger := slog.New(golog.NewHandler(os.Stderr, &golog.Options{Color: true}))
logger.Info("server started", "addr", ":8080")

NewHandler takes the writer for the lines and the options; nil options log Info and above, without colour. To make it the logger behind slog.Info and the other package functions, pass it to slog.SetDefault.

This program logs a few records the way a server would, in colour when standard error is a terminal:

examples/console/main.go
// Command console logs a few records to the terminal, in colour when it is one. -debug logs Debug records too.
//
// go run ./examples/console -debug
package main
import (
"errors"
"flag"
"io"
"log/slog"
"os"
"time"
"github.com/vardius/golog/v2"
)
// newLogger logs to w, Debug records too if debug is set, and in colour if color is.
func newLogger(w io.Writer, debug, color bool) *slog.Logger {
level := slog.LevelInfo
if debug {
level = slog.LevelDebug
}
return slog.New(golog.NewHandler(w, &golog.Options{Level: level, Color: color}))
}
// isTerminal reports whether f is a character device, as a terminal is: colour codes in a file or a pipe are noise.
func isTerminal(f *os.File) bool {
fi, err := f.Stat()
return err == nil && fi.Mode()&os.ModeCharDevice != 0
}
func serve(logger *slog.Logger) {
logger.Debug("config loaded", "path", "config.toml")
logger.Info("server started", "addr", ":8080")
http := logger.WithGroup("http")
http.Info("request", "method", "GET", "path", "/", "status", 200, "took", 3*time.Millisecond)
http.Warn("slow request", "method", "GET", "path", "/search", "status", 200, "took", 1204*time.Millisecond)
logger.Error("payment failed", "order", 1042, "err", errors.New("card declined"))
}
func main() {
debug := flag.Bool("debug", false, "log Debug records too")
flag.Parse()
serve(newLogger(os.Stderr, *debug, isTerminal(os.Stderr)))
}
  1. From a clone of the repository, run it in a terminal:

    Terminal window
    go run ./examples/console

    Four lines, with the levels in colour and the time and keys dimmed.

  2. Add -debug to log Debug records too:

    Terminal window
    go run ./examples/console -debug
  3. Send the lines to a file instead. Standard error isn’t a terminal now, so there’s no colour:

    Terminal window
    go run ./examples/console 2> app.log
    cat app.log
  • golog.NewHandler made a slog.Handler, and slog.New a logger around it. Everything else is slog’s API.
  • Each value came after its key, and came out as key=value. "card declined" is quoted because it has a space in it.
  • WithGroup("http") put http. before the keys that followed it.
  • With -debug unset, the Debug record was dropped before any of its values were formatted.