xlogcore

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2021 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package xlogcore defines xlog interface.

Index

Constants

View Source
const (
	// DebugLevel logs are typically voluminous, and are usually disabled in
	// production.
	DebugLevel int8 = iota - 1
	// InfoLevel is the default logging priority.
	InfoLevel
	// WarnLevel logs are more important than Info, but don't need individual
	// human review.
	WarnLevel
	// ErrorLevel logs are high-priority. If an application is running smoothly,
	// it shouldn't generate any error-level logs.
	ErrorLevel
	// PanicLevel logs a message, then panics.
	PanicLevel
	// FatalLevel logs a message, then calls os.Exit(1).
	FatalLevel
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Driver supports zap. It defaults to zap.
	Driver string `yaml:"driver"`
	// Level from -1 to 4
	Level int8 `yaml:"level"`
	// Encoding sets the logger's encoding. Valid values are "json" and "console"
	Encoding string `yaml:"encoding"`
	// FileName is the name of the log file. if the FileName is empty.
	FileName string `yaml:"file_name"`
	// RotateMaxSize is the maximum size in megabytes of the log file before it gets
	// rotated. It defaults to 100 megabytes.
	RotationMaxSize int `yaml:"rotation_max_size"`
	// RetainMaxAge is the maximum number of days to retain old log files based on the
	// timestamp encoded in their filename.  Note that a day is defined as 24
	// hours and may not exactly correspond to calendar days due to daylight
	// savings, leap seconds, etc. The default is not to remove old log files
	// based on age.
	RetainMaxAge int `yaml:"retain_max_age"`
	// RetainMaxBackups is the maximum number of old log files to retain.  The default
	// is to retain all old log files (though RetainMaxAge may still cause them to get
	// deleted.)
	RetainMaxBackups int `yaml:"retain_max_backups"`
	// RetainLocalTime determines if the time used for formatting the timestamps in
	// backup files is the computer's local time.  The default is to use UTC
	// time.
	RetainLocalTime bool `yaml:"retain_local_time"`
	// RetainCompress determines if the rotated log files should be compressed
	// using gzip. The default is not to perform compression.
	RetainCompress bool `yaml:"retain_compress"`
}

Config configures a Logger when creating.

type FastXLog

type FastXLog interface {
	// Sync flushes buffered logs. Users should call Sync before exiting.
	Sync()

	// XLog converting FastXLog to XLog. The operation is quite inexpensive.
	XLog() XLog
	// WithOptions clones the current Logger, applies the supplied Options, and
	// returns the resulting Logger. It's safe to use concurrently.
	WithOptions(opts ...Option) FastXLog
	// AddCallerSkip increases the number of callers skipped by caller annotation.
	AddCallerSkip(n int) FastXLog

	// With adds a variadic number of fields to the logging context.
	With(kvs ...KeyVal) FastXLog

	// ToCtx adds the FastXLog, with a variadic number of fields, to ctx and
	// returns the resulting context.Context.
	ToCtx(ctx context.Context, kvs ...KeyVal) context.Context
	// FromCtx gets the FastXLog from the ctx.
	FromCtx(ctx context.Context) FastXLog

	// Debug construct and log a Debug message.
	Debug(msg string)
	// Info construct and log a Info message.
	Info(msg string)
	// Warn construct and log a Warn message.
	Warn(msg string)
	// Error construct and log a Error message.
	Error(msg string)
	// Panic construct and log a Panic message, then panics.
	Panic(msg string)
	// Fatal construct and log a Fatal message, then calls os.Exit(1).
	Fatal(msg string)
}

FastXLog provides fast, leveled, structured logging. All methods are safe for concurrent use. Xlog can be converted to a FastXLog with its Fast method.

type KeyVal

type KeyVal struct {
	Key       string
	Type      KeyValType
	Integer   int64
	String    string
	Interface interface{}
}

func Any

func Any(key string, value interface{}) KeyVal

Any takes a key and an arbitrary value and chooses the best way to represent them as a field, falling back to a reflection-based approach only if necessary.

Since byte/uint8 and rune/int32 are aliases, Any can't differentiate between them. To minimize surprises, []byte values are treated as binary blobs, byte values are treated as uint8, and runes are always treated as integers.

func Bool

func Bool(key string, val bool) KeyVal

Bool constructs a field that carries a bool.

func Boolp

func Boolp(key string, val *bool) KeyVal

Boolp constructs a field that carries a *bool. The returned KeyVal will safely and explicitly represent `nil` when appropriate.

func ByteString

func ByteString(key string, val []byte) KeyVal

ByteString constructs a field that carries UTF-8 encoded text as a []byte. To log opaque binary blobs (which aren't necessarily valid UTF-8), use Binary.

func Complex128

func Complex128(key string, val complex128) KeyVal

Complex128 constructs a field that carries a complex number. Unlike most numeric fields, this costs an allocation (to convert the complex128 to interface{}).

func Complex128p

func Complex128p(key string, val *complex128) KeyVal

Complex128p constructs a field that carries a *complex128. The returned KeyVal will safely and explicitly represent `nil` when appropriate.

func Complex64

func Complex64(key string, val complex64) KeyVal

Complex64 constructs a field that carries a complex number. Unlike most numeric fields, this costs an allocation (to convert the complex64 to interface{}).

func Complex64p

func Complex64p(key string, val *complex64) KeyVal

Complex64p constructs a field that carries a *complex64. The returned KeyVal will safely and explicitly represent `nil` when appropriate.

func Duration

func Duration(key string, val time.Duration) KeyVal

Duration constructs a field with the given key and value. The encoder controls how the duration is serialized.

func Durationp

func Durationp(key string, val *time.Duration) KeyVal

Durationp constructs a field that carries a *time.Duration. The returned KeyVal will safely and explicitly represent `nil` when appropriate.

func Float32

func Float32(key string, val float32) KeyVal

Float32 constructs a field that carries a float32. The way the floating-point value is represented is encoder-dependent, so marshaling is necessarily lazy.

func Float32p

func Float32p(key string, val *float32) KeyVal

Float32p constructs a field that carries a *float32. The returned KeyVal will safely and explicitly represent `nil` when appropriate.

func Float64

func Float64(key string, val float64) KeyVal

Float64 constructs a field that carries a float64. The way the floating-point value is represented is encoder-dependent, so marshaling is necessarily lazy.

func Float64p

func Float64p(key string, val *float64) KeyVal

Float64p constructs a field that carries a *float64. The returned KeyVal will safely and explicitly represent `nil` when appropriate.

func Int

func Int(key string, val int) KeyVal

Int constructs a field with the given key and value.

func Int16

func Int16(key string, val int16) KeyVal

Int16 constructs a field with the given key and value.

func Int16p

func Int16p(key string, val *int16) KeyVal

Int16p constructs a field that carries a *int16. The returned KeyVal will safely and explicitly represent `nil` when appropriate.

func Int32

func Int32(key string, val int32) KeyVal

Int32 constructs a field with the given key and value.

func Int32p

func Int32p(key string, val *int32) KeyVal

Int32p constructs a field that carries a *int32. The returned KeyVal will safely and explicitly represent `nil` when appropriate.

func Int64

func Int64(key string, val int64) KeyVal

Int64 constructs a field with the given key and value.

func Int64p

func Int64p(key string, val *int64) KeyVal

Int64p constructs a field that carries a *int64. The returned KeyVal will safely and explicitly represent `nil` when appropriate.

func Int8

func Int8(key string, val int8) KeyVal

Int8 constructs a field with the given key and value.

func Int8p

func Int8p(key string, val *int8) KeyVal

Int8p constructs a field that carries a *int8. The returned KeyVal will safely and explicitly represent `nil` when appropriate.

func Intp

func Intp(key string, val *int) KeyVal

Intp constructs a field that carries a *int. The returned KeyVal will safely and explicitly represent `nil` when appropriate.

func NamedError

func NamedError(key string, err error) KeyVal

NamedError constructs a field that lazily stores err.Error() under the provided key. Errors which also implement fmt.Formatter (like those produced by github.com/pkg/errors) will also have their verbose representation stored under key+"Verbose". If passed a nil error, the field is a no-op.

For the common case in which the key is simply "error", the Error function is shorter and less repetitive.

func Namespace

func Namespace(key string) KeyVal

Namespace creates a named, isolated scope within the logger's context. All subsequent fields will be added to the new namespace.

This helps prevent key collisions when injecting loggers into sub-components or third-party libraries.

func Reflect

func Reflect(key string, val interface{}) KeyVal

Reflect constructs a field with the given key and an arbitrary object. It uses an encoding-appropriate, reflection-based function to lazily serialize nearly any object into the logging context, but it's relatively slow and allocation-heavy. Outside tests, Any is always a better choice.

If encoding fails (e.g., trying to serialize a map[int]string to JSON), Reflect includes the error message in the final log output.

func Skip

func Skip() KeyVal

Skip constructs a no-op field, which is often useful when handling invalid inputs in other Field constructors.

func String

func String(key string, val string) KeyVal

String constructs a field with the given key and value.

func Stringer

func Stringer(key string, val fmt.Stringer) KeyVal

Stringer constructs a field with the given key and the output of the value's String method. The Stringer's String method is called lazily.

func Stringp

func Stringp(key string, val *string) KeyVal

Stringp constructs a field that carries a *string. The returned KeyVal will safely and explicitly represent `nil` when appropriate.

func Time

func Time(key string, val time.Time) KeyVal

Time constructs a KeyVal with the given key and value. The encoder controls how the time is serialized.

func Timep

func Timep(key string, val *time.Time) KeyVal

Timep constructs a field that carries a *time.Time. The returned KeyVal will safely and explicitly represent `nil` when appropriate.

func Uint

func Uint(key string, val uint) KeyVal

Uint constructs a field with the given key and value.

func Uint16

func Uint16(key string, val uint16) KeyVal

Uint16 constructs a field with the given key and value.

func Uint16p

func Uint16p(key string, val *uint16) KeyVal

Uint16p constructs a field that carries a *uint16. The returned KeyVal will safely and explicitly represent `nil` when appropriate.

func Uint32

func Uint32(key string, val uint32) KeyVal

Uint32 constructs a field with the given key and value.

func Uint32p

func Uint32p(key string, val *uint32) KeyVal

Uint32p constructs a field that carries a *uint32. The returned KeyVal will safely and explicitly represent `nil` when appropriate.

func Uint64

func Uint64(key string, val uint64) KeyVal

Uint64 constructs a field with the given key and value.

func Uint64p

func Uint64p(key string, val *uint64) KeyVal

Uint64p constructs a field that carries a *uint64. The returned KeyVal will safely and explicitly represent `nil` when appropriate.

func Uint8

func Uint8(key string, val uint8) KeyVal

Uint8 constructs a field with the given key and value.

func Uint8p

func Uint8p(key string, val *uint8) KeyVal

Uint8p constructs a field that carries a *uint8. The returned KeyVal will safely and explicitly represent `nil` when appropriate.

func Uintp

func Uintp(key string, val *uint) KeyVal

Uintp constructs a field that carries a *uint. The returned KeyVal will safely and explicitly represent `nil` when appropriate.

func Uintptr

func Uintptr(key string, val uintptr) KeyVal

Uintptr constructs a field with the given key and value.

func Uintptrp

func Uintptrp(key string, val *uintptr) KeyVal

Uintptrp constructs a field that carries a *uintptr. The returned KeyVal will safely and explicitly represent `nil` when appropriate.

type KeyValType

type KeyValType uint8

A KeyValType indicates which member of the KeyVal union struct should be used and how it should be serialized.

const (
	// UnknownType is the default field type. Attempting to add it to an encoder will panic.
	UnknownType KeyValType = iota
	// ArrayMarshalerType indicates that the field carries an ArrayMarshaler.
	ArrayMarshalerType
	// ObjectMarshalerType indicates that the field carries an ObjectMarshaler.
	ObjectMarshalerType
	// BinaryType indicates that the field carries an opaque binary blob.
	BinaryType
	// BoolType indicates that the field carries a bool.
	BoolType
	// ByteStringType indicates that the field carries UTF-8 encoded bytes.
	ByteStringType
	// Complex128Type indicates that the field carries a complex128.
	Complex128Type
	// Complex64Type indicates that the field carries a complex128.
	Complex64Type
	// DurationType indicates that the field carries a time.Duration.
	DurationType
	// Float64Type indicates that the field carries a float64.
	Float64Type
	// Float32Type indicates that the field carries a float32.
	Float32Type
	// Int64Type indicates that the field carries an int64.
	Int64Type
	// Int32Type indicates that the field carries an int32.
	Int32Type
	// Int16Type indicates that the field carries an int16.
	Int16Type
	// Int8Type indicates that the field carries an int8.
	Int8Type
	// StringType indicates that the field carries a string.
	StringType
	// TimeType indicates that the field carries a time.Time that is
	// representable by a UnixNano() stored as an int64.
	TimeType
	// TimeFullType indicates that the field carries a time.Time stored as-is.
	TimeFullType
	// Uint64Type indicates that the field carries a uint64.
	Uint64Type
	// Uint32Type indicates that the field carries a uint32.
	Uint32Type
	// Uint16Type indicates that the field carries a uint16.
	Uint16Type
	// Uint8Type indicates that the field carries a uint8.
	Uint8Type
	// UintptrType indicates that the field carries a uintptr.
	UintptrType
	// ReflectType indicates that the field carries an interface{}, which should
	// be serialized using reflection.
	ReflectType
	// NamespaceType signals the beginning of an isolated namespace. All
	// subsequent fields should be added to the new namespace.
	NamespaceType
	// StringerType indicates that the field carries a fmt.Stringer.
	StringerType
	// ErrorType indicates that the field carries an error.
	ErrorType
	// SkipType indicates that the field is a no-op.
	SkipType
)

type Option

type Option func(*Options)

Option configures Options.

func OptionWithAddCallerSkip

func OptionWithAddCallerSkip(n int) Option

OptionWithAddCallerSkip increases the number of callers skipped by caller annotation.

type Options

type Options struct {
	AddCallerSkip *int
}

Options configures a Logger.

type XLog

type XLog interface {
	// Sync flushes buffered logs. Users should call Sync before exiting.
	Sync()

	// Fast converting XLog to FastXLog. The operation is quite inexpensive.
	Fast() FastXLog
	// WithOptions clones the current Logger, applies the supplied Options, and
	// returns the resulting Logger. It's safe to use concurrently.
	WithOptions(opts ...Option) XLog
	// AddCallerSkip increases the number of callers skipped by caller annotation.
	AddCallerSkip(n int) XLog

	// With adds a variadic number of fields to the logging context.
	With(kvs ...interface{}) XLog

	// ToCtx adds the XLog, with a variadic number of fields, to ctx and
	// returns the resulting context.Context.
	ToCtx(ctx context.Context, kvs ...interface{}) context.Context
	// FromCtx gets the XLog from the ctx.
	FromCtx(ctx context.Context) XLog

	// Debug construct and log a Debug message.
	Debug(args ...interface{})
	// Debugf construct and log a Debug message.
	Debugf(tpl string, args ...interface{})
	// Info construct and log a Info message.
	Info(args ...interface{})
	// Infof construct and log a Info message.
	Infof(tpl string, args ...interface{})
	// Warn construct and log a Warn message.
	Warn(args ...interface{})
	// Warnf construct and log a Warn message.
	Warnf(tpl string, args ...interface{})
	// Error construct and log a Error message.
	Error(args ...interface{})
	// Errorf construct and log a Error message.
	Errorf(tpl string, args ...interface{})
	// Panic construct and log a Panic message, then panics.
	Panic(args ...interface{})
	// Panicf construct and log a Panic message, then panics.
	Panicf(tpl string, args ...interface{})
	// Fatal construct and log a Fatal message, then calls os.Exit(1).
	Fatal(args ...interface{})
	// Fatalf construct and log a Fatal message, then calls os.Exit(1).
	Fatalf(tpl string, args ...interface{})
}

Xlog provide a more ergonomic, but slightly slower, API. FastXLog can be converted to a Xlog with its Xlog method.

Jump to

Keyboard shortcuts

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