Documentation
¶
Index ¶
- Constants
- Variables
- func NewTestHandler(t testing.T, opts *slog.HandlerOptions) slog.Handler
- func OnJournalStream(f *os.File) bool
- type Buffer
- func (b *Buffer) Free()
- func (b *Buffer) Len() int
- func (b *Buffer) Reset()
- func (b *Buffer) SetLen(n int)
- func (b *Buffer) String() string
- func (b *Buffer) Write(p []byte) (int, error)
- func (b *Buffer) WriteByte(c byte) error
- func (b *Buffer) WriteNewLine()
- func (b *Buffer) WriteRune(r rune) error
- func (b *Buffer) WriteString(s string) (int, error)
- type TextHandler
- func (h *TextHandler) Enabled(_ context.Context, level slog.Level) bool
- func (h *TextHandler) Handle(ctx context.Context, r slog.Record) error
- func (h *TextHandler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (h *TextHandler) WithGroup(name string) slog.Handler
- func (h *TextHandler) WithJournalPriority(level func(context.Context, slog.Level) slog.Level) *TextHandler
Constants ¶
const ( TimeFormat = "2006-01-02T15:04:05.000Z0700" MiniTimeFormat = "15:04:05.000" )
TimeFormat is the time format to use for plain (non-JSON) output. This is a version of RFC3339 that contains millisecond precision.
const ModuleKey = "module"
const OutboardKey = "outboard"
Variables ¶
var ( Trace = slog.LevelDebug - 4 Debug = slog.LevelDebug Info = slog.LevelInfo Warn = slog.LevelWarn Error = slog.LevelError )
Functions ¶
func NewTestHandler ¶
func OnJournalStream ¶ added in v0.18.0
OnJournalStream reports whether f is the stream systemd connected to journald. systemd sets JOURNAL_STREAM to that stream's device and inode, and the variable is inherited by children whose output goes elsewhere, so it only counts when it matches f itself.
Types ¶
type Buffer ¶
type Buffer []byte
buffer adapted from go/src/fmt/print.go
func (*Buffer) WriteNewLine ¶
func (b *Buffer) WriteNewLine()
WriteNewLine ends the buffer with a newline unless it already ends in one, as it does after a multi-line attr.
type TextHandler ¶
type TextHandler struct {
// contains filtered or unexported fields
}
TextHandler is a [Handler] that writes Records to an io.Writer as a sequence of key=value pairs separated by spaces and followed by a newline.
func NewTextHandler ¶
func NewTextHandler(w io.Writer, opts *slog.HandlerOptions) *TextHandler
NewTextHandler creates a TextHandler that writes to w, using the given options. If opts is nil, the default options are used.
func (*TextHandler) Enabled ¶
Enabled reports whether the handler handles records at the given level. The handler ignores records whose level is lower.
func (*TextHandler) Handle ¶
Handle formats its argument [Record] as a single line of space-separated key=value items.
If the Record's time is zero, the time is omitted. Otherwise, the key is "time" and the value is output in RFC3339 format with millisecond precision.
If the Record's level is zero, the level is omitted. Otherwise, the key is "level" and the value of [Level.String] is output.
If the AddSource option is set and source information is available, the key is "source" and the value is output as FILE:LINE.
The message's key is "msg".
To modify these or other attributes, or remove them from the output, use [HandlerOptions.ReplaceAttr].
If a value implements encoding.TextMarshaler, the result of MarshalText is written. Otherwise, the result of fmt.Sprint is written.
Keys and values are quoted with strconv.Quote if they contain Unicode space characters, non-printing characters, '"' or '='.
Keys inside groups consist of components (keys or group names) separated by dots. No further escaping is performed. Thus there is no way to determine from the key "a.b.c" whether there are two groups "a" and "b" and a key "c", or a single group "a.b" and a key "c", or single group "a" and a key "b.c". If it is necessary to reconstruct the group structure of a key even in the presence of dots inside components, use [HandlerOptions.ReplaceAttr] to encode that information in the key.
Each call to Handle results in a single serialized call to io.Writer.Write.
func (*TextHandler) WithAttrs ¶
func (h *TextHandler) WithAttrs(attrs []slog.Attr) slog.Handler
WithAttrs returns a new TextHandler whose attributes consists of h's attributes followed by attrs.
func (*TextHandler) WithJournalPriority ¶ added in v0.18.0
func (h *TextHandler) WithJournalPriority(level func(context.Context, slog.Level) slog.Level) *TextHandler
WithJournalPriority returns a copy of h that starts every line it writes with the <N> syslog priority prefix journald reads off a stream, so that `journalctl -p warning` finds the warnings. Without it journald files every line at its default priority, info, whatever level the text says.
level picks the level a record's priority comes from, given the context it was logged with and the level it prints at. Nil means the printed level.
Only use it on a stream that is known to reach journald (see OnJournalStream). Anywhere else the prefixes would print as raw text.