You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

logging.go 4.3 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package log
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "github.com/natefinch/lumberjack"
  7. "go.uber.org/zap"
  8. "go.uber.org/zap/zapcore"
  9. )
  10. // Level represents the level of logging.
  11. type LogLevel int8
  12. const (
  13. // DebugLevel logs are typically voluminous, and are usually disabled in
  14. // production.
  15. DebugLevel = LogLevel(zapcore.DebugLevel)
  16. // InfoLevel is the default logging priority.
  17. InfoLevel = LogLevel(zapcore.InfoLevel)
  18. // WarnLevel logs are more important than Info, but don't need individual
  19. // human review.
  20. WarnLevel = LogLevel(zapcore.WarnLevel)
  21. // ErrorLevel logs are high-priority. If an application is running smoothly,
  22. // it shouldn't generate any error-level logs.
  23. ErrorLevel = LogLevel(zapcore.ErrorLevel)
  24. // PanicLevel logs a message, then panics.
  25. PanicLevel = LogLevel(zapcore.PanicLevel)
  26. // FatalLevel logs a message, then calls os.Exit(1).
  27. FatalLevel = LogLevel(zapcore.FatalLevel)
  28. )
  29. func (l *LogLevel) UnmarshalText(text []byte) error {
  30. if l == nil {
  31. return errors.New("can't unmarshal a nil *Level")
  32. }
  33. if !l.unmarshalText(text) && !l.unmarshalText(bytes.ToLower(text)) {
  34. return fmt.Errorf("unrecognized level: %q", text)
  35. }
  36. return nil
  37. }
  38. func (l *LogLevel) unmarshalText(text []byte) bool {
  39. switch string(text) {
  40. case "debug", "DEBUG":
  41. *l = DebugLevel
  42. case "info", "INFO", "": // make the zero value useful
  43. *l = InfoLevel
  44. case "warn", "WARN":
  45. *l = WarnLevel
  46. case "error", "ERROR":
  47. *l = ErrorLevel
  48. case "panic", "PANIC":
  49. *l = PanicLevel
  50. case "fatal", "FATAL":
  51. *l = FatalLevel
  52. default:
  53. return false
  54. }
  55. return true
  56. }
  57. type Logger interface {
  58. Debug(v ...interface{})
  59. Debugf(format string, v ...interface{})
  60. Info(v ...interface{})
  61. Infof(format string, v ...interface{})
  62. Warn(v ...interface{})
  63. Warnf(format string, v ...interface{})
  64. Error(v ...interface{})
  65. Errorf(format string, v ...interface{})
  66. Panic(v ...interface{})
  67. Panicf(format string, v ...interface{})
  68. Fatal(v ...interface{})
  69. Fatalf(format string, v ...interface{})
  70. }
  71. var (
  72. log Logger
  73. zapLogger *zap.Logger
  74. zapLoggerConfig = zap.NewDevelopmentConfig()
  75. zapLoggerEncoderConfig = zapcore.EncoderConfig{
  76. TimeKey: "time",
  77. LevelKey: "level",
  78. NameKey: "logger",
  79. CallerKey: "caller",
  80. MessageKey: "message",
  81. StacktraceKey: "stacktrace",
  82. EncodeLevel: zapcore.CapitalColorLevelEncoder,
  83. EncodeTime: zapcore.ISO8601TimeEncoder,
  84. EncodeDuration: zapcore.SecondsDurationEncoder,
  85. EncodeCaller: zapcore.ShortCallerEncoder,
  86. }
  87. )
  88. func init() {
  89. zapLoggerConfig.EncoderConfig = zapLoggerEncoderConfig
  90. zapLogger, _ = zapLoggerConfig.Build()
  91. log = zapLogger.Sugar()
  92. }
  93. func Init(logPath string, level LogLevel) {
  94. lumberJackLogger := &lumberjack.Logger{
  95. Filename: logPath,
  96. MaxSize: 10,
  97. MaxBackups: 5,
  98. MaxAge: 30,
  99. Compress: false,
  100. }
  101. syncer := zapcore.AddSync(lumberJackLogger)
  102. encoderConfig := zap.NewProductionEncoderConfig()
  103. encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
  104. encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
  105. encoder := zapcore.NewConsoleEncoder(encoderConfig)
  106. core := zapcore.NewCore(encoder, syncer, zap.NewAtomicLevelAt(zapcore.Level(level)))
  107. zapLogger = zap.New(core, zap.AddCaller())
  108. log = zapLogger.Sugar()
  109. }
  110. // SetLogger: customize yourself logger.
  111. func SetLogger(logger Logger) {
  112. log = logger
  113. }
  114. // GetLogger get logger
  115. func GetLogger() Logger {
  116. return log
  117. }
  118. // Debug ...
  119. func Debug(v ...interface{}) {
  120. log.Debug(v...)
  121. }
  122. // Debugf ...
  123. func Debugf(format string, v ...interface{}) {
  124. log.Debugf(format, v...)
  125. }
  126. // Info ...
  127. func Info(v ...interface{}) {
  128. log.Info(v...)
  129. }
  130. // Infof ...
  131. func Infof(format string, v ...interface{}) {
  132. log.Infof(format, v...)
  133. }
  134. // Warn ...
  135. func Warn(v ...interface{}) {
  136. log.Warn(v...)
  137. }
  138. // Warnf ...
  139. func Warnf(format string, v ...interface{}) {
  140. log.Warnf(format, v...)
  141. }
  142. // Error ...
  143. func Error(v ...interface{}) {
  144. log.Error(v...)
  145. }
  146. // Errorf ...
  147. func Errorf(format string, v ...interface{}) {
  148. log.Errorf(format, v...)
  149. }
  150. // Panic ...
  151. func Panic(v ...interface{}) {
  152. log.Panic(v...)
  153. }
  154. // Panicf ...
  155. func Panicf(format string, v ...interface{}) {
  156. log.Panicf(format, v...)
  157. }
  158. // Fatal ...
  159. func Fatal(v ...interface{}) {
  160. log.Fatal(v...)
  161. }
  162. // Fatalf ...
  163. func Fatalf(format string, v ...interface{}) {
  164. log.Fatalf(format, v...)
  165. }