Documentation
¶
Overview ¶
Package slogutil provides handlers, formatters, and helpers for log/slog.
Index ¶
- func Database(name, host string, port int) slog.Attr
- func Duration(d time.Duration) slog.Attr
- func Error(err error) slog.Attr
- func HTTPRequest(r *http.Request) slog.Attr
- func HTTPStatus(code int) slog.Attr
- func Latency(d time.Duration) slog.Attr
- func LevelRoute(routes map[slog.Level]slog.Handler) slog.Handler
- func Multi(handlers ...slog.Handler) slog.Handler
- func PrettyHandler(w io.Writer, opts *PrettyHandlerOptions) slog.Handler
- func RequestID(id string) slog.Attr
- func Sampling(handler slog.Handler, rate int) slog.Handler
- func Stringer(key string, v fmt.Stringer) slog.Attr
- func TraceID(id string) slog.Attr
- func UserID(id string) slog.Attr
- type PrettyHandlerOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Database ¶ added in v0.2.0
Database returns an attribute group with database connection details.
func Duration ¶
Duration returns a slog.Attr with key "duration" and a human-readable string representation of d (e.g. "1.5s", "200ms").
func Error ¶
Error returns a slog.Attr with key "error" and the error message as value. If err is nil, the attr value is "<nil>".
func HTTPRequest ¶
HTTPRequest returns a slog.Attr group named "http_request" containing the method, path, and remote address from the given request.
func HTTPStatus ¶
HTTPStatus returns a slog.Attr with key "status" and the HTTP status code as an integer value.
func Latency ¶ added in v0.2.0
Latency returns an attribute with a latency duration in milliseconds.
func LevelRoute ¶
LevelRoute returns a slog.Handler that routes records to different handlers based on the log level. If an exact level match is not found in routes, it falls back to the next lower level's handler. If no handler matches, the record is silently dropped.
Common usage routes errors to a stderr handler and info messages to stdout:
slog.New(slogutil.LevelRoute(map[slog.Level]slog.Handler{
slog.LevelInfo: stdoutHandler,
slog.LevelError: stderrHandler,
}))
func Multi ¶
Multi returns a slog.Handler that fans out log records to all provided handlers. Enabled returns true if any underlying handler is enabled for the given level. Handle sends each record to every handler; the first error encountered is returned.
func PrettyHandler ¶
func PrettyHandler(w io.Writer, opts *PrettyHandlerOptions) slog.Handler
PrettyHandler returns a new slog.Handler that writes colorized, human-readable log lines to w. It is intended for development console output.
func Sampling ¶
Sampling returns a slog.Handler that only passes 1 in every rate records to the underlying handler. This is useful for high-volume code paths where logging every event would be too expensive. Records at slog.LevelError or above are always logged regardless of the sampling rate. The counter is incremented atomically and is safe for concurrent use.
func Stringer ¶
Stringer returns a slog.Attr with the given key and the result of calling String() on v. This is a convenience for types that implement fmt.Stringer.
Types ¶
type PrettyHandlerOptions ¶
type PrettyHandlerOptions struct {
// Level is the minimum level to log. If nil, defaults to slog.LevelInfo.
Level slog.Leveler
// TimeFormat is the time layout string. If empty, defaults to time.DateTime.
TimeFormat string
}
PrettyHandlerOptions configures the PrettyHandler.