MCPcopy
hub / github.com/uber-go/zap / Example_presets

Function Example_presets

example_test.go:34–66  ·  example_test.go::Example_presets
()

Source from the content-addressed store, hash-verified

32)
33
34func Example_presets() {
35 // Using zap's preset constructors is the simplest way to get a feel for the
36 // package, but they don't allow much customization.
37 logger := zap.NewExample() // or NewProduction, or NewDevelopment
38 defer logger.Sync()
39
40 const url = "http://example.com"
41
42 // In most circumstances, use the SugaredLogger. It's 4-10x faster than most
43 // other structured logging packages and has a familiar, loosely-typed API.
44 sugar := logger.Sugar()
45 sugar.Infow("Failed to fetch URL.",
46 // Structured context as loosely typed key-value pairs.
47 "url", url,
48 "attempt", 3,
49 "backoff", time.Second,
50 )
51 sugar.Infof("Failed to fetch URL: %s", url)
52
53 // In the unusual situations where every microsecond matters, use the
54 // Logger. It's even faster than the SugaredLogger, but only supports
55 // structured logging.
56 logger.Info("Failed to fetch URL.",
57 // Structured context as strongly typed fields.
58 zap.String("url", url),
59 zap.Int("attempt", 3),
60 zap.Duration("backoff", time.Second),
61 )
62 // Output:
63 // {"level":"info","msg":"Failed to fetch URL.","url":"http://example.com","attempt":3,"backoff":"1s"}
64 // {"level":"info","msg":"Failed to fetch URL: http://example.com"}
65 // {"level":"info","msg":"Failed to fetch URL.","url":"http://example.com","attempt":3,"backoff":"1s"}
66}
67
68func Example_basicConfiguration() {
69 // For some users, the presets offered by the NewProduction, NewDevelopment,

Callers

nothing calls this directly

Calls 9

NewExampleFunction · 0.92
StringFunction · 0.92
IntFunction · 0.92
DurationFunction · 0.92
SugarMethod · 0.80
InfowMethod · 0.80
SyncMethod · 0.65
InfofMethod · 0.45
InfoMethod · 0.45

Tested by

no test coverage detected