Esempio n. 1
0
// doFirstThing illustrates setting a new logging context.
// Any message logged with the new context will have values for
// a and b.
func doFirstThing(ctx context.Context, a, b int) error {
	// add some logging to the context
	ctx = log.NewContext(ctx,
		log.Property{Key: "a", Value: a},
		log.Property{Key: "b", Value: b})

	// ... perform some more processing and then

	return doSecondThing(ctx)
}
Esempio n. 2
0
func ExampleNewContext(ctx context.Context, n1, n2 int) error {
	ctx = slog.NewContext(ctx,
		slog.Property{Key: "n1", Value: n1},
		slog.Property{Key: "n2", Value: n2})

	if err := doSomethingWith(n1, n2); err != nil {
		return slog.Error(ctx, "doSomethingWith failed",
			slog.WithError(err))
	}

	slog.Debug(ctx, "did something with")

	// ... more processing and then ...

	return nil
}