| 66 | } |
| 67 | |
| 68 | func Example_basicConfiguration() { |
| 69 | // For some users, the presets offered by the NewProduction, NewDevelopment, |
| 70 | // and NewExample constructors won't be appropriate. For most of those |
| 71 | // users, the bundled Config struct offers the right balance of flexibility |
| 72 | // and convenience. (For more complex needs, see the AdvancedConfiguration |
| 73 | // example.) |
| 74 | // |
| 75 | // See the documentation for Config and zapcore.EncoderConfig for all the |
| 76 | // available options. |
| 77 | rawJSON := []byte(`{ |
| 78 | "level": "debug", |
| 79 | "encoding": "json", |
| 80 | "outputPaths": ["stdout", "/tmp/logs"], |
| 81 | "errorOutputPaths": ["stderr"], |
| 82 | "initialFields": {"foo": "bar"}, |
| 83 | "encoderConfig": { |
| 84 | "messageKey": "message", |
| 85 | "levelKey": "level", |
| 86 | "levelEncoder": "lowercase" |
| 87 | } |
| 88 | }`) |
| 89 | |
| 90 | var cfg zap.Config |
| 91 | if err := json.Unmarshal(rawJSON, &cfg); err != nil { |
| 92 | panic(err) |
| 93 | } |
| 94 | logger := zap.Must(cfg.Build()) |
| 95 | defer logger.Sync() |
| 96 | |
| 97 | logger.Info("logger construction succeeded") |
| 98 | // Output: |
| 99 | // {"level":"info","message":"logger construction succeeded","foo":"bar"} |
| 100 | } |
| 101 | |
| 102 | func Example_advancedConfiguration() { |
| 103 | // The bundled Config struct only supports the most common configuration |