Ripzap - The fastest structured, leveled JSON logger for Go ⚡️. Dependency free.
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
benchmarks update benchmarks Feb 14, 2019
docs from astroflow to astro Dec 18, 2018
examples fix: log's logger caller callerSkipFrameCount Feb 15, 2019
internal/json remove cbor support Feb 9, 2019
log fix: log's logger caller callerSkipFrameCount Feb 15, 2019
pkgerrors fix: log's logger caller callerSkipFrameCount Feb 15, 2019
rzhttp fix: log's logger caller callerSkipFrameCount Feb 15, 2019
.gitignore fix stacktrace Feb 6, 2019
.rocket.sane fix: cicd rocket configuration file Dec 18, 2018
.travis.yml fix cicd Feb 7, 2019
LICENSE.txt move to zero allocation Feb 6, 2019
Makefile test: trying to fix With issue Feb 7, 2019
README.md fix: log's logger caller callerSkipFrameCount Feb 15, 2019
array.go fix NewDict Feb 14, 2019
array_test.go make tests passing Feb 13, 2019
benchmarks_test.go fix: log's logger caller callerSkipFrameCount Feb 15, 2019
config.go fix: log's logger caller callerSkipFrameCount Feb 15, 2019
ctx.go fix: log's logger caller callerSkipFrameCount Feb 15, 2019
ctx_test.go fix: log's logger caller callerSkipFrameCount Feb 15, 2019
defaults.go update logger tests Feb 7, 2019
doc.go fix tests Feb 12, 2019
encoder.go private event methods Feb 13, 2019
encoder_json.go astrolib to bloom42. preparing for PR Feb 13, 2019
event.go fix NewDict Feb 14, 2019
field.go fix: log's logger caller callerSkipFrameCount Feb 15, 2019
fields.go version Feb 7, 2019
formatter.go update Feb 7, 2019
formatter_cli.go FormatterCLI: remove trailing space Feb 10, 2019
formatter_console.go FormatterConsole: improve message displayer when message == "" Feb 15, 2019
formatter_logfmt.go docs: Add Doc strings to HTTP methods Feb 8, 2019
go.mod fix bad dependencies import Feb 16, 2019
hook.go astro to rz Feb 6, 2019
hook_test.go fix: log's logger caller callerSkipFrameCount Feb 15, 2019
logger.go fix: log's logger caller callerSkipFrameCount Feb 15, 2019
logger_example_test.go fix: log's logger caller callerSkipFrameCount Feb 15, 2019
logger_test.go fix: log's logger caller callerSkipFrameCount Feb 15, 2019
loglevel.go fix: FormatterConsole Feb 9, 2019
sampler.go fix: Ctx tests Feb 8, 2019
sampler_test.go remove cbor support Feb 9, 2019
version.go fix bad dependencies import Feb 16, 2019
writer.go update documentation Feb 7, 2019
writer_test.go fix: tests Feb 9, 2019

README.md

rz

RipZap - The fastest structured, leveled JSON logger for Go ⚡️. Dependency free.


Make logging great again

GoDoc Build Status GitHub release

Console logging

The rz package provides a fast and simple logger dedicated to JSON output avoiding allocations and reflection..

Uber's zap and rs's zerolog libraries pioneered this approach.

ripzap is an update of zerolog taking this concept to the next level with a simpler to use and safer API and even better performance.

To keep the code base and the API simple, ripzap focuses on efficient structured logging only. Pretty logging on the console is made possible using the provided (but inefficient) Formatters.

  1. Quickstart
  2. Configuration
  3. Field types
  4. HTTP Handler
  5. Examples
  6. Benchmarks
  7. Contributing
  8. License

Quickstart

package main

import (
	"os"

	"github.com/bloom42/rz-go"
	"github.com/bloom42/rz-go/log"
)

func main() {

	env := os.Getenv("GO_ENV")
	hostname, _ := os.Hostname()

	// update global logger's context fields
	log.SetLogger(log.With(rz.Fields(
		rz.String("hostname", hostname), rz.String("environment", env),
	)))

	if env == "production" {
		log.SetLogger(log.With(rz.Level(rz.InfoLevel)))
	}

	log.Info("info from logger", rz.String("hello", "world"))
	// {"level":"info","hostname":"","environment":"","hello":"world","timestamp":"2019-02-07T09:30:07Z","message":"info from logger"}
}

Configuration

Logger

// Writer update logger's writer.
func Writer(writer io.Writer) LoggerOption {}
// Level update logger's level.
func Level(lvl LogLevel) LoggerOption {}
// Sampler update logger's sampler.
func Sampler(sampler LogSampler) LoggerOption {}
// AddHook appends hook to logger's hook
func AddHook(hook LogHook) LoggerOption {}
// Hooks replaces logger's hooks
func Hooks(hooks ...LogHook) LoggerOption {}
// With replaces logger's context fields
func With(fields func(*Event)) LoggerOption {}
// Stack enable/disable stack in error messages.
func Stack(enableStack bool) LoggerOption {}
// Timestamp enable/disable timestamp logging in error messages.
func Timestamp(enableTimestamp bool) LoggerOption {}
// Caller enable/disable caller field in message messages.
func Caller(enableCaller bool) LoggerOption {}
// Formatter update logger's formatter.
func Formatter(formatter LogFormatter) LoggerOption {}
// TimestampFieldName update logger's timestampFieldName.
func TimestampFieldName(timestampFieldName string) LoggerOption {}
// LevelFieldName update logger's levelFieldName.
func LevelFieldName(levelFieldName string) LoggerOption {}
// MessageFieldName update logger's messageFieldName.
func MessageFieldName(messageFieldName string) LoggerOption {}
// ErrorFieldName update logger's errorFieldName.
func ErrorFieldName(errorFieldName string) LoggerOption {}
// CallerFieldName update logger's callerFieldName.
func CallerFieldName(callerFieldName string) LoggerOption {}
// CallerSkipFrameCount update logger's callerSkipFrameCount.
func CallerSkipFrameCount(callerSkipFrameCount int) LoggerOption {}
// ErrorStackFieldName update logger's errorStackFieldName.
func ErrorStackFieldName(errorStackFieldName string) LoggerOption {}
// TimeFieldFormat update logger's timeFieldFormat.
func TimeFieldFormat(timeFieldFormat string) LoggerOption {}
// TimestampFunc update logger's timestampFunc.
func TimestampFunc(timestampFunc func() time.Time) LoggerOption {}

Global

var (
	// DurationFieldUnit defines the unit for time.Duration type fields added
	// using the Duration method.
	DurationFieldUnit = time.Millisecond

	// DurationFieldInteger renders Duration fields as integer instead of float if
	// set to true.
	DurationFieldInteger = false

	// ErrorHandler is called whenever rz fails to write an event on its
	// output. If not set, an error is printed on the stderr. This handler must
	// be thread safe and non-blocking.
	ErrorHandler func(err error)
)

Field Types

Standard Types

  • String
  • Bool
  • Int, Int8, Int16, Int32, Int64
  • Uint, Uint8, Uint16, Uint32, Uint64
  • Float32, Float64

Advanced Fields

  • Err: Takes an error and render it as a string using the logger.errorFieldName field name.
  • Error: Adds a field with a error.
  • Timestamp: Insert a timestamp field with logger.timestampFieldName field name and formatted using logger.timeFieldFormat.
  • Time: Adds a field with the time formated with the logger.timeFieldFormat.
  • Duration: Adds a field with a time.Duration.
  • Dict: Adds a sub-key/value as a field of the event.
  • Interface: Uses reflection to marshal the type.

HTTP Handler

See the bloom42/rz-go/rzhttp package or the example here.

Examples

See the examples folder.

Benchmarks

$ make benchmarks
cd benchmarks && ./run.sh
goos: linux
goarch: amd64
pkg: github.com/bloom42/rz-go/benchmarks
BenchmarkDisabledWithoutFields/sirupsen/logrus-4                100000000               17.9 ns/op            16 B/op          1 allocs/op
BenchmarkDisabledWithoutFields/uber-go/zap-4                    30000000                38.8 ns/op             0 B/op          0 allocs/op
BenchmarkDisabledWithoutFields/rs/zerolog-4                     300000000                4.61 ns/op            0 B/op          0 allocs/op
BenchmarkDisabledWithoutFields/bloom42/rz-go-4                  300000000                4.29 ns/op            0 B/op          0 allocs/op
BenchmarkWithoutFields/sirupsen/logrus-4                          300000              4430 ns/op            1137 B/op         24 allocs/op
BenchmarkWithoutFields/uber-go/zap-4                             5000000               319 ns/op               0 B/op          0 allocs/op
BenchmarkWithoutFields/rs/zerolog-4                             10000000               123 ns/op               0 B/op          0 allocs/op
BenchmarkWithoutFields/bloom42/rz-go-4                          10000000               119 ns/op               0 B/op          0 allocs/op
Benchmark10Context/sirupsen/logrus-4                               50000             23591 ns/op            3262 B/op         50 allocs/op
Benchmark10Context/uber-go/zap-4                                 5000000               340 ns/op               0 B/op          0 allocs/op
Benchmark10Context/rs/zerolog-4                                 10000000               131 ns/op               0 B/op          0 allocs/op
Benchmark10Context/bloom42/rz-go-4                              10000000               129 ns/op               0 B/op          0 allocs/op
Benchmark10Fields/sirupsen/logrus-4                                50000             25172 ns/op            4043 B/op         54 allocs/op
Benchmark10Fields/uber-go/zap-4                                   500000              2868 ns/op              80 B/op          1 allocs/op
Benchmark10Fields/rs/zerolog-4                                    500000              2354 ns/op             640 B/op          6 allocs/op
Benchmark10Fields/bloom42/rz-go-4                                 500000              2316 ns/op             512 B/op          3 allocs/op
Benchmark10Fields10Context/sirupsen/logrus-4                       50000             25234 ns/op            4567 B/op         53 allocs/op
Benchmark10Fields10Context/uber-go/zap-4                          500000              2879 ns/op              80 B/op          1 allocs/op
Benchmark10Fields10Context/rs/zerolog-4                           500000              2454 ns/op             640 B/op          6 allocs/op
Benchmark10Fields10Context/bloom42/rz-go-4                        500000              2409 ns/op             512 B/op          3 allocs/op
PASS
ok      github.com/bloom42/rz-go/benchmarks     30.044s

Contributing

See https://opensource.bloom.sh/contributing

License

See LICENSE.txt and https://opensource.bloom.sh/licensing

From an original work by rs: zerolog - commit aa55558e4cb2e8f05cd079342d430f77e946d00a