log

package
v0.0.0-...-4125756 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package log provides function for managing structured logs. You can create a new logger by calling the New function

// Create a production logger with Info level.
logger, _ := log.New()
defer logger.Close()

// Create a production logger with Debug level
logger, _ := log.New(log.WithLevel(log.DebugLevel))
defer logger.Close()

Index

Constants

View Source
const (
	UnknownType         = FieldType(zapcore.UnknownType)
	ArrayMarshalerType  = FieldType(zapcore.ArrayMarshalerType)
	ObjectMarshalerType = FieldType(zapcore.ObjectMarshalerType)
	BinaryType          = FieldType(zapcore.BinaryType)
	BoolType            = FieldType(zapcore.BoolType)
	ByteStringType      = FieldType(zapcore.ByteStringType)
	Complex128Type      = FieldType(zapcore.Complex128Type)
	Complex64Type       = FieldType(zapcore.Complex64Type)
	DurationType        = FieldType(zapcore.DurationType)
	Float64Type         = FieldType(zapcore.Float64Type)
	Float32Type         = FieldType(zapcore.Float32Type)
	Int64Type           = FieldType(zapcore.Int64Type)
	Int32Type           = FieldType(zapcore.Int32Type)
	Int16Type           = FieldType(zapcore.Int16Type)
	Int8Type            = FieldType(zapcore.Int8Type)
	StringType          = FieldType(zapcore.StringType)
	TimeType            = FieldType(zapcore.TimeType)
	Uint64Type          = FieldType(zapcore.Uint64Type)
	Uint32Type          = FieldType(zapcore.Uint32Type)
	Uint16Type          = FieldType(zapcore.Uint16Type)
	Uint8Type           = FieldType(zapcore.Uint8Type)
	UintptrType         = FieldType(zapcore.UintptrType)
	ReflectType         = FieldType(zapcore.ReflectType)
	NamespaceType       = FieldType(zapcore.NamespaceType)
	StringerType        = FieldType(zapcore.StringerType)
	ErrorType           = FieldType(zapcore.ErrorType)
	SkipType            = FieldType(zapcore.SkipType)
)

Field types

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

Variables

This section is empty.

Functions

func ReplaceGlobal

func ReplaceGlobal(logger *Logger) func()

ReplaceGlobal replaces the global Logger and returns a function to restore the original values. It's safe for concurrent use.

func WithLevel

func WithLevel(level Level) func(*Option)

WithLevel set up the logger log level.

Types

type Field

type Field = zap.Field

A Field is a named piece of data added to a log message.

func Any

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

Any field.

func Bool

func Bool(key string, val bool) Field

Bool field.

func ByteString

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

ByteString field.

func Duration

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

Duration field.

func Error

func Error(err error) Field

Error field.

func Float32

func Float32(key string, val float32) Field

Float32 field.

func Float64

func Float64(key string, val float64) Field

Float64 field.

func Int

func Int(key string, val int) Field

Int field.

func Int32

func Int32(key string, val int32) Field

Int32 field.

func Int64

func Int64(key string, val int64) Field

Int64 field.

func String

func String(key string, val string) Field

String field.

func Stringer

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

Stringer field.

func Strings

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

Strings field

func Time

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

Time field.

func Uint

func Uint(key string, val uint) Field

Uint field.

func Uint32

func Uint32(key string, val uint32) Field

Uint32 field.

func Uint64

func Uint64(key string, val uint64) Field

Uint64 field.

type FieldType

type FieldType uint8

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

type Level

type Level int8

A Level is a logging priority. Higher levels are more important.

type Logger

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

A Logger provides fast, leveled, structured logging. All methods are safe for concurrent use.

func L

func L() *Logger

L returns the global Logger, which can be reconfigured with ReplaceGlobal. By default, global logger is set to no-op. It's safe for concurrent use.

func New

func New(opts ...func(*Option)) (*Logger, error)

New is a reasonable production logging configuration. Logging is enabled at InfoLevel and above by default.

It uses a JSON encoder, writes to standard error, and enables sampling. Stacktraces are automatically included on logs of ErrorLevel and above.

func NewNop

func NewNop() *Logger

NewNop returns a no-op Logger. It never writes out logs or internal errors, and it never runs user-defined hooks.

func (*Logger) Close

func (l *Logger) Close()

Close is flushing any buffered log entries. Applications should take care to call Close before exiting.

func (*Logger) Debug

func (l *Logger) Debug(ctx context.Context, message string, fields ...Field)

Debug logs a message at DebugLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

func (*Logger) Error

func (l *Logger) Error(ctx context.Context, message string, fields ...Field)

Error logs a message at ErrorLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

func (*Logger) Fatal

func (l *Logger) Fatal(ctx context.Context, message string, fields ...Field)

Fatal logs a message at FatalLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

The logger then calls os.Exit(1), even if logging at FatalLevel is disabled.

func (*Logger) Info

func (l *Logger) Info(ctx context.Context, message string, fields ...Field)

Info logs a message at InfoLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

func (*Logger) Warn

func (l *Logger) Warn(ctx context.Context, message string, fields ...Field)

Warn logs a message at WarnLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

type Option

type Option struct {
	Level Level
}

Option provide a set of optional configuration that can be provided when creating a logger.

Jump to

Keyboard shortcuts

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