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=:80802026-10-24T09:41:09.004+11:00 WARN slow request http.method=GET http.path=/search http.took=1.204s2026-10-24T09:41:12.871+11:00 ERROR payment failed order=1042 err="card declined"Install
Section titled “Install”golog needs Go 1.26 or newer.
go get github.com/vardius/golog/v2Your first logger
Section titled “Your first logger”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.
A whole program
Section titled “A whole program”This program logs a few records the way a server would, in colour when standard error is a terminal:
// Command console logs a few records to the terminal, in colour when it is one. -debug logs Debug records too.//// go run ./examples/console -debugpackage 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)))}Run it
Section titled “Run it”-
From a clone of the repository, run it in a terminal:
Terminal window go run ./examples/consoleFour lines, with the levels in colour and the time and keys dimmed.
-
Add
-debugto log Debug records too:Terminal window go run ./examples/console -debug -
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.logcat app.log
What just happened
Section titled “What just happened”golog.NewHandlermade aslog.Handler, andslog.Newa 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")puthttp.before the keys that followed it.- With
-debugunset, the Debug record was dropped before any of its values were formatted.