MCPcopy Index your code
hub / github.com/bracesdev/errtrace / Example_logWithSlog

Function Example_logWithSlog

example_trace_test.go:85–99  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

83}
84
85func Example_logWithSlog() {
86 // This example demonstrates how to log an errtrace-wrapped error
87 // with the slog package.
88 // Unlike LogAttr, we're able to use any key name here.
89 logger, printLogOutput := newExampleLogger()
90
91 if err := f1(); err != nil {
92 logger.Error("f1 failed", "my-error", err)
93 }
94
95 printLogOutput()
96
97 // Output:
98 // {"level":"ERROR","msg":"f1 failed","my-error":"failed\n\nbraces.dev/errtrace_test.f3\n\t/path/to/errtrace/example_trace_test.go:3\nbraces.dev/errtrace_test.f2\n\t/path/to/errtrace/example_trace_test.go:2\nbraces.dev/errtrace_test.f1\n\t/path/to/errtrace/example_trace_test.go:1\n"}
99}
100
101// newExampleLogger creates a new slog.Logger for use in examples.
102// It omits timestamps from the output to allow for output matching,

Callers

nothing calls this directly

Calls 3

newExampleLoggerFunction · 0.85
f1Function · 0.70
ErrorMethod · 0.45

Tested by

no test coverage detected