log

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2022 License: Apache-2.0 Imports: 13 Imported by: 45

Documentation

Overview

Copyright 2019 The arhat.dev Authors.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright 2019 The arhat.dev Authors.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

View Source
const (
	LevelVerbose = Level(zapcore.DebugLevel)
	LevelDebug   = Level(zapcore.InfoLevel)
	LevelInfo    = Level(zapcore.WarnLevel)
	LevelError   = Level(zapcore.ErrorLevel)
	LevelSilent  = Level(zapcore.FatalLevel + 1)
)

Log levels

View Source
const (
	TimeLayout = time.RFC3339Nano
)

TimeLayout used by this logger

Variables

This section is empty.

Functions

func FlagsForLogConfig

func FlagsForLogConfig(prefix string, c *Config) *pflag.FlagSet

func SetDefaultLogger

func SetDefaultLogger(cs ConfigSet) (err error)

Types

type Config

type Config struct {
	Level       string `json:"level" yaml:"level"`
	Format      string `json:"format" yaml:"format"`
	KubeLog     bool   `json:"kubeLog" yaml:"kubeLog"`
	Destination `json:",inline" yaml:",inline"`
}

type ConfigSet

type ConfigSet []Config

func (ConfigSet) GetUnique

func (cs ConfigSet) GetUnique() ConfigSet

func (ConfigSet) KubeLogFile

func (cs ConfigSet) KubeLogFile() string

type Destination

type Destination struct {
	File string `json:"file" yaml:"file"`
}

type Field

type Field = zapcore.Field

func Any

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

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 Array added in v0.10.0

func Array(key string, val zapcore.ArrayMarshaler) Field

Array constructs a field with the given key and ArrayMarshaler. It provides a flexible, but still type-safe and efficient, way to add array-like types to the logging context. The struct's MarshalLogArray method is called lazily.

func Binary

func Binary(key string, val []byte) Field

Binary constructs a field that carries an opaque binary blob.

Binary data is serialized in an encoding-appropriate format. For example, zap's JSON encoder base64-encodes binary blobs. To log UTF-8 encoded text, use ByteString.

func Bool

func Bool(key string, val bool) Field

Bool constructs a field that carries a bool.

func Boolp added in v0.10.0

func Boolp(key string, val *bool) Field

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

func Bools added in v0.10.0

func Bools(key string, bs []bool) Field

Bools constructs a field that carries a slice of bools.

func ByteString

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

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 ByteStrings added in v0.10.0

func ByteStrings(key string, bss [][]byte) Field

ByteStrings constructs a field that carries a slice of []byte, each of which must be UTF-8 encoded text.

func Complex128

func Complex128(key string, val complex128) Field

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 added in v0.10.0

func Complex128p(key string, val *complex128) Field

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

func Complex128s added in v0.10.0

func Complex128s(key string, nums []complex128) Field

Complex128s constructs a field that carries a slice of complex numbers.

func Complex64

func Complex64(key string, val complex64) Field

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 added in v0.10.0

func Complex64p(key string, val *complex64) Field

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

func Complex64s added in v0.10.0

func Complex64s(key string, nums []complex64) Field

Complex64s constructs a field that carries a slice of complex numbers.

func Duration

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

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

func Durationp added in v0.10.0

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

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

func Durations added in v0.10.0

func Durations(key string, ds []time.Duration) Field

Durations constructs a field that carries a slice of time.Durations.

func Error

func Error(err error) Field

Error is shorthand for the common idiom NamedError("error", err).

func Errors added in v0.10.0

func Errors(key string, errs []error) Field

Errors constructs a field that carries a slice of errors.

func Float32

func Float32(key string, val float32) Field

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 added in v0.10.0

func Float32p(key string, val *float32) Field

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

func Float32s added in v0.10.0

func Float32s(key string, nums []float32) Field

Float32s constructs a field that carries a slice of floats.

func Float64

func Float64(key string, val float64) Field

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 added in v0.10.0

func Float64p(key string, val *float64) Field

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

func Float64s added in v0.10.0

func Float64s(key string, nums []float64) Field

Float64s constructs a field that carries a slice of floats.

func Inline added in v0.10.0

func Inline(val zapcore.ObjectMarshaler) Field

Inline constructs a Field that is similar to Object, but it will add the elements of the provided ObjectMarshaler to the current namespace.

func Int

func Int(key string, val int) Field

Int constructs a field with the given key and value.

func Int16

func Int16(key string, val int16) Field

Int16 constructs a field with the given key and value.

func Int16p added in v0.10.0

func Int16p(key string, val *int16) Field

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

func Int16s added in v0.10.0

func Int16s(key string, nums []int16) Field

Int16s constructs a field that carries a slice of integers.

func Int32

func Int32(key string, val int32) Field

Int32 constructs a field with the given key and value.

func Int32p added in v0.10.0

func Int32p(key string, val *int32) Field

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

func Int32s added in v0.10.0

func Int32s(key string, nums []int32) Field

Int32s constructs a field that carries a slice of integers.

func Int64

func Int64(key string, val int64) Field

Int64 constructs a field with the given key and value.

func Int64p added in v0.10.0

func Int64p(key string, val *int64) Field

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

func Int64s added in v0.10.0

func Int64s(key string, nums []int64) Field

Int64s constructs a field that carries a slice of integers.

func Int8

func Int8(key string, val int8) Field

Int8 constructs a field with the given key and value.

func Int8p added in v0.10.0

func Int8p(key string, val *int8) Field

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

func Int8s added in v0.10.0

func Int8s(key string, nums []int8) Field

Int8s constructs a field that carries a slice of integers.

func Intp added in v0.10.0

func Intp(key string, val *int) Field

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

func Ints added in v0.10.0

func Ints(key string, nums []int) Field

Ints constructs a field that carries a slice of integers.

func NamedError

func NamedError(key string, err error) Field

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 added in v0.10.0

func Namespace(key string) Field

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 Object added in v0.10.0

func Object(key string, val zapcore.ObjectMarshaler) Field

Object constructs a field with the given key and ObjectMarshaler. It provides a flexible, but still type-safe and efficient, way to add map- or struct-like user-defined types to the logging context. The struct's MarshalLogObject method is called lazily.

func Reflect added in v0.10.0

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

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 added in v0.10.0

func Skip() Field

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

func Stack added in v0.10.0

func Stack(key string) Field

Stack constructs a field that stores a stacktrace of the current goroutine under provided key. Keep in mind that taking a stacktrace is eager and expensive (relatively speaking); this function both makes an allocation and takes about two microseconds.

func StackSkip added in v0.10.0

func StackSkip(key string, skip int) Field

StackSkip constructs a field similarly to Stack, but also skips the given number of frames from the top of the stacktrace.

func String

func String(key string, val string) Field

String constructs a field with the given key and value.

func StringError

func StringError(err string) Field

func Stringer added in v0.10.0

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

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 added in v0.10.0

func Stringp(key string, val *string) Field

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

func Strings

func Strings(key string, ss []string) Field

Strings constructs a field that carries a slice of strings.

func Time

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

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

func Timep added in v0.10.0

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

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

func Times added in v0.10.0

func Times(key string, ts []time.Time) Field

Times constructs a field that carries a slice of time.Times.

func Uint

func Uint(key string, val uint) Field

Uint constructs a field with the given key and value.

func Uint16

func Uint16(key string, val uint16) Field

Uint16 constructs a field with the given key and value.

func Uint16p added in v0.10.0

func Uint16p(key string, val *uint16) Field

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

func Uint16s added in v0.10.0

func Uint16s(key string, nums []uint16) Field

Uint16s constructs a field that carries a slice of unsigned integers.

func Uint32

func Uint32(key string, val uint32) Field

Uint32 constructs a field with the given key and value.

func Uint32p added in v0.10.0

func Uint32p(key string, val *uint32) Field

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

func Uint32s added in v0.10.0

func Uint32s(key string, nums []uint32) Field

Uint32s constructs a field that carries a slice of unsigned integers.

func Uint64

func Uint64(key string, val uint64) Field

Uint64 constructs a field with the given key and value.

func Uint64p added in v0.10.0

func Uint64p(key string, val *uint64) Field

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

func Uint64s added in v0.10.0

func Uint64s(key string, nums []uint64) Field

Uint64s constructs a field that carries a slice of unsigned integers.

func Uint8

func Uint8(key string, val uint8) Field

Uint8 constructs a field with the given key and value.

func Uint8p added in v0.10.0

func Uint8p(key string, val *uint8) Field

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

func Uint8s added in v0.10.0

func Uint8s(key string, nums []uint8) Field

Uint8s constructs a field that carries a slice of unsigned integers.

func Uintp added in v0.10.0

func Uintp(key string, val *uint) Field

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

func Uintptr

func Uintptr(key string, val uintptr) Field

Uintptr constructs a field with the given key and value.

func Uintptrp added in v0.10.0

func Uintptrp(key string, val *uintptr) Field

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

func Uintptrs added in v0.10.0

func Uintptrs(key string, us []uintptr) Field

Uintptrs constructs a field that carries a slice of pointer addresses.

func Uints added in v0.10.0

func Uints(key string, nums []uint) Field

Uints constructs a field that carries a slice of unsigned integers.

type Interface

type Interface interface {
	// Enabled check if level is enabled
	Enabled(level Level) bool

	// WithName return a logger which shares config and runtime but with different name
	WithName(name string) Interface

	// WithFields return a logger with predefined fields
	WithFields(fields ...Field) Interface

	// V is the verbose level, should be used in library and not useful or untested stuff
	// such as showing a call started, printing some internal values for inspection
	V(msg string, fields ...Field)

	// D is the debug level, should be used for information
	D(msg string, fields ...Field)

	// I is the info level, should be used to indicate application state, show important messages
	I(msg string, fields ...Field)

	// E is the error level, should only be used to report unexpected or fatal error, will print
	// a stacktrace when used
	E(msg string, fields ...Field)

	// Flush logger
	Flush() error
}
var (
	Log        Interface
	NoOpLogger Interface
)

Global log instance

type Level

type Level zapcore.Level

func (Level) String

func (l Level) String() string

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

func New

func New(name string, config ConfigSet) (*Logger, error)

func (*Logger) D

func (l *Logger) D(msg string, fields ...Field)

D debug

func (*Logger) E

func (l *Logger) E(msg string, fields ...Field)

E error

func (*Logger) Enabled

func (l *Logger) Enabled(level Level) bool

func (*Logger) Flush

func (l *Logger) Flush() error

Flush log write

func (*Logger) I

func (l *Logger) I(msg string, fields ...Field)

I info

func (*Logger) V

func (l *Logger) V(msg string, fields ...Field)

V verbose

func (*Logger) WithFields

func (l *Logger) WithFields(fields ...Field) Interface

func (*Logger) WithName

func (l *Logger) WithName(name string) Interface

type Structure

type Structure struct {
	Msg        string `json:"M,omitempty"`
	Level      string `json:"L,omitempty"`
	Time       string `json:"T,omitempty"`
	Name       string `json:"N,omitempty"`
	Caller     string `json:"C,omitempty"`
	Stacktrace string `json:"S,omitempty"`
}

Jump to

Keyboard shortcuts

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