Imports #
"errors"
"log/slog"
"time"
"context"
"fmt"
"io"
"log/slog"
"log/slog/internal/buffer"
"strconv"
"time"
"errors"
"log/slog"
"time"
"context"
"fmt"
"io"
"log/slog"
"log/slog/internal/buffer"
"strconv"
"time"
var testAttrs = []slog.Attr{...}var testDuration = *ast.BinaryExprvar testError = *ast.CallExprvar testInt = 32768const testMessage = "Test logging, but use a somewhat realistic message length."var testString = "7e3b3b2aaeff56a7108fe11e154200dd/7819479873059528190"var testTime = *ast.CallExprconst wantText = "time=1651363200 level=0 msg=Test logging, but use a somewhat realistic message length. string=7e3b3b2aaeff56a7108fe11e154200dd/7819479873059528190 status=32768 duration=23000000000 time=1651363200 error=fail\n"An asyncHandler simulates a Handler that passes Records to a background goroutine for processing. Because sending to a channel can be expensive due to locking, we simulate a lock-free queue by adding the Record to a ring buffer. Omitting the locking makes this little more than a copy of the Record, but that is a worthwhile thing to measure because Records are on the large side. Since nothing actually reads from the ring buffer, it can handle an arbitrary number of Records without either blocking or allocation.
type asyncHandler struct {
ringBuffer [100]slog.Record
next int
}A disabledHandler's Enabled method always returns false.
type disabledHandler struct {
}A fastTextHandler writes a Record to an io.Writer in a format similar to slog.TextHandler, but without quoting or locking. It has a few other performance-motivated shortcuts, like writing times as seconds since the epoch instead of strings. It is intended to represent a high-performance Handler that synchronously writes text (as opposed to binary).
type fastTextHandler struct {
w io.Writer
}func (disabledHandler) Enabled(context.Context, slog.Level) boolfunc (h *fastTextHandler) Enabled(context.Context, slog.Level) boolfunc (*asyncHandler) Enabled(context.Context, slog.Level) boolfunc (h *fastTextHandler) Handle(_ context.Context, r slog.Record) errorfunc (disabledHandler) Handle(context.Context, slog.Record) errorfunc (h *asyncHandler) Handle(_ context.Context, r slog.Record) errorfunc (*asyncHandler) WithAttrs([]slog.Attr) slog.Handlerfunc (h *fastTextHandler) WithAttrs([]slog.Attr) slog.Handlerfunc (disabledHandler) WithAttrs([]slog.Attr) slog.Handlerfunc (*fastTextHandler) WithGroup(string) slog.Handlerfunc (*asyncHandler) WithGroup(string) slog.Handlerfunc (disabledHandler) WithGroup(string) slog.Handlerfunc (h *fastTextHandler) appendTime(buf *buffer.Buffer, t time.Time)func (h *fastTextHandler) appendValue(buf *buffer.Buffer, v slog.Value)func newAsyncHandler() *asyncHandlerfunc newFastTextHandler(w io.Writer) slog.HandlerGenerated with Arrow