zaplogfmt

package module
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 22, 2020 License: MIT Imports: 13 Imported by: 518

README

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.

Documentation

Overview

Package zaplogfmt provides a zap encoder that formats log entries in "logfmt" format.

Example (Array)
package main

import (
	"os"

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

func main() {
	config := zap.NewProductionEncoderConfig()
	config.TimeKey = ""

	logger := zap.New(zapcore.NewCore(
		zaplogfmt.NewEncoder(config),
		os.Stdout,
		zapcore.DebugLevel,
	))

	logger.Info("counting", zap.Ints("values", []int{0, 1, 2, 3}))

}
Output:

level=info msg=counting values=[0,1,2,3]
Example (Object)
package main

import (
	"os"

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

type Person struct {
	First string
	Last  string
	Age   int
}

func (p Person) MarshalLogObject(enc zapcore.ObjectEncoder) error {
	enc.AddString("first", p.First)
	enc.AddString("last", p.Last)
	enc.AddInt("age", p.Age)
	return nil
}

func main() {
	config := zap.NewProductionEncoderConfig()
	config.TimeKey = ""

	logger := zap.New(zapcore.NewCore(
		zaplogfmt.NewEncoder(config),
		os.Stdout,
		zapcore.DebugLevel,
	))

	person := Person{First: "Arthur", Last: "Dent", Age: 42}
	logger.Warn("hitchhiker discovered", zap.Object("identity", person))

}
Output:

level=warn msg="hitchhiker discovered" identity="first=Arthur last=Dent age=42"
Example (Usage)
package main

import (
	"os"

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

func main() {
	config := zap.NewProductionEncoderConfig()
	config.TimeKey = ""

	logger := zap.New(zapcore.NewCore(
		zaplogfmt.NewEncoder(config),
		os.Stdout,
		zapcore.DebugLevel,
	)).Named("main").With(zap.String("type", "greeting"))

	logger.Info("Hello World")

}
Output:

level=info logger=main msg="Hello World" type=greeting

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewEncoder

func NewEncoder(cfg zapcore.EncoderConfig) zapcore.Encoder

NewEncoder creates an encoder writes logfmt formatted log entries.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL