Documentation ¶
Overview ¶
Package plog - Logging convenience functions
Index ¶
- Constants
- Variables
- func ErrorAttr(err error) slog.Attr
- func ErrorError(sl *slog.Logger, msg string, err error, args ...any)
- func ErrorLogLogger(msg string, logger *slog.Logger, debugREs ...*regexp.Regexp) *log.Logger
- func FatalError(sl *slog.Logger, msg string, err error, args ...any)
- func InfoError(sl *slog.Logger, msg string, err error, args ...any)
- func NewJSONLogger(w io.Writer) (*slog.LevelVar, *slog.Logger)
- func NewTestLogger() (*slog.LevelVar, *bytes.Buffer, *slog.Logger)
- func RemoveTimestamp(s string) string
- func SetDebugLogging(on bool)
- func WarnError(sl *slog.Logger, msg string, err error, args ...any)
- type AtomicString
- type Handler
Examples ¶
Constants ¶
const (
LKeyError = "error"
)
Canned attr keys.
Variables ¶
var ( /* DefaultLevelVar controls the level at which Logger logs. */ DefaultLevelVar slog.LevelVar /* Default is our own default logger. It may be used instead of slog.Default(). Its level is controlled by DefaultLevelVar. */ Default = slog.New(NewHandler(&DefaultLevelVar, slog.Default().Handler())) )
Functions ¶
func ErrorError ¶
ErrorError logs the error with sl.Error, or slog.Error if sl is nil.
func ErrorLogLogger ¶
ErrorLogLogger returns a log.Logger which logs at level ERROR to logger. Writes to the returned log.Logger are written to the underlying slog.Logger as an attribute pair with the Key LKeyError and message msg. If any regular expressions are passed via debugREs, log messages written to the log.Logger which match any of the regular expressions will be logged at level DEBUG, not level ERROR. This is useful on internet-facing servers for not logging EOFs caused by banner-grabbing.
func FatalError ¶
FatalError wraps ErrorError and then calls os.Exit(1).
func NewJSONLogger ¶
NewJSONLogger returns a new Logger which wraps a slog.NewJSONHandler which writes to w. Only the LevelVar field is set in the slog.HandlerOptions passed to slog.NewJSONHandler.
func NewTestLogger ¶
NewTestLogger returns a new JSON slog.Logger wrapped in a Handler which logs to the returned bytes.Buffer. The logger's Level is set to DEBUG.
func RemoveTimestamp ¶
RemoveTimestamp removes a timestamp from a log message. This is intended to be used in test functions. The returned string will not end in a newline.
Example ¶
msg := `{"time":"2023-10-19T22:36:37.247404361+02:00",` + `"level":"ERROR","msg":"kittens"}` fmt.Printf("%s\n", RemoveTimestamp(msg))
Output: {"time":"","level":"ERROR","msg":"kittens"}
func SetDebugLogging ¶
func SetDebugLogging(on bool)
SetDebugLogging enables or disables DEBUG-level logs.
Types ¶
type AtomicString ¶
type AtomicString struct {
// contains filtered or unexported fields
}
AtomicString is like atomic.Value, but for a string. It implements slog.LogValuer.
func (*AtomicString) Load ¶
func (a *AtomicString) Load() string
Load gets the stored string. It returns the empty string if no string has been stored.
func (*AtomicString) LogValue ¶
func (a *AtomicString) LogValue() slog.Value
LogValue returns the string in a suitable form for slogging. This is handy for calling slog.Logger.With without needing to worry about someone changing the string in a.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler is identical to the default slog handler except that it allows for a settable level and that it waits until Handle is called to resolve slog.Attrs supplied by WithAttrs. Handler's methods satisfy slog.Handler.
func NewHandler ¶
NewHandler returns a new Handler wrapping h whose level is set by lv.