Movatterモバイル変換


[0]ホーム

URL:


slogtest

packagestandard library
go1.25.2Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 7, 2025 License:BSD-3-ClauseImports:8Imported by:13

Details

Repository

cs.opensource.google/go/go

Links

Documentation

Overview

Package slogtest implements support for testing implementations of log/slog.Handler.

Example (Parsing)

This example demonstrates one technique for testing a handler with thispackage. The handler is given abytes.Buffer to write to, and each lineof the resulting output is parsed.For JSON output,encoding/json.Unmarshal produces a result in the desiredformat when given a pointer to a map[string]any.

package mainimport ("bytes""encoding/json""log""log/slog""testing/slogtest")func main() {var buf bytes.Bufferh := slog.NewJSONHandler(&buf, nil)results := func() []map[string]any {var ms []map[string]anyfor line := range bytes.SplitSeq(buf.Bytes(), []byte{'\n'}) {if len(line) == 0 {continue}var m map[string]anyif err := json.Unmarshal(line, &m); err != nil {panic(err) // In a real test, use t.Fatal.}ms = append(ms, m)}return ms}err := slogtest.TestHandler(h, results)if err != nil {log.Fatal(err)}}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

funcRunadded ingo1.22.0

func Run(t *testing.T, newHandler func(*testing.T)slog.Handler, result func(*testing.T) map[string]any)

Run exercises aslog.Handler on the same test cases asTestHandler, butruns each case in a subtest. For each test case, it first calls newHandler toget an instance of the handler under test, then runs the test case, thencalls result to get the result. If the test case fails, it calls t.Error.

funcTestHandler

func TestHandler(hslog.Handler, results func() []map[string]any)error

TestHandler tests aslog.Handler.If TestHandler finds any misbehaviors, it returns an error for each,combined into a single error witherrors.Join.

TestHandler installs the given Handler in aslog.Logger andmakes several calls to the Logger's output methods.The Handler should be enabled for levels Info and above.

The results function is invoked after all such calls.It should return a slice of map[string]any, one for each call to a Logger output method.The keys and values of the map should correspond to the keys and values of the Handler'soutput. Each group in the output should be represented as its own nested map[string]any.The standard keysslog.TimeKey,slog.LevelKey andslog.MessageKey should be used.

If the Handler outputs JSON, then callingencoding/json.Unmarshal with a `map[string]any`will create the right data structure.

If a Handler intentionally drops an attribute that is checked by a test,then the results function should check for its absence and add it to the map it returns.

Types

This section is empty.

Source Files

View all Source files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp