sandipb / zap-examples Goto Github PK
View Code? Open in Web Editor NEWExamples of using Uber's zap Go logging library
License: MIT License
Examples of using Uber's zap Go logging library
License: MIT License
I modified the example to experiment. zap is good if used along the published cases, but not friendly on customization.
What I am expecting is that since zap has parent and child logger relationship, I would expect the settings being passed down if they belong to the same descendants paths.
While it looks like the case in first half of example, but it does not look like the case for zap.WrapCore (prints debug and info messages after // ****** )
Know that WithOptions is just cloning, so it is probably doing the right thing, and not the right method to call. But then how can I do that?
cfg := zap.Config{
Encoding: "json",
Level: zap.NewAtomicLevelAt(zapcore.DebugLevel),
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
EncoderConfig: zapcore.EncoderConfig{
MessageKey: "message",
LevelKey: "level",
EncodeLevel: zapcore.CapitalLevelEncoder,
TimeKey: "time",
EncodeTime: zapcore.ISO8601TimeEncoder,
CallerKey: "caller",
EncodeCaller: zapcore.ShortCallerEncoder,
},
}
logger, _ = cfg.Build()
childlogger := logger.With(zap.String("logger", "child generation 1"))
logger.Debug("This is a DEBUG message")
logger.Info("This is an INFO message")
logger.Info("This is an INFO message with fields", zap.String("region", "us-west"), zap.Int("id", 2))
// ****** Child logger also has the same logging level settings ****
childlogger.Debug("This is a DEBUG message from child logger")
childlogger.Info("This is an INFO message from child logger")
fmt.Printf("\n*** Same logger with console logging enabled instead with Info level now\n\n")
logger.WithOptions(
zap.WrapCore(
func(zapcore.Core) zapcore.Core {
return zapcore.NewCore(zapcore.NewConsoleEncoder(cfg.EncoderConfig), zapcore.AddSync(os.Stderr), zapcore.InfoLevel)
})).Debug("This is a DEBUG message, but should not be printed")
// ****** new settings no longer honored after this line ****
logger.Debug("This is a DEBUG message, but should not be printed")
childlogger.Debug("This is a DEBUG message from child logger")
childlogger.Info("This is an INFO message from child logger")
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.