log

package
v1.9.3 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2025 License: MIT Imports: 16 Imported by: 7

Documentation

Overview

nolint:forcetypeassert,gochecknoglobals,mnd

nolint:exhaustive,dupl

nolint:err113

nolint:exhaustive,dupl

nolint:gochecknoglobals,mnd

Index

Constants

View Source
const (
	FieldKeyTime  = "time"
	FieldKeyLevel = "level"
	FieldKeyMsg   = "msg"

	FieldKeyLogError = "error"
	FieldKeyFunc     = "func"
	FieldKeyFile     = "file"
)

Default field names for the default fields

Variables

This section is empty.

Functions

func LogLevelFuncForError

func LogLevelFuncForError(err error, logger Logger) func(ctx context.Context, message any, fields ...Field)

nolint:exhaustive

func StdLoggerWithLevel

func StdLoggerWithLevel(adapter Logger, level Level, withFields ...Field) *stdlog.Logger

func ToContext

func ToContext(ctx context.Context, fields ...Field) context.Context

Types

type Adapter

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

func Default added in v1.4.0

func Default() *Adapter

func New added in v1.4.0

func New(options ...Option) *Adapter

func (*Adapter) Close added in v1.4.0

func (l *Adapter) Close() error

func (*Adapter) Debug

func (l *Adapter) Debug(ctx context.Context, msg any, fields ...Field)

func (*Adapter) Error

func (l *Adapter) Error(ctx context.Context, msg any, fields ...Field)

func (*Adapter) Fatal

func (l *Adapter) Fatal(ctx context.Context, msg any, fields ...Field)

func (*Adapter) Info

func (l *Adapter) Info(ctx context.Context, msg any, fields ...Field)

func (*Adapter) IsLevelEnabled

func (l *Adapter) IsLevelEnabled(level Level) bool

func (*Adapter) Level

func (l *Adapter) Level() Level

func (*Adapter) Log

func (l *Adapter) Log(ctx context.Context, level Level, msg any, fields ...Field)

Log will log a message at the level given as parameter.

func (*Adapter) Panic

func (l *Adapter) Panic(ctx context.Context, msg any, fields ...Field)

func (*Adapter) Print

func (l *Adapter) Print(ctx context.Context, msg any, fields ...Field)

func (*Adapter) SetLogLevel added in v1.5.1

func (l *Adapter) SetLogLevel(lvl Level)

func (*Adapter) Trace

func (l *Adapter) Trace(ctx context.Context, msg any, fields ...Field)

func (*Adapter) Warn

func (l *Adapter) Warn(ctx context.Context, msg any, fields ...Field)

type Encoder added in v1.6.0

type Encoder interface {
	Encode(field ...Field) ([]byte, error)
}

type Field

type Field struct {
	Name string
	Type FieldType

	Int       int64
	String    string
	Interface any
}

func Any

func Any(name string, value any) Field

nolint:cyclop,gocyclo,funlen,inamedparam,exhaustive

func Bool added in v1.5.0

func Bool(name string, value bool) Field

func Bools added in v1.6.0

func Bools(name string, values []bool) Field

func ByteString

func ByteString(name string, value []byte) Field

for UTF-8 encoded string

func ContextLogValues

func ContextLogValues(ctx context.Context) []Field

func Dictionary added in v1.6.0

func Dictionary(name string, value map[string]any) Field

func Duration added in v1.5.0

func Duration(name string, value time.Duration) Field

func Durations added in v1.6.0

func Durations(name string, values []time.Duration) Field

func Error added in v1.5.0

func Error(err error) Field

func Float32 added in v1.6.0

func Float32(name string, value float32) Field

func Float64 added in v1.5.0

func Float64(name string, value float64) Field

func Floats32 added in v1.6.0

func Floats32(name string, values []float32) Field

func Floats64 added in v1.6.0

func Floats64(name string, values []float64) Field

func Int added in v1.5.0

func Int(name string, value int) Field

func Int16 added in v1.6.0

func Int16(name string, value int16) Field

func Int32 added in v1.6.0

func Int32(name string, value int32) Field

func Int64 added in v1.5.0

func Int64(name string, value int64) Field

func Int8 added in v1.6.0

func Int8(name string, value int8) Field

func Ints added in v1.6.0

func Ints(name string, values []int) Field

func Ints16 added in v1.6.0

func Ints16(name string, values []int16) Field

func Ints32 added in v1.6.0

func Ints32(name string, values []int32) Field

func Ints64 added in v1.6.0

func Ints64(name string, values []int64) Field

func Ints8 added in v1.6.0

func Ints8(name string, values []int8) Field

func Object added in v1.6.0

func Object(name string, values any) Field

func Objects added in v1.6.0

func Objects(name string, values []any) Field

func String added in v1.4.0

func String(name string, value string) Field

func Stringer added in v1.5.0

func Stringer(name string, value fmt.Stringer) Field

func Strings added in v1.6.0

func Strings(name string, values []string) Field

func Time added in v1.5.0

func Time(name string, value time.Time) Field

func Times added in v1.6.0

func Times(name string, values []time.Time) Field

func Uint added in v1.5.0

func Uint(name string, value uint) Field

func Uint16 added in v1.6.0

func Uint16(name string, value uint16) Field

func Uint32 added in v1.6.0

func Uint32(name string, value uint32) Field

func Uint64 added in v1.5.0

func Uint64(name string, value uint64) Field

func Uint8 added in v1.6.0

func Uint8(name string, value uint8) Field

func Uints added in v1.6.0

func Uints(name string, values []uint) Field

func Uints16 added in v1.6.0

func Uints16(name string, values []uint16) Field

func Uints32 added in v1.6.0

func Uints32(name string, values []uint32) Field

func Uints64 added in v1.6.0

func Uints64(name string, values []uint64) Field

func Uints8 added in v1.6.0

func Uints8(name string, values []uint8) Field

type FieldType added in v1.6.0

type FieldType uint8

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

const (
	UnknownType FieldType = iota
	BoolType

	Float32Type
	Float64Type
	IntType
	UintType

	StringType
	TimeType
	DurationType
	ObjectType

	ErrorType

	ArrBoolType
	ArrFloat32Type
	ArrFloat64Type
	ArrIntType
	ArrUintType

	ArrStringType
	ArrTimeType
	ArrDurationType
	ArrObjectType

	ReflectType
	ArrReflectType
	DictionaryType
)

type JsonEncoder added in v1.6.0

type JsonEncoder struct{}

func (JsonEncoder) Encode added in v1.6.0

func (j JsonEncoder) Encode(fields ...Field) ([]byte, error)

type Level

type Level uint32 // nolint:recvcheck
const (
	// PanicLevel level, highest level of severity. Logs and then calls panic with the
	// message passed to Debug, Info, ...
	PanicLevel Level = iota
	// FatalLevel level. Logs and then calls `logger.Exit(1)`. It will exit even if the
	// logging level is set to Panic.
	FatalLevel
	// ErrorLevel level. Logs. Used for errors that should definitely be noted.
	// Commonly used for hooks to send errors to an error tracking service.
	ErrorLevel
	// WarnLevel level. Non-critical entries that deserve eyes.
	WarnLevel
	// InfoLevel level. General operational entries about what's going on inside the
	// application.
	InfoLevel
	// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
	DebugLevel
	// TraceLevel level. Designates finer-grained informational events than the Debug.
	TraceLevel
)

These are the different logging levels. You can set the logging level to log on your instance of logger

func LogLevelForError

func LogLevelForError(err error) Level

func ParseLogLevel

func ParseLogLevel(lvl string) (Level, error)

func (Level) MarshalText added in v1.7.0

func (l Level) MarshalText() ([]byte, error)

MarshalText marshals the Level to text. Note that the text representation drops the -Level suffix (see example).

func (*Level) Set added in v1.7.0

func (l *Level) Set(s string) error

Set sets the level for the flag.Value interface.

func (Level) String

func (l Level) String() string

func (*Level) UnmarshalText added in v1.7.0

func (l *Level) UnmarshalText(text []byte) error

UnmarshalText unmarshals text to a level. Like MarshalText, UnmarshalText expects the text representation of a Level to drop the -Level suffix (see example).

In particular, this makes it easy to configure logging levels using YAML, TOML, or JSON files.

type LogLevelSpecifier

type LogLevelSpecifier interface {
	LogLevel() Level
}

type Logger

type Logger interface {
	Log(ctx context.Context, level Level, msg any, fields ...Field)
	Trace(ctx context.Context, msg any, fields ...Field)
	Debug(ctx context.Context, msg any, fields ...Field)
	Info(ctx context.Context, msg any, fields ...Field)
	Warn(ctx context.Context, msg any, fields ...Field)
	Error(ctx context.Context, msg any, fields ...Field)
	Fatal(ctx context.Context, msg any, fields ...Field)
	Panic(ctx context.Context, msg any, fields ...Field)
}

type LoggerWriter

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

func (*LoggerWriter) Write

func (l *LoggerWriter) Write(p []byte) (int, error)

type Option added in v1.4.0

type Option func(*Adapter)

func WithEncoder added in v1.6.0

func WithEncoder(encoder Encoder) Option

func WithExitFunction added in v1.4.0

func WithExitFunction(f exitFunc) Option

func WithFieldsDeduplication added in v1.6.0

func WithFieldsDeduplication(deduplicateFields bool) Option

func WithLevel added in v1.4.0

func WithLevel(level Level) Option

func WithOutput added in v1.4.0

func WithOutput(w ...io.Writer) Option

func WithReportCaller added in v1.4.0

func WithReportCaller(reportCaller bool) Option

func WithTimeNow added in v1.6.0

func WithTimeNow(timeNow func() time.Time) Option

func WithTimestamp added in v1.6.0

func WithTimestamp(enableTimestamp bool) Option

type PlainTextEncoder added in v1.6.0

type PlainTextEncoder struct{}

func (PlainTextEncoder) Encode added in v1.6.0

func (p PlainTextEncoder) Encode(fields ...Field) ([]byte, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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