log

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2024 License: MIT Imports: 6 Imported by: 9

Documentation

Overview

Package log provides a thin wrapper over the log/slog package, with utility functions for log message formatting.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

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

Debug logs the given message at the DEBUG log level, along with any given log attributes. It uses the slog.Default logger.

Note that the DEBUG log level is typically disabled by default in most log handlers, in which case no output will be produced.

Log attributes

A log attribute is a key/value pair attached to a log line. You can pass attributes in the following ways:

// Pairs of string keys and corresponding values:
log.Debug("Message", "attr1", "value1", "attr2", 2)
// slog.Attr objects:
log.Debug("Message", slog.String("attr1", "value1"), slog.Int("attr2", 2))
// Or a mix of the two:
log.Debug("Message", "attr1", "value1", slog.Int("attr2", 2))

func DebugError added in v0.5.0

func DebugError(err error, logAttributes ...any)

DebugError logs the given error at the DEBUG log level, along with any given log attributes. It uses the slog.Default logger.

Note that the DEBUG log level is typically disabled by default in most log handlers, in which case no output will be produced.

Log attributes

A log attribute is a key/value pair attached to a log line. You can pass attributes in the following ways:

// Pairs of string keys and corresponding values:
log.DebugError(err, "attr1", "value1", "attr2", 2)
// slog.Attr objects:
log.DebugError(err, slog.String("attr1", "value1"), slog.Int("attr2", 2))
// Or a mix of the two:
log.DebugError(err, "attr1", "value1", slog.Int("attr2", 2))

func DebugErrorCause added in v0.5.0

func DebugErrorCause(err error, message string, logAttributes ...any)

DebugErrorCause logs the given message at the DEBUG log level, and adds a 'cause' attribute with the given error, along with any other log attributes. It uses the slog.Default logger.

Note that the DEBUG log level is typically disabled by default in most log handlers, in which case no output will be produced.

Log attributes

A log attribute is a key/value pair attached to a log line. You can pass attributes in the following ways:

// Pairs of string keys and corresponding values:
log.DebugErrorCause(err, "Message", "attr1", "value1", "attr2", 2)
// slog.Attr objects:
log.DebugErrorCause(err, "Message", slog.String("attr1", "value1"), slog.Int("attr2", 2))
// Or a mix of the two:
log.DebugErrorCause(err, "Message", "attr1", "value1", slog.Int("attr2", 2))

func DebugErrorCausef added in v0.5.0

func DebugErrorCausef(err error, messageFormat string, formatArgs ...any)

DebugErrorCausef logs a formatted message (using fmt.Sprintf) at the DEBUG log level, and adds a 'cause' attribute with the given error. It uses the slog.Default logger.

Note that the DEBUG log level is typically disabled by default in most log handlers, in which case no output will be produced.

func DebugErrors added in v0.5.0

func DebugErrors(message string, errs ...error)

DebugErrors logs the given message at the DEBUG log level, and adds a 'cause' attribute with the given errors. It uses the slog.Default logger.

Note that the DEBUG log level is typically disabled by default in most log handlers, in which case no output will be produced.

func Debugf

func Debugf(messageFormat string, formatArgs ...any)

Debugf creates a message from the given format and arguments using fmt.Sprintf, and logs it at the DEBUG log level. It uses the slog.Default logger.

Note that the DEBUG log level is typically disabled by default in most log handlers, in which case no output will be produced.

func Error

func Error(err error, logAttributes ...any)

Error logs the given error at the ERROR log level, along with any given log attributes. It uses the slog.Default logger.

Log attributes

A log attribute is a key/value pair attached to a log line. You can pass attributes in the following ways:

// Pairs of string keys and corresponding values:
log.Error(err, "attr1", "value1", "attr2", 2)
// slog.Attr objects:
log.Error(err, slog.String("attr1", "value1"), slog.Int("attr2", 2))
// Or a mix of the two:
log.Error(err, "attr1", "value1", slog.Int("attr2", 2))

func ErrorCause added in v0.3.0

func ErrorCause(err error, message string, logAttributes ...any)

ErrorCause logs the given message at the ERROR log level, and adds a 'cause' attribute with the given error, along with any other log attributes. It uses the slog.Default logger.

Log attributes

A log attribute is a key/value pair attached to a log line. You can pass attributes in the following ways:

// Pairs of string keys and corresponding values:
log.ErrorCause(err, "Message", "attr1", "value1", "attr2", 2)
// slog.Attr objects:
log.ErrorCause(err, "Message", slog.String("attr1", "value1"), slog.Int("attr2", 2))
// Or a mix of the two:
log.ErrorCause(err, "Message", "attr1", "value1", slog.Int("attr2", 2))

func ErrorCausef added in v0.3.0

func ErrorCausef(err error, messageFormat string, formatArgs ...any)

ErrorCausef logs a formatted message (using fmt.Sprintf) at the ERROR log level, and adds a 'cause' attribute with the given error. It uses the slog.Default logger.

func ErrorMessage

func ErrorMessage(message string, logAttributes ...any)

ErrorMessage logs the given message at the ERROR log level, along with any given log attributes. It uses the slog.Default logger.

Log attributes

A log attribute is a key/value pair attached to a log line. You can pass attributes in the following ways:

// Pairs of string keys and corresponding values:
log.ErrorMessage("Message", "attr1", "value1", "attr2", 2)
// slog.Attr objects:
log.ErrorMessage("Message", slog.String("attr1", "value1"), slog.Int("attr2", 2))
// Or a mix of the two:
log.ErrorMessage("Message", "attr1", "value1", slog.Int("attr2", 2))

func ErrorMessagef

func ErrorMessagef(messageFormat string, formatArgs ...any)

ErrorMessagef creates a message from the given format and arguments using fmt.Sprintf, and logs it at the ERROR log level. It uses the slog.Default logger.

func Errors

func Errors(message string, errs ...error)

Errors logs the given message at the ERROR log level, and adds a 'cause' attribute with the given errors. It uses the slog.Default logger.

func Info

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

Info logs the given message at the INFO log level, along with any given log attributes. It uses the slog.Default logger.

Log attributes

A log attribute is a key/value pair attached to a log line. You can pass attributes in the following ways:

// Pairs of string keys and corresponding values:
log.Info("Message", "attr1", "value1", "attr2", 2)
// slog.Attr objects:
log.Info("Message", slog.String("attr1", "value1"), slog.Int("attr2", 2))
// Or a mix of the two:
log.Info("Message", "attr1", "value1", slog.Int("attr2", 2))

func Infof

func Infof(messageFormat string, formatArgs ...any)

Infof creates a message from the given format and arguments using fmt.Sprintf, and logs it at the INFO log level. It uses the slog.Default logger.

func JSON added in v0.4.0

func JSON(key string, value any) slog.Attr

JSON returns a log attribute with the given key and value. Your log output handler can then handle the value appropriately:

func Warn

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

Warn logs the given message at the WARN log level, along with any given log attributes. It uses the slog.Default logger.

Log attributes

A log attribute is a key/value pair attached to a log line. You can pass attributes in the following ways:

// Pairs of string keys and corresponding values:
log.Warn("Message", "attr1", "value1", "attr2", 2)
// slog.Attr objects:
log.Warn("Message", slog.String("attr1", "value1"), slog.Int("attr2", 2))
// Or a mix of the two:
log.Warn("Message", "attr1", "value1", slog.Int("attr2", 2))

func WarnError

func WarnError(err error, logAttributes ...any)

WarnError logs the given error at the WARN log level, along with any given log attributes. It uses the slog.Default logger.

Log attributes

A log attribute is a key/value pair attached to a log line. You can pass attributes in the following ways:

// Pairs of string keys and corresponding values:
log.WarnError(err, "attr1", "value1", "attr2", 2)
// slog.Attr objects:
log.WarnError(err, slog.String("attr1", "value1"), slog.Int("attr2", 2))
// Or a mix of the two:
log.WarnError(err, "attr1", "value1", slog.Int("attr2", 2))

func WarnErrorCause added in v0.4.0

func WarnErrorCause(err error, message string, logAttributes ...any)

WarnErrorCause logs the given message at the WARN log level, and adds a 'cause' attribute with the given error, along with any other log attributes. It uses the slog.Default logger.

Log attributes

A log attribute is a key/value pair attached to a log line. You can pass attributes in the following ways:

// Pairs of string keys and corresponding values:
log.WarnErrorCause(err, "Message", "attr1", "value1", "attr2", 2)
// slog.Attr objects:
log.WarnErrorCause(err, "Message", slog.String("attr1", "value1"), slog.Int("attr2", 2))
// Or a mix of the two:
log.WarnErrorCause(err, "Message", "attr1", "value1", slog.Int("attr2", 2))

func WarnErrorCausef added in v0.4.0

func WarnErrorCausef(err error, messageFormat string, formatArgs ...any)

WarnErrorCausef logs a formatted message (using fmt.Sprintf) at the WARN log level, and adds a 'cause' attribute with the given error. It uses the slog.Default logger.

func WarnErrors

func WarnErrors(message string, errs ...error)

WarnErrors logs the given message at the WARN log level, and adds a 'cause' attribute with the given errors. It uses the slog.Default logger.

func Warnf

func Warnf(messageFormat string, formatArgs ...any)

Warnf creates a message from the given format and arguments using fmt.Sprintf, and logs it at the WARN log level. It uses the slog.Default logger.

Types

type Logger added in v0.3.0

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

A Logger provides methods to produce structured log records for its output handler. It is analogous to slog.Logger, but provides more utilities for log message formatting.

The logger must be initialized with New or Default. An uninitialized logger will panic on every method.

func Default added in v0.3.2

func Default() Logger

Default creates a Logger with the same output handler as the one currently used by slog.Default.

func New added in v0.3.0

func New(outputHandler slog.Handler) Logger

New creates a Logger to produce structured log records for the given output handler.

func (Logger) Debug added in v0.3.0

func (logger Logger) Debug(message string, logAttributes ...any)

Debug logs the given message at the DEBUG log level, along with any given log attributes.

Note that the DEBUG log level is typically disabled by default in most log handlers, in which case no output will be produced.

Log attributes

A log attribute is a key/value pair attached to a log line. You can pass attributes in the following ways:

// Pairs of string keys and corresponding values:
logger.Debug("Message", "attr1", "value1", "attr2", 2)
// slog.Attr objects:
logger.Debug("Message", slog.String("attr1", "value1"), slog.Int("attr2", 2))
// Or a mix of the two:
logger.Debug("Message", "attr1", "value1", slog.Int("attr2", 2))

func (Logger) DebugError added in v0.5.0

func (logger Logger) DebugError(err error, logAttributes ...any)

DebugError logs the given error at the DEBUG log level, along with any given log attributes.

Note that the DEBUG log level is typically disabled by default in most log handlers, in which case no output will be produced.

Log attributes

A log attribute is a key/value pair attached to a log line. You can pass attributes in the following ways:

// Pairs of string keys and corresponding values:
logger.DebugError(err, "attr1", "value1", "attr2", 2)
// slog.Attr objects:
logger.DebugError(err, slog.String("attr1", "value1"), slog.Int("attr2", 2))
// Or a mix of the two:
logger.DebugError(err, "attr1", "value1", slog.Int("attr2", 2))

func (Logger) DebugErrorCause added in v0.5.0

func (logger Logger) DebugErrorCause(err error, message string, logAttributes ...any)

DebugErrorCause logs the given message at the DEBUG log level, and adds a 'cause' attribute with the given error, along with any other log attributes.

Note that the DEBUG log level is typically disabled by default in most log handlers, in which case no output will be produced.

Log attributes

A log attribute is a key/value pair attached to a log line. You can pass attributes in the following ways:

// Pairs of string keys and corresponding values:
logger.DebugErrorCause(err, "Message", "attr1", "value1", "attr2", 2)
// slog.Attr objects:
logger.DebugErrorCause(err, "Message", slog.String("attr1", "value1"), slog.Int("attr2", 2))
// Or a mix of the two:
logger.DebugErrorCause(err, "Message", "attr1", "value1", slog.Int("attr2", 2))

func (Logger) DebugErrorCausef added in v0.5.0

func (logger Logger) DebugErrorCausef(err error, messageFormat string, formatArgs ...any)

DebugErrorCausef logs a formatted message (using fmt.Sprintf) at the DEBUG log level, and adds a 'cause' attribute with the given error.

Note that the DEBUG log level is typically disabled by default in most log handlers, in which case no output will be produced.

func (Logger) DebugErrors added in v0.5.0

func (logger Logger) DebugErrors(message string, errs ...error)

DebugErrors logs the given message at the DEBUG log level, and adds a 'cause' attribute with the given errors.

Note that the DEBUG log level is typically disabled by default in most log handlers, in which case no output will be produced.

func (Logger) Debugf added in v0.3.0

func (logger Logger) Debugf(messageFormat string, formatArgs ...any)

Debugf creates a message from the given format and arguments using fmt.Sprintf, and logs it at the DEBUG log level.

Note that the DEBUG log level is typically disabled by default in most log handlers, in which case no output will be produced.

func (Logger) Error added in v0.3.0

func (logger Logger) Error(err error, logAttributes ...any)

Error logs the given error at the ERROR log level, along with any given log attributes.

Log attributes

A log attribute is a key/value pair attached to a log line. You can pass attributes in the following ways:

// Pairs of string keys and corresponding values:
logger.Error(err, "attr1", "value1", "attr2", 2)
// slog.Attr objects:
logger.Error(err, slog.String("attr1", "value1"), slog.Int("attr2", 2))
// Or a mix of the two:
logger.Error(err, "attr1", "value1", slog.Int("attr2", 2))

func (Logger) ErrorCause added in v0.3.0

func (logger Logger) ErrorCause(err error, message string, logAttributes ...any)

ErrorCause logs the given message at the ERROR log level, and adds a 'cause' attribute with the given error, along with any other log attributes.

Log attributes

A log attribute is a key/value pair attached to a log line. You can pass attributes in the following ways:

// Pairs of string keys and corresponding values:
logger.ErrorCause(err, "Message", "attr1", "value1", "attr2", 2)
// slog.Attr objects:
logger.ErrorCause(err, "Message", slog.String("attr1", "value1"), slog.Int("attr2", 2))
// Or a mix of the two:
logger.ErrorCause(err, "Message", "attr1", "value1", slog.Int("attr2", 2))

func (Logger) ErrorCausef added in v0.3.0

func (logger Logger) ErrorCausef(err error, messageFormat string, formatArgs ...any)

ErrorCausef logs a formatted message (using fmt.Sprintf) at the ERROR log level, and adds a 'cause' attribute with the given error.

func (Logger) ErrorMessage added in v0.3.0

func (logger Logger) ErrorMessage(message string, logAttributes ...any)

ErrorMessage logs the given message at the ERROR log level, along with any given log attributes.

Log attributes

A log attribute is a key/value pair attached to a log line. You can pass attributes in the following ways:

// Pairs of string keys and corresponding values:
logger.ErrorMessage("Message", "attr1", "value1", "attr2", 2)
// slog.Attr objects:
logger.ErrorMessage("Message", slog.String("attr1", "value1"), slog.Int("attr2", 2))
// Or a mix of the two:
logger.ErrorMessage("Message", "attr1", "value1", slog.Int("attr2", 2))

func (Logger) ErrorMessagef added in v0.3.0

func (logger Logger) ErrorMessagef(messageFormat string, formatArgs ...any)

ErrorMessagef creates a message from the given format and arguments using fmt.Sprintf, and logs it at the ERROR log level.

func (Logger) Errors added in v0.3.0

func (logger Logger) Errors(message string, errs ...error)

Errors logs the given message at the ERROR log level, and adds a 'cause' attribute with the given errors.

func (Logger) Handler added in v0.3.0

func (logger Logger) Handler() slog.Handler

Handler returns the output handler for the logger.

func (Logger) Info added in v0.3.0

func (logger Logger) Info(message string, logAttributes ...any)

Info logs the given message at the INFO log level, along with any given log attributes.

Log attributes

A log attribute is a key/value pair attached to a log line. You can pass attributes in the following ways:

// Pairs of string keys and corresponding values:
logger.Info("Message", "attr1", "value1", "attr2", 2)
// slog.Attr objects:
logger.Info("Message", slog.String("attr1", "value1"), slog.Int("attr2", 2))
// Or a mix of the two:
logger.Info("Message", "attr1", "value1", slog.Int("attr2", 2))

func (Logger) Infof added in v0.3.0

func (logger Logger) Infof(messageFormat string, formatArgs ...any)

Infof creates a message from the given format and arguments using fmt.Sprintf, and logs it at the INFO log level.

func (Logger) Warn added in v0.3.0

func (logger Logger) Warn(message string, logAttributes ...any)

Warn logs the given message at the WARN log level, along with any given log attributes.

Log attributes

A log attribute is a key/value pair attached to a log line. You can pass attributes in the following ways:

// Pairs of string keys and corresponding values:
logger.Warn("Message", "attr1", "value1", "attr2", 2)
// slog.Attr objects:
logger.Warn("Message", slog.String("attr1", "value1"), slog.Int("attr2", 2))
// Or a mix of the two:
logger.Warn("Message", "attr1", "value1", slog.Int("attr2", 2))

func (Logger) WarnError added in v0.4.0

func (logger Logger) WarnError(err error, logAttributes ...any)

WarnError logs the given error at the WARN log level, along with any given log attributes.

Log attributes

A log attribute is a key/value pair attached to a log line. You can pass attributes in the following ways:

// Pairs of string keys and corresponding values:
logger.WarnError(err, "attr1", "value1", "attr2", 2)
// slog.Attr objects:
logger.WarnError(err, slog.String("attr1", "value1"), slog.Int("attr2", 2))
// Or a mix of the two:
logger.WarnError(err, "attr1", "value1", slog.Int("attr2", 2))

func (Logger) WarnErrorCause added in v0.4.0

func (logger Logger) WarnErrorCause(err error, message string, logAttributes ...any)

WarnErrorCause logs the given message at the WARN log level, and adds a 'cause' attribute with the given error, along with any other log attributes.

Log attributes

A log attribute is a key/value pair attached to a log line. You can pass attributes in the following ways:

// Pairs of string keys and corresponding values:
logger.WarnErrorCause(err, "Message", "attr1", "value1", "attr2", 2)
// slog.Attr objects:
logger.WarnErrorCause(err, "Message", slog.String("attr1", "value1"), slog.Int("attr2", 2))
// Or a mix of the two:
logger.WarnErrorCause(err, "Message", "attr1", "value1", slog.Int("attr2", 2))

func (Logger) WarnErrorCausef added in v0.4.0

func (logger Logger) WarnErrorCausef(err error, messageFormat string, formatArgs ...any)

WarnErrorCausef logs a formatted message (using fmt.Sprintf) at the WARN log level, and adds a 'cause' attribute with the given error.

func (Logger) WarnErrors added in v0.4.0

func (logger Logger) WarnErrors(message string, errs ...error)

WarnErrors logs the given message at the WARN log level, and adds a 'cause' attribute with the given errors.

func (Logger) Warnf added in v0.3.0

func (logger Logger) Warnf(messageFormat string, formatArgs ...any)

Warnf creates a message from the given format and arguments using fmt.Sprintf, and logs it at the WARN log level.

func (Logger) With added in v0.3.0

func (logger Logger) With(logAttributes ...any) Logger

With returns a Logger that includes the given attributes in each log. If no attributes are given, the logger is returned as-is.

Log attributes

A log attribute is a key/value pair attached to a log line. You can pass attributes in the following ways:

// Pairs of string keys and corresponding values:
logger.With("attr1", "value1", "attr2", 2)
// slog.Attr objects:
logger.With(slog.String("attr1", "value1"), slog.Int("attr2", 2))
// Or a mix of the two:
logger.With("attr1", "value1", slog.Int("attr2", 2))

func (Logger) WithGroup added in v0.3.0

func (logger Logger) WithGroup(name string) Logger

WithGroup returns a Logger that starts an attribute group. Keys of attributes added to the Logger (through Logger.With) will be qualified by the given name. If name is empty, the logger is returned as-is.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳