Skip to content
/ log Public

A simple, leveled, fast, zero allocation, json structured logging package for Go. Designed to make logging on the hot path dirt cheap, dependency free and my life easier.

License

Notifications You must be signed in to change notification settings

brunotm/log

Repository files navigation

log

Build Status Go Report Card GoDoc

A simple, leveled, fast, zero allocation, structured logging package for Go. Designed to make logging on the hot path dirt cheap, dependency free and my life easier.

A simpler textual key/value format is also available by setting Config.Text to true

By default log entries with the same level and message within each second, will be sampled to cap CPU and I/O load under high logging activity. This behavior can be disabled by setting Config.EnableSampling to false

Usage

Default package logger

import (
    "github.com/brunotm/log"
)

func main() {
    // Simple logging
    log.Info("info message").String("key", "value").Write()
    // Text format:
    // time="2021-03-25T13:32:50.391Z" level="info" caller="_local/main.go:26" app="app1" message="info message" key="value"
    // JSON format:
    // {"time":"2021-03-25T13:33:20.547Z", "level":"info", "caller":"_local/main.go:26", "app":"app1", "message":"info message", "key":"value"}
}

New customized logger

package main

import (
    "os"

    "github.com/brunotm/log"
)

func main() {
    config := DefaultConfig
    config.Level = DEBUG
    config.Format = FormatText // Enable text logging

    // New logger with added context
    l := New(os.Stdout, config).
        With(func(e Entry) {
            e.String("app", "app1")
        })

    // Simple logging
    l.Info("info message").String("key", "value").Write()
    // Text format:
    // time="2021-03-25T13:32:50.391Z" level="info" caller="_local/main.go:26" app="app1" message="info message" key="value"
    // JSON format:
    // {"time":"2021-03-25T13:33:20.547Z", "level":"info", "caller":"_local/main.go:26", "app":"app1", "message":"info message", "key":"value"}

    l.Warn("warn message").Bool("flag", false).Write()
    // Text format:
    // time="2021-03-25T13:32:50.391Z" level="warn" caller="_local/main.go:29" app="app1" message="warn message" flag=false
    // JSON format:
    // {"time":"2021-03-25T13:33:20.547Z", "level":"warn", "caller":"_local/main.go:29", "app":"app1", "message":"warn message", "flag":false}

    l.Error("caught an error").String("error", "request error").Write()
    // Text format:
    // time="2021-03-25T13:32:50.391Z" level="error" caller="_local/main.go:32" app="app1" message="caught an error" error="request error"
    // JSON format:
    // {"time":"2021-03-25T13:33:20.547Z", "level":"error", "caller":"_local/main.go:32", "app":"app1", "message":"caught an error", "error":"request error"}

    l.Fatal("caught an unrecoverable error").Error("error", errors.New("some error")).Write()
    // Text format:
    // time="2021-03-25T13:32:50.391Z" level="fatal" caller="_local/main.go:35" app="app1" message="caught an unrecoverable error" error="some error"
    // JSON format:
    // {"time":"2021-03-25T13:33:20.547Z", "level":"fatal", "caller":"_local/main.go:35", "app":"app1", "message":"caught an unrecoverable error", "error":"some error"}
}

Performance on a 2,3 GHz Intel Core i5, 2017 13-inch Macbook Pro

Message: {"level":"info","time":"2019-01-30T20:54:07.029Z","message":"informational message","string value":"text","int value":8,"float":722727272.0099,"int":8,"float value":722727272.0099}

pkg: github.com/brunotm/log
BenchmarkLogNoSampling-8         1672147               704 ns/op               0 B/op          0 allocs/op
BenchmarkLogWithSampling-8      10266382               111 ns/op               0 B/op          0 allocs/op
BenchmarkLogNoLevel-8           340966922                3.46 ns/op            0 B/op          0 allocs/op

Contact

Bruno Moura [email protected]

License

log source code is available under the Apache Version 2.0 License

About

A simple, leveled, fast, zero allocation, json structured logging package for Go. Designed to make logging on the hot path dirt cheap, dependency free and my life easier.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages