1
0

logger.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package logger
  2. import (
  3. "go.uber.org/zap"
  4. "go.uber.org/zap/zapcore"
  5. "os"
  6. )
  7. var logger *zap.SugaredLogger
  8. func init() {
  9. // First, define our level-handling logic.
  10. highPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
  11. return lvl >= zapcore.ErrorLevel
  12. })
  13. lowPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
  14. return lvl < zapcore.ErrorLevel
  15. })
  16. // Directly output to stdout and stderr, and add caller information.
  17. consoleDebugging := zapcore.Lock(os.Stdout)
  18. consoleErrors := zapcore.Lock(os.Stderr)
  19. encoderConfig := zap.NewDevelopmentEncoderConfig()
  20. encoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05")
  21. encoderConfig.ConsoleSeparator = " "
  22. encoderConfig.EncodeLevel = colorLevelEncoder
  23. consoleEncoder := zapcore.NewConsoleEncoder(encoderConfig)
  24. // Join the outputs, encoders, and level-handling functions into
  25. // zapcore.Cores, then tee the two cores together.
  26. core := zapcore.NewTee(
  27. zapcore.NewCore(consoleEncoder, consoleErrors, highPriority),
  28. zapcore.NewCore(consoleEncoder, consoleDebugging, lowPriority),
  29. )
  30. // From a zapcore.Core, it's easy to construct a Logger.
  31. logger = zap.New(core, zap.AddCaller()).WithOptions(zap.AddCallerSkip(1)).Sugar()
  32. }
  33. func Sync() {
  34. _ = logger.Sync()
  35. }
  36. func GetLogger() *zap.SugaredLogger {
  37. return logger
  38. }
  39. func Info(args ...interface{}) {
  40. logger.Infoln(args...)
  41. }
  42. func Error(args ...interface{}) {
  43. logger.Errorln(args...)
  44. }
  45. func Fatal(args ...interface{}) {
  46. logger.Fatalln(args...)
  47. }
  48. func Warn(args ...interface{}) {
  49. logger.Warnln(args...)
  50. }
  51. func Debug(args ...interface{}) {
  52. logger.Debugln(args...)
  53. }
  54. func Infof(format string, args ...interface{}) {
  55. logger.Infof(format, args...)
  56. }
  57. func Errorf(format string, args ...interface{}) {
  58. logger.Errorf(format, args...)
  59. }
  60. func Fatalf(format string, args ...interface{}) {
  61. logger.Fatalf(format, args...)
  62. }
  63. func Warnf(format string, args ...interface{}) {
  64. logger.Warnf(format, args...)
  65. }
  66. func Debugf(format string, args ...interface{}) {
  67. logger.Debugf(format, args...)
  68. }