Documentation
¶
Overview ¶
Package zapcfg provides a collection of sophisticated uber-go/zap loggers.
For each logger, the following aspects are considered:
| Setting | Option #1 | Option #2 | | -------------------- | -------------- | --------- | | Environment | Dev | Prod | | Format | TSV with color | JSON | | Output Target | Console | File | | Separate Errors | Yes | No | | Compress Legacy Logs | Yes | No |
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetDevelopmentLogger ¶
GetDevelopmentLogger returns a sophisticated, customized logger for development.
Logs with level below than ERROR will be written to:
a) stdout in TSV format with colored level; b) normal log files in JSON format with rotation and expiration;
Logs with level ERROR or above will be written to:
a) stderr in TSV format with colored level; b) error log files in JSON format with rotation (max size is 10MB, retain at most 20 files) and expiration (14 days);
Logs with level DPANIC or above will cause panic after writing the message.
Log files won't be created if the given log file path is empty or blank.
Example ¶
package main import ( "time" "bitbucket.org/ai69/zapcfg" "go.uber.org/zap" ) func main() { logger := zapcfg.GetDevelopmentLogger("log/access.log", "log/error.log") defer logger.Sync() logger.Debug("This is a DEBUG message") logger.Info("This is an INFO message") logger.Info("This is an INFO message with fields", zap.Int("id", 1), zap.Duration("sleep", 128*time.Millisecond), zap.Time("now", time.Now())) logger.Sugar().Infof("The answer to life the universe and everything = %d", 42) logger.Warn("This is a WARN message") logger.Error("This is an ERROR message") }
Output:
func GetNoopLogger ¶ added in v0.2.0
GetNoopLogger returns a logger enabled at fatal level, it basically logs nothing.
Example ¶
package main import ( "time" "bitbucket.org/ai69/zapcfg" "go.uber.org/zap" ) func main() { logger := zapcfg.GetNoopLogger() defer logger.Sync() logger.Debug("This is a DEBUG message") logger.Info("This is an INFO message") logger.Info("This is an INFO message with fields", zap.Int("id", 1), zap.Duration("sleep", 64*time.Millisecond), zap.Time("now", time.Now())) logger.Warn("This is a WARN message") logger.Error("This is an ERROR message") logger.DPanic("This is a DPANIC message") }
Output:
func GetProductionLogger ¶
GetProductionLogger returns a sophisticated, customized logger for production deployment.
Logs with level below than INFO will be ignored.
Logs with level INFO or above will be written to:
a) stdout in JSON format; b) log files in JSON format with rotation (max size is 10MB), expiration (30 days) and compression (.gz);
Logs with level PANIC or above will cause panic after writing the message.
Log files won't be created if the given log file path is empty or blank.
Example ¶
package main import ( "time" "bitbucket.org/ai69/zapcfg" "go.uber.org/zap" ) func main() { logger := zapcfg.GetProductionLogger("log/app.log") defer logger.Sync() logger.Debug("This is a DEBUG message") logger.Info("This is an INFO message") logger.Info("This is an INFO message with fields", zap.Int("id", 1), zap.Duration("sleep", 64*time.Millisecond), zap.Time("now", time.Now())) logger.Warn("This is a WARN message") logger.Error("This is an ERROR message") logger.DPanic("This is a DPANIC message") }
Output:
func SetGlobalDevelopmentLogger ¶
func SetGlobalDevelopmentLogger(normalLogPath, errorLogPath string)
SetGlobalDevelopmentLogger sets the logger returned from GetDevelopmentLogger() as the global Logger and SugaredLogger of zap.
Example ¶
package main import ( "time" "bitbucket.org/ai69/zapcfg" "go.uber.org/zap" ) func main() { zapcfg.SetGlobalDevelopmentLogger("", "log/error.log") logger := zap.L().Sugar().Named("dev") logger.Debug("This is a DEBUG message") logger.Info("This is an INFO message") logger.Infow("This is an INFO message with fields", "id", 1, "sleep", 256*time.Millisecond, "now", time.Now()) logger.Infof("The answer to life the universe and everything = %d", 42) logger.Warn("This is a WARN message") logger.Error("This is an ERROR message") }
Output:
func SetGlobalProductionLogger ¶
func SetGlobalProductionLogger(logPath string)
SetGlobalProductionLogger sets the logger returned from GetProductionLogger() as the global Logger and SugaredLogger of zap.
Example ¶
package main import ( "time" "bitbucket.org/ai69/zapcfg" "go.uber.org/zap" ) func main() { zapcfg.SetGlobalProductionLogger("log/app.log") logger := zap.L().Named("prod") logger.Debug("This is a DEBUG message") logger.Info("This is an INFO message") logger.Info("This is an INFO message with fields", zap.Int("id", 1), zap.Duration("sleep", 32*time.Millisecond), zap.Time("now", time.Now())) logger.Warn("This is a WARN message") logger.Error("This is an ERROR message") logger.DPanic("This is a DPANIC message") }
Output:
Types ¶
This section is empty.