Documentation ¶
Index ¶
- func StdLogGuess(msg []byte) ([]byte, slog.Level)
- type CodeLocation
- type Logger
- func (l *Logger) Debug(msg string, attrs ...any)
- func (l *Logger) Debugf(format string, args ...any)
- func (l *Logger) Enabled(level slog.Level) bool
- func (l *Logger) Error(msg string, attrs ...any)
- func (l *Logger) Errorf(format string, args ...any)
- func (l *Logger) Info(msg string, attrs ...any)
- func (l *Logger) Infof(format string, args ...any)
- func (l *Logger) Log(ctx context.Context, lvl slog.Level, location CodeLocation, message string, ...)
- func (l *Logger) Logf(ctx context.Context, lvl slog.Level, location CodeLocation, format string, ...)
- func (l *Logger) Slogger() *slog.Logger
- func (l *Logger) StdLogger(leveler StdLogLeveler) *stdlog.Logger
- func (l *Logger) Trace(msg string, attrs ...any) TraceFn
- func (l *Logger) TraceDebug(msg string, attrs ...any) TraceFn
- func (l *Logger) TraceErr(msg string, attrs ...any) TraceErrFn
- func (l *Logger) TraceThreshold(threshold time.Duration, thresholdPercent float64, msg string, attrs ...any) TraceFn
- func (l *Logger) Warn(msg string, attrs ...any)
- func (l *Logger) Warnf(format string, args ...any)
- func (l *Logger) With(args ...any) *Logger
- func (l *Logger) WithGroup(name string) *Logger
- type StdLogLeveler
- type TraceErrFn
- type TraceFn
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func StdLogGuess ¶
StdLogGuess returns a StdLogLeveler that will attempt to guess the log level of messages coming from a *stdlog.Logger using a regular expression that looks for lines beginning with the following patterns:
LVL: LVL- [LVL] Where LVL is one of: dbg, debug, inf, info, warn, warning, err, error
Types ¶
type CodeLocation ¶
type CodeLocation uintptr
CodeLocation represents the source of a logging line.
const ( // NoLocation can be used in conjunction with [Logger.Log] and [Logger.Logf] // to specify that the log should have no location. NoLocation CodeLocation = 0 )
func Up ¶
func Up(skip int) CodeLocation
Up returns the location of the caller at skip levels above the current location.
func (CodeLocation) String ¶
func (cl CodeLocation) String() string
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is a logging type with several extra convenience methods, including <METHOD>f variants for formatted output.
Non-formatted methods take a list of attrs as key-value pairs, with one exception. If the first argument is an error value, it will be treated specially, and automatically given the "err" key.
Under the hood, this wraps a *slog.Logger, which can be retrieved with the [Slogger] method for passing to dependencies that support it.
func NewLogger ¶
NewLogger returns a new logger wrapping an *slog.Logger
func (*Logger) Enabled ¶
Enabled reports whether l emits log records at the given context and level.
func (*Logger) Log ¶
func (l *Logger) Log(ctx context.Context, lvl slog.Level, location CodeLocation, message string, attrs ...any)
Log logs a message at the specified level, at the specified code location. This is a low-level logging method intended for wrapping and precise control of the logging location in cases such as wrapping or helper calls.
func (*Logger) Logf ¶
func (l *Logger) Logf(ctx context.Context, lvl slog.Level, location CodeLocation, format string, args ...any)
Logf logs a formatted message at the specified level, at the specified code location. This is a low-level logging method intended for wrapping and precise control of the logging location in cases such as wrapping or helper calls.
func (*Logger) Slogger ¶
Slogger returns the underlying *slog.Logger
func (*Logger) StdLogger ¶
func (l *Logger) StdLogger(leveler StdLogLeveler) *stdlog.Logger
StdLogger returns a *stdlog.Logger suitable for passing to dependencies which consume the standard log type.
func (*Logger) Trace ¶
Trace tracks the duration of a function or span of code. It returns a closure, that, when called, will log the message along with a duration at the Info level. It can be particularly useful for use with defer to track the execution time of the current functon.
// Example trace := log.Trace() result := someFunction() trace() // Example with defer func (t *MyType) SomeMethod(arg string) error { defer t.Logger.Trace()() }
func (*Logger) TraceDebug ¶
TraceDebug is exactly like Trace, only it will log traces at the debug level.
func (*Logger) TraceErr ¶
func (l *Logger) TraceErr(msg string, attrs ...any) TraceErrFn
TraceErr tracks the duration of a function call by returning a function that, when called with an error value, will log the message along with a duration at either the Info or Error levels, depending on whether the error is nil.
Example:
errTrace := log.TraceErr() result, err := someFunction() trace(err)
func (*Logger) TraceThreshold ¶
func (l *Logger) TraceThreshold(threshold time.Duration, thresholdPercent float64, msg string, attrs ...any) TraceFn
TraceThreshold works like TraceDebuge, except that it will log durations which exceed the threshold duration at the warning level and ordinary durations at the debug level. If the thresholdPercent value is >0, it will only warn if the duration exceeds the threshold by a certain percent.
As with the other tracing methods, log messages will have the `duration` attribute appended, representing the duration of the span. If thresholdPercent is >0, `duration_percent` will also be added.
func (*Logger) With ¶
With returns a Logger that includes the given attributes in each output operation. Arguments are converted to attributes as if by Logger.Log.
type StdLogLeveler ¶
StdLogLeveler is a function to determine the log message to assign messages from a *stdlog.Logger.
func StdLogStatic ¶
func StdLogStatic(logLevel slog.Level) StdLogLeveler
StdLogStatic returns a StdLogLeveler that will log all messages at the provided level.
type TraceErrFn ¶
type TraceErrFn func(error)
TraceErrFn is the closure returned from Logger.TraceErr for tracing execution time of a process that returns an [error] value.
type TraceFn ¶
type TraceFn func()
TraceFn is the closure returned from Logger.Trace for tracing execution time of a process or function.