Documentation
¶
Index ¶
Constants ¶
const ( TimeKey = slog.TimeKey LevelKey = slog.LevelKey MessageKey = slog.MessageKey SourceKey = slog.SourceKey )
const ( LevelDebug = slog.LevelDebug LevelInfo = slog.LevelInfo LevelWarn = slog.LevelWarn LevelError = slog.LevelError )
const ( KindAny = slog.KindAny KindBool = slog.KindBool KindDuration = slog.KindDuration KindFloat64 = slog.KindFloat64 KindInt64 = slog.KindInt64 KindString = slog.KindString KindTime = slog.KindTime KindUint64 = slog.KindUint64 KindGroup = slog.KindGroup KindLogValuer = slog.KindLogValuer )
Variables ¶
var ( Any = slog.Any AnyValue = slog.AnyValue Bool = slog.Bool Duration = slog.Duration Group = slog.Group GroupValue = slog.GroupValue Int = slog.Int IntValue = slog.IntValue New = slog.New NewJSONHandler = slog.NewJSONHandler NewRecord = slog.NewRecord String = slog.String StringValue = slog.StringValue Time = slog.Time )
Functions ¶
func CountEmptyGroups ¶
CountEmptyGroups returns the number of empty group values in its argument.
Types ¶
type AttrFormat ¶
type AttrFormat struct {
Prefix string // Printed instead of attr key.
Suffix string // Printed after the attr value.
MinWidth int // Minimum width of the attr value.
MaxWidth int // Maximum width of the attr value. -1 means no limit. 0 means no value.
AlignRight bool // MinWidth padding added to the left.
TruncFromStart bool // MaxWidth truncate from the beginning.
SkipQuote bool // Do not quote the value, even if needed.
}
AttrFormat specifies how to format an attribute.
Value {MaxWidth: -1} results in outputting just the value, without attrSep, key and '='. Zero value results in outputting nothing, same as removing the attr using ReplaceAttr.
Special cases:
- LevelKey with MinWidth=3 and MaxWidth=3 outputs short level string (e.g. "WRN").
type HandlerOptions ¶
type HandlerOptions = slog.HandlerOptions
type LayoutHandler ¶
type LayoutHandler struct {
// contains filtered or unexported fields
}
func NewLayoutHandler ¶
func NewLayoutHandler(w io.Writer, opts *LayoutHandlerOptions) *LayoutHandler
NewLayoutHandler creates a LayoutHandler that writes to w, using the given options. If opts is nil, the default options are used.
func (*LayoutHandler) Enabled ¶
func (h *LayoutHandler) Enabled(_ context.Context, l Level) bool
Enabled reports whether l is greater than or equal to the minimum level.
func (*LayoutHandler) Handle ¶
func (h *LayoutHandler) Handle(_ context.Context, r Record) error
Handle is the internal implementation of Handler.Handle used by TextHandler and LayoutHandler.
func (*LayoutHandler) WithAttrs ¶
func (h *LayoutHandler) WithAttrs(as []Attr) Handler
func (*LayoutHandler) WithGroup ¶
func (h *LayoutHandler) WithGroup(name string) Handler
type LayoutHandlerOptions ¶
type LayoutHandlerOptions struct {
// AddSource causes the handler to compute the source code position
// of the log statement and add a SourceKey attribute to the output.
AddSource bool
// Level reports the minimum record level that will be logged.
// The handler discards records with lower levels.
// If Level is nil, the handler assumes LevelInfo.
// The handler calls Level.Level for each record processed;
// to adjust the minimum level dynamically, use a LevelVar.
Level Leveler
// ReplaceAttr is called to rewrite each non-group attribute before it is logged.
// The attribute's value has been resolved (see [Value.Resolve]).
// If ReplaceAttr returns a zero Attr, the attribute is discarded.
//
// The built-in attributes with keys "time", "level", "source", and "msg"
// are passed to this function, except that time is omitted
// if zero, and source is omitted if AddSource is false.
//
// The first argument is a list of currently open groups that contain the
// Attr. It must not be retained or modified. ReplaceAttr is never called
// for Group attributes, only their contents. For example, the attribute
// list
//
// Int("a", 1), Group("g", Int("b", 2)), Int("c", 3)
//
// results in consecutive calls to ReplaceAttr with the following arguments:
//
// nil, Int("a", 1)
// []string{"g"}, Int("b", 2)
// nil, Int("c", 3)
//
// ReplaceAttr can be used to change the default keys of the built-in
// attributes, convert types (for example, to replace a `time.Time` with the
// integer seconds since the Unix epoch), sanitize personal information, or
// remove attributes from the output.
ReplaceAttr func(groups []string, a Attr) Attr
// RecordTimeFormat specifies the time format for the built-in slog.TimeKey attribute
// instead of default (RFC3339 with millisecond precision).
RecordTimeFormat string
// TimeFormat specifies the time format for user-defined time.Time attributes
// instead of default (RFC3339 with millisecond precision).
TimeFormat string
// Format specifies per-attribute formatting options.
//
// If an attribute's key is present in the map,
// the corresponding formatting options are applied when outputting the attribute,
// otherwise the attribute is output in the default slog.TextHandler format.
//
// Key should be the full key, including group prefixes separated by '.'.
//
// All attributes included in Format are output without attribute separator (' '),
// key and '='. Include these parts in format as prefix if needed.
//
// Use zero AttrFormat value to remove the attr from output.
Format map[string]AttrFormat
// PrefixKeys specifies keys that, if present, output just before the message key,
// in order given by the slice.
//
// Key should be the full key, including group prefixes separated by '.'.
//
// If multiple attributes have the same key only the last one is output.
// If slog.MessageKey is present in PrefixKeys, it is ignored.
// If same key is present multiple times in PrefixKeys, all but the first are ignored.
// If same key is present in both PrefixKeys and SuffixKeys, it is output as a prefix.
//
// Keys not present in PrefixKeys and SuffixKeys are output as usual,
// between the message and the suffix keys, in order they were added.
PrefixKeys []string
// SuffixKeys specifies keys that, if present, output after all other attributes,
// in order given by the slice.
//
// Key should be the full key, including group prefixes separated by '.'.
//
// If multiple attributes have the same key only the last one is output.
// If slog.MessageKey is present in SuffixKeys, it is ignored.
// If same key is present multiple times in SuffixKeys, all but the first are ignored.
// If same key is present in both PrefixKeys and SuffixKeys, it is output as a prefix.
//
// Keys not present in PrefixKeys and SuffixKeys are output as usual,
// between the message and the suffix keys, in order they were added.
SuffixKeys []string
}
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 *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 ¶
func (h *TextHandler) Enabled(ctx context.Context, level Level) bool
Enabled reports whether the handler handles records at the given level. The handler ignores records whose level is lower.
func (*TextHandler) Handle ¶
func (h *TextHandler) Handle(ctx context.Context, r Record) error
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.
The level's key is "level" and its value is the result of calling [Level.String].
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 []Attr) Handler
WithAttrs returns a new TextHandler whose attributes consists of h's attributes followed by attrs.
func (*TextHandler) WithGroup ¶
func (h *TextHandler) WithGroup(name string) Handler
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package benchmarks contains benchmarks for slog.
|
Package benchmarks contains benchmarks for slog. |
|
Package buffer provides a pool-allocated byte buffer.
|
Package buffer provides a pool-allocated byte buffer. |
|
Package race contains helper functions for manually instrumenting code for the race detector.
|
Package race contains helper functions for manually instrumenting code for the race detector. |