Documentation
¶
Index ¶
- func WithTempLevel(ctx context.Context, level slog.Level) context.Context
- func WithTempOutput(ctx context.Context, out io.Writer, only bool) context.Context
- type AsyncWriter
- type BufferedWriter
- type Handler
- type Option
- func WithAbbreviatedSourcePath() Option
- func WithAsyncErrorOutput(dst io.Writer, bufferSize int, fallback func(msg string, args ...any)) Option
- func WithContext(ctx context.Context) Option
- func WithDefaultOutput(out io.Writer) Option
- func WithErrorOutput(out io.Writer) Option
- func WithLogFile(path string) Option
- func WithMinLevel(level slog.Level) Option
- func WithModulePrefix() Option
- func WithPlaceholderReplace() Option
- func WithPrefixFunc(fn func(context.Context, slog.Record) string) Option
- func WithReplaceAttr(fn func(groups []string, a slog.Attr) slog.Attr) Option
- func WithSourcePath() Option
- func WithStackTraceFunc(fn func(error) string) Option
- func WithTimestampFormat(layout string) Option
- func WithoutSource() Option
- func WithoutTimestamp() Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithTempLevel ¶
WithTempLevel returns a new context that holds the given log level. The logger will output any log messages with the given level or higher, as long as the context is passed along.
func WithTempOutput ¶
WithTempOutput returns a new context that holds the given log output. Any writes to log/slog will be written to the given writer, as long as the context is passed along. The only flag determines whether the given writer should be the only output or an addition.
Types ¶
type AsyncWriter ¶
type AsyncWriter struct {
// contains filtered or unexported fields
}
AsyncWriter wraps an io.Writer with a buffered channel and a background goroutine, decoupling the caller from a slow or unreliable destination (mail, webhook, remote log API). Writes never block: when the buffer is full, messages are dropped and the drop count is reported via fallback.
It is transport-agnostic; the destination io.Writer owns delivery.
func NewAsyncWriter ¶
func NewAsyncWriter(dst io.Writer, bufferSize int, fallback func(msg string, args ...any)) *AsyncWriter
NewAsyncWriter returns a started AsyncWriter that drains into dst. A bufferSize <= 0 defaults to 100. fallback receives write failures and overflow reports; a nil fallback defaults to writing them to stderr. Call Close to stop the goroutine and flush remaining messages.
A custom fallback that logs back through a handler this writer is attached to can deadlock during Close; keep the fallback independent of that handler.
func (*AsyncWriter) Close ¶
func (w *AsyncWriter) Close() error
Close stops the background goroutine after draining the buffer. It is idempotent and safe to register with the handler so handler.Close drains it on shutdown.
type BufferedWriter ¶
type BufferedWriter struct {
// contains filtered or unexported fields
}
BufferedWriter wraps an io.Writer with buffering. Pass it to WithDefaultOutput or WithErrorOutput to reduce syscalls. Call Flush() to ensure all buffered data is written.
func NewBufferedWriter ¶
func NewBufferedWriter(w io.Writer, bufferSize int) *BufferedWriter
func (*BufferedWriter) Flush ¶
func (bw *BufferedWriter) Flush() error
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
func (*Handler) Enabled ¶
Enabled reports whether the handler handles records at the given level. The handler ignores records whose level is lower. Enabled is called early, before any arguments are processed, to save effort if the log event should be discarded.
func (*Handler) Handle ¶
Handle handles the Record. It will only be called if Enabled returns true. Handle methods that produce output should observe the following rules:
- If r.Time is the zero time, ignore the time.
- If an Attr's key is the empty string, ignore the Attr.
func (*Handler) WithAttrs ¶
WithAttrs returns a new Handler whose attributes consist of both the receiver's attributes and the arguments. The Handler owns the slice: it may retain, modify or discard it.
func (*Handler) WithGroup ¶
WithGroup returns a new Handler with the given group appended to the receiver's existing groups. The keys of all subsequent attributes, whether added by With or in a Record, should be qualified by the sequence of group names.
How this qualification happens is up to the Handler, so long as this Handler's attribute keys differ from those of another Handler with a different sequence of group names.
A Handler should treat WithGroup as starting a Group of Attrs that ends at the end of the log event. That is,
logger.WithGroup("s").LogAttrs(level, msg, slog.Int("a", 1), slog.Int("b", 2))
should behave like
logger.LogAttrs(level, msg, slog.Group("s", slog.Int("a", 1), slog.Int("b", 2)))
type Option ¶
type Option func(*options) error
func WithAbbreviatedSourcePath ¶
func WithAbbreviatedSourcePath() Option
WithAbbreviatedSourcePath renders the module-relative package path with each directory abbreviated to its first rune, keeping the file name intact (e.g. "w/worker.go"). Combine with WithModulePrefix to abbreviate the full module-qualified path instead. Has no effect when WithoutSource is set.
func WithAsyncErrorOutput ¶
func WithAsyncErrorOutput(dst io.Writer, bufferSize int, fallback func(msg string, args ...any)) Option
WithAsyncErrorOutput routes error-level logs to dst through an AsyncWriter, so a slow or unreliable destination never blocks the caller. The writer is registered as a closer, so handler.Close drains it on shutdown. See NewAsyncWriter for the bufferSize and fallback semantics.
func WithContext ¶
func WithDefaultOutput ¶
func WithErrorOutput ¶
func WithLogFile ¶
func WithMinLevel ¶
func WithModulePrefix ¶
func WithModulePrefix() Option
WithModulePrefix keeps the main module's path prefix on the source path, so e.g. "go.wohnparc.dev/app/moeware/worker/worker.go" is rendered in full rather than the default module-relative "worker/worker.go". It modifies WithSourcePath and WithAbbreviatedSourcePath and has no effect on its own or when WithoutSource is set. The module path is read from the binary's build info, which is always kept when unavailable (e.g. `go run` of a single file).
func WithPlaceholderReplace ¶
func WithPlaceholderReplace() Option
func WithReplaceAttr ¶
WithReplaceAttr registers a hook that is called for every non-group attribute before it is formatted, mirroring the semantics of slog.HandlerOptions.ReplaceAttr. Use it to rename or redact attributes.
groups holds the sequence of group names in effect for the attribute. The attribute's value is already resolved. Returning an Attr with an empty Key drops the attribute. If the returned Attr is itself a group, its members are processed under the same group qualification.
The hook is not called for the built-in message, level, timestamp or source fields, which this handler formats directly.
func WithSourcePath ¶
func WithSourcePath() Option
WithSourcePath renders the source as the module-relative package path (e.g. "worker/worker.go") instead of the base file name. The path is derived from the caller's package, so it does not depend on -trimpath. Combine with WithModulePrefix to keep the full module-qualified path. Has no effect when WithoutSource is set.
func WithStackTraceFunc ¶
func WithTimestampFormat ¶
WithTimestampFormat sets the layout used to render the leading timestamp, using the reference time "Mon Jan 2 15:04:05 MST 2006" (see the time package). An empty layout is ignored, keeping the default "2006-01-02 15:04:05". Has no effect when WithoutTimestamp is set.
func WithoutSource ¶
func WithoutSource() Option
func WithoutTimestamp ¶
func WithoutTimestamp() Option