logit
logit: Simple Golang logging/slog library
Features
- No dependencies
- Fluent configuration (one line)
- Extra log levels
- Usual: debug, info, warn, error
- Added: trace, notice, fatal, panic
- Color support
- slog based so it requires Golang 1.21+
- slog.Logger + slog.Handler compatible
- Opinionated
TL;DR
Init module within an empty folder:
$ go mod init example.com/demo
$ cat go.mod
module example.com/demo
go 1.24.5
Download/install logit:
$ go get -u github.com/sfmunoz/logit
Create main.go file:
package main
import "github.com/sfmunoz/logit"
var log = logit.Logit().
WithLevel(logit.LevelNotice).
With("app", "my-app")
func main() {
log.Trace("trace-msg")
log.Debug("debug-msg")
log.Info("info-msg")
log.Notice("notice-msg")
log.Warn("warn-msg")
log.Error("error-msg")
}
Run it:
$ go run main.go
Detailed configuration:
package main
import (
"log/slog"
"os"
"github.com/sfmunoz/logit"
)
var log = logit.Logit().
With("app", "my-app").
WithWriter(os.Stderr).
WithSource(true).
WithLevel(slog.LevelDebug).
WithTimeFormat("2006-01-02T15:04:05.000Z07:00").
WithTime(true).
WithUptime(true).
WithColor(true)
func main() {
log.Info("hello world")
}
References
$ go doc slog
(...)
For a guide to writing a custom handler, see https://golang.org/s/slog-handler-guide
(...)
The guide to read is this one:
https://github.com/golang/example/blob/master/slog-handler-guide/README.md
There are 4 versions of the indenthandler example in the README.md:
https://github.com/golang/example/tree/master/slog-handler-guide
Google: 'golang slog'
slog related videos:
Others
Google: 'golang slog color'
Google: 'golang log packages'
zap & zerolog: