zaplogfmt

package module
v0.0.0-...-9593231 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2023 License: MIT Imports: 15 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 CapitalColorFullNameEncoder

func CapitalColorFullNameEncoder(loggerName string, enc zapcore.PrimitiveArrayEncoder)

CapitalColorFullNameEncoder serializes the logger name to an all-caps string and adds color. For example, gossip is serialized to "GOSSIP" and colored white.

func CapitalFullNameEncoder

func CapitalFullNameEncoder(loggerName string, enc zapcore.PrimitiveArrayEncoder)

CapitalFullNameEncoder serializes the logger name to an all-caps string. For example, gossip is serialized to "GOSSIP".

func ColorEpochTimeEncoder

func ColorEpochTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder)

ColorEpochTimeEncoder serializes a time.Time to an integer representing the number of seconds since the Unix epoch and adds color.

func EpochTimeEncoder

func EpochTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder)

EpochTimeEncoder serializes a time.Time to an integer representing the number of seconds since the Unix epoch.

func NewDefaultEncoderConfig

func NewDefaultEncoderConfig() zapcore.EncoderConfig

func NewEncoder

func NewEncoder(cfg zapcore.EncoderConfig) zapcore.Encoder

Types

This section is empty.

Jump to

Keyboard shortcuts

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