logger.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package log
  2. import (
  3. "go.uber.org/zap"
  4. "go.uber.org/zap/zapcore"
  5. )
  6. var (
  7. // `gl` is the global logger.
  8. // Other packages should use public methods such as Info/Error to do the logging.
  9. // For other types of logging, e.g. logging to a separate file, they should use their own loggers.
  10. gl *zap.Logger
  11. gLevel zap.AtomicLevel
  12. )
  13. // Initializes the global console logger.
  14. func init() {
  15. gLevel = zap.NewAtomicLevelAt(zap.InfoLevel)
  16. gl, _ = zap.Config{
  17. Level: gLevel,
  18. Development: true,
  19. // Use "console" to print readable stacktrace.
  20. Encoding: "console",
  21. EncoderConfig: zap.NewDevelopmentEncoderConfig(),
  22. OutputPaths: []string{"stderr"},
  23. ErrorOutputPaths: []string{"stderr"},
  24. }.Build(
  25. // Skip one caller stack to locate the correct caller.
  26. zap.AddCallerSkip(1),
  27. )
  28. }
  29. // SetLevel wraps the zap Level's SetLevel method.
  30. func SetLevel(level zapcore.Level) {
  31. gLevel.SetLevel(level)
  32. }
  33. // EnabledLevel wraps the zap Level's Enabled method.
  34. func EnabledLevel(level zapcore.Level) bool {
  35. return gLevel.Enabled(level)
  36. }
  37. // Debug wraps the zap Logger's Debug method.
  38. func Debug(msg string, fields ...zap.Field) {
  39. gl.Debug(msg, fields...)
  40. }
  41. // Info wraps the zap Logger's Info method.
  42. func Info(msg string, fields ...zap.Field) {
  43. gl.Info(msg, fields...)
  44. }
  45. // Warn wraps the zap Logger's Warn method.
  46. func Warn(msg string, fields ...zap.Field) {
  47. gl.Warn(msg, fields...)
  48. }
  49. // Error wraps the zap Logger's Error method.
  50. func Error(msg string, fields ...zap.Field) {
  51. gl.Error(msg, fields...)
  52. }
  53. // Sync wraps the zap Logger's Sync method.
  54. func Sync() {
  55. _ = gl.Sync()
  56. }