zaplogfmt

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2020 License: MIT Imports: 13 Imported by: 0

README

Logfmt Encoder

This package implements logfmt for zap.

Usage

The encoder is simple to use.

package main

import (
	"os"

	"github.com/jsternberg/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")
}

To use RFC3339 output for the time instead of an integer timestamp, you can do this:

package main

import (
	"os"

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

func main() {
	config := zap.NewProductionEncoderConfig()
	config.EncodeTime = func(ts time.Time, encoder zapcore.PrimitiveArrayEncoder) {
		encoder.AppendString(ts.UTC().Format(time.RFC3339))
	}
	logger := zap.New(zapcore.NewCore(
		zaplogfmt.NewEncoder(config),
		os.Stdout,
		zapcore.DebugLevel,
	))
	logger.Info("Hello World")
}

Limitations

It is not possible to log an array, channel, function, map, slice, or struct. Functions and channels since they don't really have a suitable representation to begin with. Logfmt does not have a method of outputting arrays or maps so arrays, slices, maps, and structs cannot be rendered.

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

Index

Constants

This section is empty.

Variables

View Source
var ErrUnsupportedValueType = errors.New("unsupported value type")

Functions

func NewEncoder

func NewEncoder(cfg zapcore.EncoderConfig, opts ...Option) zapcore.Encoder

NewEncoder return a new encoder.

Types

type Option added in v1.4.0

type Option func(*config)

Option is a function which can be used to modify the default custom config.

func WithAlternativeCallerEncoder added in v1.4.0

func WithAlternativeCallerEncoder(encoder zapcore.CallerEncoder) Option

WithAlternativeCallerEncoder sets the caller encoder for logs which are less than the caller level. e.g. If set to the ShortCallerEncoder, the caller is still logged for logs which are less than the caller level using the abbreviated format.

func WithCallerLevel added in v1.4.0

func WithCallerLevel(level zapcore.Level) Option

WithCallerLevel sets the minimum log level for which the caller is logged. e.g. If it is set to WarnLevel the caller will be logged for only WarnLevel and above logs.

Jump to

Keyboard shortcuts

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