Skip to content
Go logging library
Go
Branch: master
Clone or download
Latest commit 8330568 Nov 11, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.codecov.yaml Codecov config Sep 15, 2019
.travis.yml Support Go 1.11 and newer Sep 15, 2019
LICENSE Hello, world May 17, 2019
README.markdown Rename Profile helpers Sep 15, 2019
go.mod Better formatting of non-strings Nov 10, 2019
go.sum Better formatting of non-strings Nov 10, 2019
output_std.go Use %q Nov 11, 2019
zlog.go Add zlog.Field Nov 7, 2019
zlog_test.go Better formatting of non-strings Nov 10, 2019

README.markdown

This project is considered stable Build Status codecov GoDoc

Go logging library. Canonical import path: zgo.at/zlog. You will need Go 1.11 or newer.

The main goal is to offer a friendly and ergonomic API.

Getting the maximum possible amount of performance or zero-allocations are not goals, although simple benchmarks show it should be more than Fast Enough™ for most purposes (if not, there are a few max-performance libraries already).

Usage

Basics:

zlog.Print("foo")                  // 15:55:17 foo
zlog.Printf("foo %d", 1)           // 15:55:17 foo 1
zlog.Error(err)                    // 15:55:17 oh noes
zlog.Errorf("foo %d", 1)           // 15:55:17 foo 1

This does what you expect: output a message to stdout or stderr.

You can add module information and fields for extra information:

log := zlog.Module("test")
log.Print("foo")                    // 15:56:12 test: foo

log = l.Fields(zlog.F{"foo": "bar"})
log.Print("foo")                    // 15:56:55 test: foo key="val"

Debug logs are printed only for modules marked as debug:

zlog.Module("bar").Debug("w00t")    // Prints nothing (didn't enable module "bar").
log := zlog.SetDebug("bar")         // Enable debug logs only for module "bar".
log.Module("bar").Debug("w00t")     // 15:56:55 w00t

Trace logs are like debug logs, but are also printed when there is an error:

log := zlog.Module("foo")
log.Trace("useful info")
log.Error(err)
log.ResetTrace()                    // Remove all traces.

This is pretty useful for adding context to errors without clobbering your general log with mostly useless info.

You can also easily record timings; this is printed for modules marked as debug:

log := zlog.SetDebug("long-running").Module("long-running")

time.Sleep(1 * time.Second)
log = log.Since("sleep one")    //   long-running 11ms  sleep one

time.Sleep(20*time.Millisecond)
log.Since("sleep two")          //   long-running 11ms  sleep two

// Add timing as fields, always works regardless of Debug.
log.SinceLog().Print("long-running finshed") 

The Recover() helper function makes it easier to recover from panics in goroutines:

go func() {
    defer zlog.Recover()        // Recover panics and report with Error().
    panic("oh noes!")
}

See GoDoc for the full reference.

Configuration

Configuration is done by setting the Config variable (usually during initialisation of your app).

It's not possible to configure individual logger instances. It's not often needed, and adds some complexity.

Existing outputs you can use:

You can’t perform that action at this time.