| 32 | ) |
| 33 | |
| 34 | func 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 | |
| 68 | func Example_basicConfiguration() { |
| 69 | // For some users, the presets offered by the NewProduction, NewDevelopment, |