1package ctxzap_test
2
3import (
4	"context"
5
6	"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
7	"github.com/grpc-ecosystem/go-grpc-middleware/tags"
8	pb_testproto "github.com/grpc-ecosystem/go-grpc-middleware/testing/testproto"
9	"go.uber.org/zap"
10)
11
12var zapLogger *zap.Logger
13
14// Simple unary handler that adds custom fields to the requests's context. These will be used for all log statements.
15func ExampleExtract_unary() {
16	_ = func(ctx context.Context, ping *pb_testproto.PingRequest) (*pb_testproto.PingResponse, error) {
17		// Add fields the ctxtags of the request which will be added to all extracted loggers.
18		grpc_ctxtags.Extract(ctx).Set("custom_tags.string", "something").Set("custom_tags.int", 1337)
19
20		// Extract a single request-scoped zap.Logger and log messages.
21		l := ctxzap.Extract(ctx)
22		l.Info("some ping")
23		l.Info("another ping")
24		return &pb_testproto.PingResponse{Value: ping.Value}, nil
25	}
26}
27