const ( // DebugLevel logs are typically voluminous, and are usually disabled in production. DebugLevel Level = iota - 1 // InfoLevel is the default logging priority. InfoLevel // WarnLevel logs are more important than Info, but don't need individual human review. WarnLevel // ErrorLevel logs are high-priority. If an application is running smoothly, it shouldn't generate any error-level logs. ErrorLevel // DPanicLevel logs are particularly important errors. In development the logger panics after writing the message. DPanicLevel // PanicLevel logs a message, then panics. PanicLevel // FatalLevel logs a message, then calls os.Exit(1). FatalLevel
funcmain() { // For some users, the presets offered by the NewProduction, NewDevelopment, // and NewExample constructors won't be appropriate. For most of those // users, the bundled Config struct offers the right balance of flexibility // and convenience. (For more complex needs, see the AdvancedConfiguration // example.) // // See the documentation for Config and zapcore.EncoderConfig for all the // available options. rawJSON := []byte(`{ "level": "debug", "encoding": "json", "outputPaths": ["stdout", "/tmp/logs"], "errorOutputPaths": ["stderr"], "initialFields": {"foo": "bar"}, "encoderConfig": { "messageKey": "message", "levelKey": "level", "levelEncoder": "lowercase" } }`)
var cfg zap.Config if err := json.Unmarshal(rawJSON, &cfg); err != nil { panic(err) } logger, err := cfg.Build() if err != nil { panic(err) } defer logger.Sync()
funcmain() { // The bundled Config struct only supports the most common configuration // options. More complex needs, like splitting logs between multiple files // or writing to non-file outputs, require use of the zapcore package. // // In this example, imagine we're both sending our logs to Kafka and writing // them to the console. We'd like to encode the console output and the Kafka // topics differently, and we'd also like special treatment for // high-priority logs.
// Assume that we have clients for two Kafka topics. The clients implement // zapcore.WriteSyncer and are safe for concurrent use. (If they only // implement io.Writer, we can use zapcore.AddSync to add a no-op Sync // method. If they're not safe for concurrent use, we can add a protecting // mutex with zapcore.Lock.) topicDebugging := zapcore.AddSync(ioutil.Discard) topicErrors := zapcore.AddSync(ioutil.Discard)
// High-priority output should also go to standard error, and low-priority // output should also go to standard out. consoleDebugging := zapcore.Lock(os.Stdout) consoleErrors := zapcore.Lock(os.Stderr)
// Optimize the Kafka output for machine consumption and the console output // for human operators. kafkaEncoder := zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()) consoleEncoder := zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())
// Join the outputs, encoders, and level-handling functions into // zapcore.Cores, then tee the four cores together. core := zapcore.NewTee( zapcore.NewCore(kafkaEncoder, topicErrors, highPriority), zapcore.NewCore(consoleEncoder, consoleErrors, highPriority), zapcore.NewCore(kafkaEncoder, topicDebugging, lowPriority), zapcore.NewCore(consoleEncoder, consoleDebugging, lowPriority), )
// From a zapcore.Core, it's easy to construct a Logger. logger := zap.New(core) defer logger.Sync() logger.Info("constructed a logger") }