log

package
v2.7.1 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2023 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultLevel  = InfoLevel
	DefaultFormat = TextFormat
	DefaultOutput = &peekLogWriter{os.Stderr}
)

Defaults for package level variables

Functions

func Debug

func Debug(message string, v ...any)

Debug prints the given message, if the current log level is DEBUG or lower

func DisableDates

func DisableDates()

DisableDates disables the date/time prefix

func Error

func Error(message string, v ...any)

Error prints the given message, if the current log level is ERROR or lower

func Fatal

func Fatal(message string, v ...any)

Fatal prints the given message, and exits the program

func File

func File() string

File returns the log file, if any, or an empty string otherwise

func Info

func Info(message string, v ...any)

Info prints the given message, if the current log level is INFO or lower

func IsDebug

func IsDebug() bool

IsDebug returns true if the current log level is DebugLevel or below

func IsFile

func IsFile() bool

IsFile returns true if the output is a non-default file

func IsTrace

func IsTrace() bool

IsTrace returns true if the current log level is TraceLevel

func Loggable

func Loggable(l Level) bool

Loggable returns true if the given log level is lower or equal to the current log level

func ResetLevelOverrides

func ResetLevelOverrides()

ResetLevelOverrides removes all log level overrides

func SetFormat

func SetFormat(newFormat Format)

SetFormat sets a new log format

func SetLevel

func SetLevel(newLevel Level)

SetLevel sets a new log level

func SetLevelOverride

func SetLevelOverride(field string, value string, level Level)

SetLevelOverride adds a log override for the given field

func SetOutput

func SetOutput(w io.Writer)

SetOutput sets the log output writer

func Trace

func Trace(message string, v ...any)

Trace prints the given message, if the current log level is TRACE

func Warn

func Warn(message string, v ...any)

Warn prints the given message, if the current log level is WARN or lower

Types

type Context

type Context map[string]any

Context represents an object's state in the form of key-value pairs

func (Context) Merge

func (c Context) Merge(other Context)

Merge merges other into this context

type Contexter

type Contexter interface {
	Context() Context
}

Contexter allows structs to export a key-value pairs in the form of a Context

type Event

type Event struct {
	Timestamp string `json:"time"`
	Level     Level  `json:"level"`
	Message   string `json:"message"`
	// contains filtered or unexported fields
}

Event represents a single log event

func Field

func Field(key string, value any) *Event

Field creates a new log event and adds a custom field and value to it

func Fields

func Fields(fields Context) *Event

Fields creates a new log event and adds a map of fields to it

func Tag

func Tag(tag string) *Event

Tag creates a new log event and adds a "tag" field to it

func Time

func Time(time time.Time) *Event

Time creates a new log event and sets the time field

func Timing

func Timing(f func()) *Event

Timing runs f and records the time if took to execute it in "time_taken_ms"

func With

func With(contexts ...Contexter) *Event

With creates a new log event and adds the fields of the given Contexter structs

func (*Event) Debug

func (e *Event) Debug(message string, v ...any) *Event

Debug logs the event with log level debug

func (*Event) Err

func (e *Event) Err(err error) *Event

Err adds an "error" field to the log event

func (*Event) Error

func (e *Event) Error(message string, v ...any) *Event

Error logs the event with log level error

func (*Event) Fatal

func (e *Event) Fatal(message string, v ...any)

Fatal logs the event as FATAL, and exits the program with exit code 1

func (*Event) Field

func (e *Event) Field(key string, value any) *Event

Field adds a custom field and value to the log event

func (*Event) FieldIf

func (e *Event) FieldIf(key string, value any, level Level) *Event

FieldIf adds a custom field and value to the log event if the given level is loggable

func (*Event) Fields

func (e *Event) Fields(fields Context) *Event

Fields adds a map of fields to the log event

func (*Event) Info

func (e *Event) Info(message string, v ...any) *Event

Info logs the event with log level info

func (*Event) IsDebug

func (e *Event) IsDebug() bool

IsDebug returns true if the current log level is DebugLevel or below

func (*Event) IsTrace

func (e *Event) IsTrace() bool

IsTrace returns true if the current log level is TraceLevel

func (*Event) JSON

func (e *Event) JSON() string

JSON returns the event as a JSON representation

func (*Event) Log

func (e *Event) Log(l Level, message string, v ...any) *Event

Log logs the event to the defined output, or does nothing if Render returns an empty string

func (*Event) Loggable

func (e *Event) Loggable(l Level) bool

Loggable returns true if the given log level is lower or equal to the current log level

func (*Event) Render

func (e *Event) Render(l Level, message string, v ...any) string

Render returns the rendered log event as a string, or an empty string. The event is only rendered, if either the global log level is >= l, or if the log level in one of the overrides matches the level.

If no overrides are defined (default), the Contexter array is not applied unless the event is actually logged. If overrides are defined, then Contexters have to be applied in any case to determine if they match. This is super complicated, but required for efficiency.

func (*Event) String

func (e *Event) String() string

String returns the event as a string

func (*Event) Tag

func (e *Event) Tag(tag string) *Event

Tag adds a "tag" field to the log event

func (*Event) Time

func (e *Event) Time(t time.Time) *Event

Time sets the time field

func (*Event) Timing

func (e *Event) Timing(f func()) *Event

Timing runs f and records the time if took to execute it in "time_taken_ms"

func (*Event) Trace

func (e *Event) Trace(message string, v ...any) *Event

Trace logs the event with log level trace

func (*Event) Warn

func (e *Event) Warn(message string, v ...any) *Event

Warn logs the event with log level warn

func (*Event) With

func (e *Event) With(contexters ...Contexter) *Event

With adds the fields of the given Contexter structs to the log event by calling their Context method

type Format

type Format int

Format is a well-known log format

const (
	TextFormat Format = iota
	JSONFormat
)

Log formats

func CurrentFormat

func CurrentFormat() Format

CurrentFormat returns the current log format

func ToFormat

func ToFormat(s string) Format

ToFormat converts a string to a Format. It returns TextFormat if the string does not match any known log formats.

func (Format) String

func (f Format) String() string

type Level

type Level int

Level is a well-known log level, as defined below

const (
	TraceLevel Level = iota
	DebugLevel
	InfoLevel
	WarnLevel
	ErrorLevel
	FatalLevel
)

Well known log levels

func CurrentLevel

func CurrentLevel() Level

CurrentLevel returns the current log level

func ToLevel

func ToLevel(s string) Level

ToLevel converts a string to a Level. It returns InfoLevel if the string does not match any known log levels.

func (Level) MarshalJSON

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

MarshalJSON converts a level to a JSON string

func (Level) String

func (l Level) String() string

Jump to

Keyboard shortcuts

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