Documentation ¶
Index ¶
- Constants
- Variables
- func BeltWithLogger(belt *belt.Belt, logger Logger) *belt.Belt
- func CtxWithLogger(ctx context.Context, logger Logger) context.Context
- func Debug(ctx context.Context, values ...any)
- func DebugFields(ctx context.Context, message string, fields field.AbstractFields)
- func Debugf(ctx context.Context, format string, args ...any)
- func Error(ctx context.Context, values ...any)
- func ErrorFields(ctx context.Context, message string, fields field.AbstractFields)
- func Errorf(ctx context.Context, format string, args ...any)
- func Fatal(ctx context.Context, values ...any)
- func FatalFields(ctx context.Context, message string, fields field.AbstractFields)
- func Fatalf(ctx context.Context, format string, args ...any)
- func Flush(ctx context.Context)
- func Info(ctx context.Context, values ...any)
- func InfoFields(ctx context.Context, message string, fields field.AbstractFields)
- func Infof(ctx context.Context, format string, args ...any)
- func Log(ctx context.Context, level Level, values ...any)
- func LogFields(ctx context.Context, level Level, message string, fields field.AbstractFields)
- func Logf(ctx context.Context, level Level, format string, args ...any)
- func Panic(ctx context.Context, values ...any)
- func PanicFields(ctx context.Context, message string, fields field.AbstractFields)
- func Panicf(ctx context.Context, format string, args ...any)
- func Trace(ctx context.Context, values ...any)
- func TraceFields(ctx context.Context, message string, fields field.AbstractFields)
- func Tracef(ctx context.Context, format string, args ...any)
- func Warn(ctx context.Context, values ...any)
- func WarnFields(ctx context.Context, message string, fields field.AbstractFields)
- func Warnf(ctx context.Context, format string, args ...any)
- type Emitter
- type Entry
- type EntryProperty
- type Hook
- type Hooks
- type Level
- type Logger
- func FromBelt(belt *belt.Belt) Logger
- func FromCtx(ctx context.Context) Logger
- func WithContextFields(ctx context.Context, allFields *field.FieldsChain, newFieldsCount int) Logger
- func WithField(ctx context.Context, key string, value any, props ...field.Property) Logger
- func WithFields(ctx context.Context, fields field.AbstractFields) Logger
- func WithHooks(ctx context.Context, hooks ...Hook) Logger
- func WithLevel(ctx context.Context, level Level) Logger
- func WithMessagePrefix(ctx context.Context, prefix string) Logger
- func WithPreHooks(ctx context.Context, preHooks ...PreHook) Logger
- func WithTraceIDs(ctx context.Context, traceIDs belt.TraceIDs, newTraceIDsCount int) Logger
- type PreHook
- type PreHooks
Constants ¶
const ( // LevelUndefined is the erroneous value of log-level which corresponds // to zero-value. LevelUndefined = types.LevelUndefined // LevelFatal will report about Fatalf-s only. LevelFatal = types.LevelFatal // LevelPanic will report about Panicf-s and Fatalf-s only. LevelPanic = types.LevelPanic // LevelError will report about Errorf-s, Panicf-s, ... LevelError = types.LevelError // LevelWarning will report about Warningf-s, Errorf-s, ... LevelWarning = types.LevelWarning // LevelInfo will report about Infof-s, Warningf-s, ... LevelInfo = types.LevelInfo // LevelDebug will report about Debugf-s, Infof-s, ... LevelDebug = types.LevelDebug // LevelTrace will report about Tracef-s, Debugf-s, ... LevelTrace = types.LevelTrace )
Variables ¶
var Default = stdlib.Default
Default is the (overridable) function which provides a Logger when no Logger is set. It is used by functions FromBelt and FromCtx.
var ToolID = toolID{}
ToolID is the unique ToolID which defines a Logger.
Functions ¶
func BeltWithLogger ¶
func BeltWithLogger(belt *belt.Belt, logger Logger) *belt.Belt
BeltWithLogger returns an Belt with the given Logger set.
func CtxWithLogger ¶
CtxWithLogger returns a context with the given Logger set.
func DebugFields ¶
func DebugFields(ctx context.Context, message string, fields field.AbstractFields)
DebugFields is just a shorthand for LogFields(ctx, logger.LevelDebug, ...)
func ErrorFields ¶
func ErrorFields(ctx context.Context, message string, fields field.AbstractFields)
ErrorFields is just a shorthand for LogFields(ctx, logger.LevelError, ...)
func Fatal ¶
Fatal is just a shorthand for Log(logger.LevelFatal, ...)
Be aware: Fatal level also triggers an `os.Exit`.
func FatalFields ¶
func FatalFields(ctx context.Context, message string, fields field.AbstractFields)
FatalFields is just a shorthand for LogFields(ctx, logger.LevelFatal, ...)
Be aware: Panic level also triggers an `os.Exit`.
func Fatalf ¶
Fatalf is just a shorthand for Logf(ctx, logger.LevelFatal, ...)
Be aware: Fatal level also triggers an `os.Exit`.
func InfoFields ¶
func InfoFields(ctx context.Context, message string, fields field.AbstractFields)
InfoFields is just a shorthand for LogFields(ctx, logger.LevelInfo, ...)
func Log ¶
Log extracts structured fields from provided values, joins the rest into an unstructured message and logs the result.
This function provides convenience (relatively to LogFields) at cost of a bit of performance.
There are few ways to extract structured fields, which are applied for each value from `values` (in descending priority order):
- If a `value` is an `*Entry` then the Entry is used (with its fields). This works only if this is the only argument. Otherwise it is threated as a simple structure (see point #3).
- If a `value` implements field.AbstractFields then ForEachField method is used (so it is become similar to LogFields).
- If a `value` is a structure (or a pointer to a structure) then fields of the structure are interpreted as structured fields to be logged (see explanation below).
- If a `value` is a map then fields a constructed out of this map.
Structured arguments are processed effectively the same as they would have through a sequence of WithField/WithFields.
Everything that does not fit into any of the rules above is just joined into an unstructured message (and works the same way as `message` in LogFields).
How structures are parsed: Structures are parsed recursively. Each field name of the path in a tree of structures is added to the resulting field name (for example in "struct{A struct{B int}}" the field name will be `A.B`). To enforce another name use tag `log` (for example "struct{A int `log:"anotherName"`}"), to prevent a field from logging use tag `log:"-"`.
Examples:
user, err := getUser() if err != nil { logger.Log(ctx, logger.LevelError, err) return err } logger.Log(ctx, logger.LevelDebug, "current user", user) // fields of structure "user" will be logged logger.Log(ctx, logger.LevelDebug, map[string]any{"user_id": user.ID, "group_id", user.GroupID}) logger.Log(ctx, logger.LevelDebug, field.Fields{{Key: "user_id", Value: user.ID}, {Key: "group_id", Value: user.GroupID}}) logger.Log(ctx, logger.LevelDebug, "current user ID is ", user.ID, " and group ID is ", user.GroupID) // will result into message "current user ID is 1234 and group ID is 5678".
func LogFields ¶
LogFields logs structured fields with a explanation message.
Anything that implements field.AbstractFields might be used as a collection of fields to be logged.
Examples:
logger.LogFields(ctx, logger.LevelDebug, "new_request", field.Fields{{Key: "user_id", Value: userID}, {Key: "group_id", Value: groupID}}) logger.LogFields(ctx, logger.LevelInfo, "affected entries", field.Field{Key: "mysql_affected", Value: affectedRows}) logger.LogFields(ctx, logger.LevelError, "unable to fetch user info", request) // where `request` implements field.AbstractFields
Sometimes it is inconvenient to manually describe each field, and for such cases see method `Log`.
func Logf ¶
Logf logs an unstructured message. All contextual structured fields are also logged.
This method exists mostly for convenience, for people who has not got used to proper structured logging, yet. See `LogFields` and `Log`. If one have variables they want to log, it is better for scalable observability to log them as structured values, instead of injecting them into a non-structured string.
func Panic ¶
Panic is just a shorthand for Log(ctx, logger.LevelPanic, ...)
Be aware: Panic level also triggers a `panic`.
func PanicFields ¶
func PanicFields(ctx context.Context, message string, fields field.AbstractFields)
PanicFields is just a shorthand for LogFields(ctx, logger.LevelPanic, ...)
Be aware: Panic level also triggers a `panic`.
func Panicf ¶
Panicf is just a shorthand for Logf(ctx, logger.LevelPanic, ...)
Be aware: Panic level also triggers a `panic`.
func TraceFields ¶
func TraceFields(ctx context.Context, message string, fields field.AbstractFields)
TraceFields is just a shorthand for LogFields(ctx, logger.LevelTrace, ...)
func WarnFields ¶
func WarnFields(ctx context.Context, message string, fields field.AbstractFields)
WarnFields is just a shorthand for LogFields(ctx, logger.LevelWarn, ...)
Types ¶
type Emitter ¶
Emitter is just a type-alias for logger/types.Emitter for convenience.
func GetEmitter ¶
GetEmitter returns the Emitter (see the description of interface "Emitter").
type EntryProperty ¶
type EntryProperty = types.EntryProperty
EntryProperty is just a type-alias for logger/types.EntryProperty for convenience.
type Logger ¶
Logger is just a type-alias for logger/types.Logger for convenience.
func FromBelt ¶
func FromBelt(belt *belt.Belt) Logger
FromBelt returns the Logger defined in the Belt. If one is not defined, then the default Logger is returned (see function "Default").
func FromCtx ¶
FromCtx returns the Logger defined in the context. If one is not defined, then the default Logger is returned (see function "Default").
func WithContextFields ¶
func WithContextFields(ctx context.Context, allFields *field.FieldsChain, newFieldsCount int) Logger
WithContextFields is a shorthand for FromCtx(ctx).WithContextFields
func WithFields ¶
func WithFields(ctx context.Context, fields field.AbstractFields) Logger
WithFields returns the logger with the added fields (used for structured logging)
func WithHooks ¶
WithHooks returns a Logger which includes/appends hooks from the arguments.
See also description of "Hook".
Special case: to reset hooks use `WithHooks()` (without any arguments).
func WithLevel ¶
WithLevel returns a logger with logger level set to the given argument.
See also the description of type "Level".
func WithMessagePrefix ¶
WithMessagePrefix adds a string to all messages logged through the derived logger.
func WithPreHooks ¶
WithPreHooks returns a Logger which includes/appends pre-hooks from the arguments.
See also description of "PreHook".
Special case: to reset hooks use `WithPreHooks()` (without any arguments).