logit: Simple Golang logging/slog library
- No dependencies
- Fluent configuration (one line)
- Simple and easy
- Compact
- 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
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 using go run main.go:
Detailed configuration:
package main
import (
"os"
"github.com/sfmunoz/logit"
)
var log = logit.Logit().
With("app", "my-app").
WithWriter(os.Stderr).
WithTpl(logit.TplTime, logit.TplUptime, logit.TplLevel, logit.TplSource, logit.TplMessage, logit.TplAttrs).
WithLevel(logit.LevelDebug).
WithTimeFormat("2006-01-02T15:04:05.000Z07:00").
WithColor(true)
func main() {
log.Info("hello world")
}Run it too with go run main.go:
$ go doc slog
(...)
For a guide to writing a custom handler, see https://golang.org/s/slog-handler-guide
(...)
Tip
So the must reading guide is the following one:
https://golang.org/s/slog-handler-guide → 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:
Google: 'golang slog color'
- https://github.com/lmittmann/tint
- https://dusted.codes/creating-a-pretty-console-logger-using-gos-slog-package
Google: 'golang log packages'
- https://betterstack.com/community/guides/logging/best-golang-logging-libraries/
- https://blog.logrocket.com/5-structured-logging-packages-for-go/
- https://www.highlight.io/blog/5-best-logging-libraries-for-go
zap & zerolog: