Documentation
¶
Overview ¶
Package log provides a thin wrapper over the log/slog package, with utility functions for log message formatting.
Index ¶
- func Debug(message string, logAttributes ...any)
- func DebugError(err error, logAttributes ...any)
- func DebugErrorCause(err error, message string, logAttributes ...any)
- func DebugErrorCausef(err error, messageFormat string, formatArgs ...any)
- func DebugErrors(message string, errs ...error)
- func Debugf(messageFormat string, formatArgs ...any)
- func Error(err error, logAttributes ...any)
- func ErrorCause(err error, message string, logAttributes ...any)
- func ErrorCausef(err error, messageFormat string, formatArgs ...any)
- func ErrorMessage(message string, logAttributes ...any)
- func ErrorMessagef(messageFormat string, formatArgs ...any)
- func Errors(message string, errs ...error)
- func Info(message string, logAttributes ...any)
- func Infof(messageFormat string, formatArgs ...any)
- func JSON(key string, value any) slog.Attr
- func Warn(message string, logAttributes ...any)
- func WarnError(err error, logAttributes ...any)
- func WarnErrorCause(err error, message string, logAttributes ...any)
- func WarnErrorCausef(err error, messageFormat string, formatArgs ...any)
- func WarnErrors(message string, errs ...error)
- func Warnf(messageFormat string, formatArgs ...any)
- type Logger
- func (logger Logger) Debug(message string, logAttributes ...any)
- func (logger Logger) DebugError(err error, logAttributes ...any)
- func (logger Logger) DebugErrorCause(err error, message string, logAttributes ...any)
- func (logger Logger) DebugErrorCausef(err error, messageFormat string, formatArgs ...any)
- func (logger Logger) DebugErrors(message string, errs ...error)
- func (logger Logger) Debugf(messageFormat string, formatArgs ...any)
- func (logger Logger) Error(err error, logAttributes ...any)
- func (logger Logger) ErrorCause(err error, message string, logAttributes ...any)
- func (logger Logger) ErrorCausef(err error, messageFormat string, formatArgs ...any)
- func (logger Logger) ErrorMessage(message string, logAttributes ...any)
- func (logger Logger) ErrorMessagef(messageFormat string, formatArgs ...any)
- func (logger Logger) Errors(message string, errs ...error)
- func (logger Logger) Handler() slog.Handler
- func (logger Logger) Info(message string, logAttributes ...any)
- func (logger Logger) Infof(messageFormat string, formatArgs ...any)
- func (logger Logger) Warn(message string, logAttributes ...any)
- func (logger Logger) WarnError(err error, logAttributes ...any)
- func (logger Logger) WarnErrorCause(err error, message string, logAttributes ...any)
- func (logger Logger) WarnErrorCausef(err error, messageFormat string, formatArgs ...any)
- func (logger Logger) WarnErrors(message string, errs ...error)
- func (logger Logger) Warnf(messageFormat string, formatArgs ...any)
- func (logger Logger) With(logAttributes ...any) Logger
- func (logger Logger) WithGroup(name string) Logger
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Debug ¶
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
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
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
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
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 ¶
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 ¶
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
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
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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
JSON returns a log attribute with the given key and value. Your log output handler can then handle the value appropriately:
- slog.JSONHandler logs it as JSON as normal
- hermannm.dev/devlog.Handler logs it in a prettified format, with colors if enabled
func Warn ¶
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 ¶
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
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
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 ¶
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 ¶
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
New creates a Logger to produce structured log records for the given output handler.
func (Logger) Debug ¶ added in v0.3.0
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
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
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
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
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
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
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
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
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
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
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
Errors logs the given message at the ERROR log level, and adds a 'cause' attribute with the given errors.
func (Logger) Info ¶ added in v0.3.0
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
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
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
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
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
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
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
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
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))