Skip to content

jaxxstorm/log

Repository files navigation

log

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.

What it does

  • Defaults to pretty output on a TTY and JSON when output is redirected.
  • Supports debug, info, warn, error, and fatal levels.
  • Adds caller information and timestamps automatically.
  • Lets you attach persistent structured fields with With(...).
  • Can write to any io.Writer or directly to a file path.

Install

go get github.com/jaxxstorm/log

Basic usage

package 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"))
}

Add context with With

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))
}

Control format and destination

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()

Available options

log.Config supports:

  • Level: debug, info, warn, error, or fatal

fatal emits the log entry, performs best-effort final sync, and then exits the process with status code 1.

  • Format: auto, pretty, or json
  • Output: any io.Writer
  • OutputPath: a file to append logs to

If Format is auto or left empty, the logger chooses pretty output for a terminal and JSON otherwise.

About

A logging library i like

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages