log

package
v0.0.0-...-cf8a7de Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StdLogGuess

func StdLogGuess(msg []byte) ([]byte, slog.Level)

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

func NewLogger(slogger *slog.Logger) *Logger

NewLogger returns a new logger wrapping an *slog.Logger

func (*Logger) Debug

func (l *Logger) Debug(msg string, attrs ...any)

Debug logs at LevelDebug.

func (*Logger) Debugf

func (l *Logger) Debugf(format string, args ...any)

Debugf logs a formatted message at LevelDebug.

func (*Logger) Enabled

func (l *Logger) Enabled(level slog.Level) bool

Enabled reports whether l emits log records at the given context and level.

func (*Logger) Error

func (l *Logger) Error(msg string, attrs ...any)

Error logs at LevelError.

func (*Logger) Errorf

func (l *Logger) Errorf(format string, args ...any)

Errorf logs a formatted message at LevelError.

func (*Logger) Info

func (l *Logger) Info(msg string, attrs ...any)

Info logs at LevelInfo.

func (*Logger) Infof

func (l *Logger) Infof(format string, args ...any)

Infof logs a formatted message at LevelInfo.

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

func (l *Logger) Slogger() *slog.Logger

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

func (l *Logger) Trace(msg string, attrs ...any) TraceFn

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

func (l *Logger) TraceDebug(msg string, attrs ...any) TraceFn

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) Warn

func (l *Logger) Warn(msg string, attrs ...any)

Warn logs at LevelWarn.

func (*Logger) Warnf

func (l *Logger) Warnf(format string, args ...any)

Warnf logs a formatted message at LevelWarn.

func (*Logger) With

func (l *Logger) With(args ...any) *Logger

With returns a Logger that includes the given attributes in each output operation. Arguments are converted to attributes as if by Logger.Log.

func (*Logger) WithGroup

func (l *Logger) WithGroup(name string) *Logger

WithGroup returns a Logger that starts a group, if name is non-empty. The keys of all attributes added to the Logger will be qualified by the given name. (How that qualification happens depends on the [Handler.WithGroup] method of the Logger's Handler.)

type StdLogLeveler

type StdLogLeveler func(logMsg []byte) (newMsg []byte, level slog.Level)

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.

Jump to

Keyboard shortcuts

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