|
32 | 32 | // they need to count every allocation and when they'd prefer a more familiar, |
33 | 33 | // loosely typed API. |
34 | 34 | // |
35 | | -// Choosing a Logger |
| 35 | +// # Choosing a Logger |
36 | 36 | // |
37 | 37 | // In contexts where performance is nice, but not critical, use the |
38 | 38 | // SugaredLogger. It's 4-10x faster than other structured logging packages and |
|
41 | 41 | // variadic number of key-value pairs. (For more advanced use cases, they also |
42 | 42 | // accept strongly typed fields - see the SugaredLogger.With documentation for |
43 | 43 | // details.) |
44 | | -// sugar := zap.NewExample().Sugar() |
45 | | -// defer sugar.Sync() |
46 | | -// sugar.Infow("failed to fetch URL", |
47 | | -// "url", "http://example.com", |
48 | | -// "attempt", 3, |
49 | | -// "backoff", time.Second, |
50 | | -// ) |
51 | | -// sugar.Infof("failed to fetch URL: %s", "http://example.com") |
| 44 | +// |
| 45 | +// sugar := zap.NewExample().Sugar() |
| 46 | +// defer sugar.Sync() |
| 47 | +// sugar.Infow("failed to fetch URL", |
| 48 | +// "url", "http://example.com", |
| 49 | +// "attempt", 3, |
| 50 | +// "backoff", time.Second, |
| 51 | +// ) |
| 52 | +// sugar.Infof("failed to fetch URL: %s", "http://example.com") |
52 | 53 | // |
53 | 54 | // By default, loggers are unbuffered. However, since zap's low-level APIs |
54 | 55 | // allow buffering, calling Sync before letting your process exit is a good |
|
57 | 58 | // In the rare contexts where every microsecond and every allocation matter, |
58 | 59 | // use the Logger. It's even faster than the SugaredLogger and allocates far |
59 | 60 | // less, but it only supports strongly-typed, structured logging. |
60 | | -// logger := zap.NewExample() |
61 | | -// defer logger.Sync() |
62 | | -// logger.Info("failed to fetch URL", |
63 | | -// zap.String("url", "http://example.com"), |
64 | | -// zap.Int("attempt", 3), |
65 | | -// zap.Duration("backoff", time.Second), |
66 | | -// ) |
| 61 | +// |
| 62 | +// logger := zap.NewExample() |
| 63 | +// defer logger.Sync() |
| 64 | +// logger.Info("failed to fetch URL", |
| 65 | +// zap.String("url", "http://example.com"), |
| 66 | +// zap.Int("attempt", 3), |
| 67 | +// zap.Duration("backoff", time.Second), |
| 68 | +// ) |
67 | 69 | // |
68 | 70 | // Choosing between the Logger and SugaredLogger doesn't need to be an |
69 | 71 | // application-wide decision: converting between the two is simple and |
70 | 72 | // inexpensive. |
71 | | -// logger := zap.NewExample() |
72 | | -// defer logger.Sync() |
73 | | -// sugar := logger.Sugar() |
74 | | -// plain := sugar.Desugar() |
75 | 73 | // |
76 | | -// Configuring Zap |
| 74 | +// logger := zap.NewExample() |
| 75 | +// defer logger.Sync() |
| 76 | +// sugar := logger.Sugar() |
| 77 | +// plain := sugar.Desugar() |
| 78 | +// |
| 79 | +// # Configuring Zap |
77 | 80 | // |
78 | 81 | // The simplest way to build a Logger is to use zap's opinionated presets: |
79 | 82 | // NewExample, NewProduction, and NewDevelopment. These presets build a logger |
80 | 83 | // with a single function call: |
81 | | -// logger, err := zap.NewProduction() |
82 | | -// if err != nil { |
83 | | -// log.Fatalf("can't initialize zap logger: %v", err) |
84 | | -// } |
85 | | -// defer logger.Sync() |
| 84 | +// |
| 85 | +// logger, err := zap.NewProduction() |
| 86 | +// if err != nil { |
| 87 | +// log.Fatalf("can't initialize zap logger: %v", err) |
| 88 | +// } |
| 89 | +// defer logger.Sync() |
86 | 90 | // |
87 | 91 | // Presets are fine for small projects, but larger projects and organizations |
88 | 92 | // naturally require a bit more customization. For most users, zap's Config |
|
94 | 98 | // go.uber.org/zap/zapcore. See the package-level AdvancedConfiguration |
95 | 99 | // example for sample code. |
96 | 100 | // |
97 | | -// Extending Zap |
| 101 | +// # Extending Zap |
98 | 102 | // |
99 | 103 | // The zap package itself is a relatively thin wrapper around the interfaces |
100 | 104 | // in go.uber.org/zap/zapcore. Extending zap to support a new encoding (e.g., |
|
106 | 110 | // Similarly, package authors can use the high-performance Encoder and Core |
107 | 111 | // implementations in the zapcore package to build their own loggers. |
108 | 112 | // |
109 | | -// Frequently Asked Questions |
| 113 | +// # Frequently Asked Questions |
110 | 114 | // |
111 | 115 | // An FAQ covering everything from installation errors to design decisions is |
112 | 116 | // available at https://github.com/uber-go/zap/blob/master/FAQ.md. |
|
0 commit comments