logger.go 1.6 KB

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