Skip to content

sykesm/zap-logfmt

 
 

Repository files navigation

Logfmt Encoder

This package provides a logfmt encoder for zap.

It is a fork of github.com/jsternberg/zap-logfmt that improves the handling of reflected fields and encodes arrays and objects instead of dropping them from logs. While logging simple fields is preferred for many reasons, having ugly data is often better than missing data.

Build Status GoDoc

Usage

The encoder is easy to configure. Simply create a new core with an instance of the logfmt encoder and use it with your preferred logging interface.

package main

import (
	"os"

	"github.com/sykesm/zap-logfmt"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

func main() {
	config := zap.NewProductionEncoderConfig()
	logger := zap.New(zapcore.NewCore(
		zaplogfmt.NewEncoder(config),
		os.Stdout,
		zapcore.DebugLevel,
	))
	logger.Info("Hello World")
}

Arrays, Objects, and Reflected Fields

While it's best to avoid complex data types in log fields, there are times when they sneak in. When complex fields are included in log records, they will be encoded, but they won't be very pretty.

Arrays

Arrays are encoded as a comma separated list of values within square brackets. This format is very similar to JSON encoding. Arrays of simple scalars remain quite readable but including elements that require quoting will result in very ugly records.

Objects

Objects are encoded as a space separated list of key=value pairs. Because this format includes an equals sign, the encoded object will require quoting. If any value in the object requires quoting, the required escapes will make the encoded field pretty difficult for humans to read.

Channels and Functions

Channels and functions are encoded as their type and their address. There aren't many meaningful ways to log channels and functions...

Maps and Structs

Maps and structs are encoded as strings that contain the result of fmt.Sprint.

Namespaces

Namespaces are supported. If a namespace is opened, all of the keys will be prepended with the namespace name. For example, with the namespace foo and the key bar, you would get a key of foo.bar.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%