ecszap

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2023 License: Apache-2.0 Imports: 5 Imported by: 68

README

Build Status

Elastic Common Schema (ECS) support for uber-go/zap logger

Use this library for automatically adding a minimal set of ECS fields to your logs, when using uber-go/zap.

Documentation

Ready to get started? Documentation is at elastic.co.

Test

go test ./...

Contribute

Create a Pull Request from your own fork.

Run ./lint.sh to update and format you changes before submitting.

Add new dependencies to the NOTICE.txt.

License

This software is licensed under the Apache 2 license.

Documentation

Overview

Package ecszap provides a formatter to be used with the go.uber.org/zap logger library that helps creating Elastic Common Schema (ECS) conformant log entries.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ECSCompatibleEncoderConfig added in v0.2.0

func ECSCompatibleEncoderConfig(cfg zapcore.EncoderConfig) zapcore.EncoderConfig

ECSCompatibleEncoderConfig takes an existing zapcore.EncoderConfig and sets ECS relevant configuration options to ECS conformant values. The returned zapcore.EncoderConfig can be used to create an ECS conformant encoder. Be aware that this will always replace any set EncodeCaller function with the ecszap.ShortCallerEncoder. This is a pure convenience function for making a transition from existing an zap logger to an ECS conformant zap loggers easier. It is recommended to make use of the ecszap.EncoderConfig whenever possible.

func EpochMicrosTimeEncoder

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

EpochMicrosTimeEncoder encodes a given time in microseconds.

func FullCallerEncoder

func FullCallerEncoder(c zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder)

FullCallerEncoder serializes the file name, line and function from the caller in an ECS compliant way; serializing the full path of the file name using the underlying zapcore.EntryCaller.

func NewCore

NewCore creates a zapcore.Core that uses an ECS conformant JSON encoder. This is the safest way to create an ECS compatible core.

func ShortCallerEncoder

func ShortCallerEncoder(c zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder)

ShortCallerEncoder serializes the file name, line and function from the caller in an ECS compliant way; removing everything except the final directory from the file name by calling the underlying zapcore.EntryCaller TrimmedPath().

func WrapCore

func WrapCore(c zapcore.Core) zapcore.Core

WrapCore wraps a core with ECS core functionality and returns a zapcore.Core. For ECS compatibility, ensure that the wrapped zapcore.Core uses an encoder that is created from an ECS compatible configuration. For further details check out ecszap.EncoderConfig or ecszap.ECSCompatibleEncoderConfig.

func WrapCoreOption added in v1.0.0

func WrapCoreOption() zap.Option

WrapCoreOption returns a zap.Option, wrapping the underlying zapcore.Core.

Types

type CallerEncoder

type CallerEncoder func(zapcore.EntryCaller, zapcore.PrimitiveArrayEncoder)

CallerEncoder is equivalent to zapcore.CallerEncoder, except that its UnmarshalText method uses FullCallerEncoder and ShortCallerEncoder from this package instead, in order to encode callers in the ECS format.

func (*CallerEncoder) UnmarshalText

func (e *CallerEncoder) UnmarshalText(text []byte) error

UnmarshalText creates a CallerEncoder function, `full` is unmarshalled to FullCallerEncoder, defaults to ShortCallerEncoder,

type EncoderConfig

type EncoderConfig struct {
	// EnableName controls if a logger's name should be serialized
	// when available. If enabled, the EncodeName configuration is
	// used for serialization.
	EnableName bool `json:"enableName" yaml:"enableName"`

	// EnableStackTrace controls if a stack trace should be serialized when available.
	EnableStackTrace bool `json:"enableStackTrace" yaml:"enableStackTrace"`

	// EnableCaller controls if the entry caller should be serialized.
	// If enabled, the EncodeCaller configuration is used for serialization.
	EnableCaller bool `json:"enableCaller" yaml:"enableCaller"`

	// LineEnding defines the string used for line endings.
	LineEnding string `json:"lineEnding" yaml:"lineEnding"`

	// EncodeName defines how to encode a loggers name.
	// It will only be applied if EnableName is set to true.
	EncodeName zapcore.NameEncoder `json:"nameEncoder" yaml:"nameEncoder"`

	// EncodeLevel sets the log level for which any context should be logged.
	EncodeLevel zapcore.LevelEncoder `json:"levelEncoder" yaml:"levelEncoder"`

	// EncodeDuration sets the format for encoding time.Duration values.
	EncodeDuration zapcore.DurationEncoder `json:"durationEncoder" yaml:"durationEncoder"`

	// EncodeCaller defines how an entry caller should be serialized.
	// It will only be applied if EnableCaller is set to true.
	EncodeCaller CallerEncoder `json:"callerEncoder" yaml:"callerEncoder"`
}

EncoderConfig exports all non ECS related configurable settings. The configuration can be used to create an ECS compatible zapcore.Core

func NewDefaultEncoderConfig

func NewDefaultEncoderConfig() EncoderConfig

NewDefaultEncoderConfig returns an EncoderConfig with default settings.

func (EncoderConfig) ToZapCoreEncoderConfig added in v0.2.0

func (cfg EncoderConfig) ToZapCoreEncoderConfig() zapcore.EncoderConfig

ToZapCoreEncoderConfig transforms the ecszap.EncoderConfig into a zapcore.EncoderConfig

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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