log is a small Go logging package I use for my own projects. It wraps Zap with
simple configuration, structured fields, and automatic pretty vs JSON output.
This repository is for personal use. I am not looking for outside pull requests.
- Defaults to pretty output on a TTY and JSON when output is redirected.
- Supports
debug,info,warn,error, andfatallevels. - Adds caller information and timestamps automatically.
- Lets you attach persistent structured fields with
With(...). - Can write to any
io.Writeror directly to a file path.
go get github.com/jaxxstorm/logpackage main
import (
log "github.com/jaxxstorm/log"
)
func main() {
logger, err := log.New(log.Config{})
if err != nil {
panic(err)
}
defer logger.Close()
logger.Info("service started", log.String("addr", ":8080"))
}package main
import (
log "github.com/jaxxstorm/log"
)
func main() {
logger, err := log.New(log.Config{
Level: log.DebugLevel,
})
if err != nil {
panic(err)
}
defer logger.Close()
requestLogger := logger.With(
log.String("component", "api"),
log.String("request_id", "req-42"),
)
requestLogger.Info("request started", log.String("route", "/healthz"))
requestLogger.Error("request failed", log.Int("status", 503))
}package main
import (
"os"
log "github.com/jaxxstorm/log"
)
func main() {
logger, err := log.New(log.Config{
Level: log.InfoLevel,
Format: log.JSONFormat,
Output: os.Stdout,
})
if err != nil {
panic(err)
}
defer logger.Close()
logger.Info("json log line", log.Bool("ok", true))
}logger, err := log.New(log.Config{
OutputPath: "app.log",
})
if err != nil {
panic(err)
}
defer logger.Close()log.Config supports:
Level:debug,info,warn,error, orfatal
fatal emits the log entry, performs best-effort final sync, and then exits the
process with status code 1.
Format:auto,pretty, orjsonOutput: anyio.WriterOutputPath: a file to append logs to
If Format is auto or left empty, the logger chooses pretty output for a
terminal and JSON otherwise.