logger.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package logger
  2. import (
  3. "os"
  4. "time"
  5. "gopkg.in/natefinch/lumberjack.v2"
  6. "github.com/spf13/viper"
  7. "go.uber.org/zap"
  8. "go.uber.org/zap/zapcore"
  9. )
  10. // error logger
  11. var log *zap.SugaredLogger
  12. var levelMap = map[string]zapcore.Level{
  13. "debug": zapcore.DebugLevel,
  14. "info": zapcore.InfoLevel,
  15. "warn": zapcore.WarnLevel,
  16. "error": zapcore.ErrorLevel,
  17. "dpanic": zapcore.DPanicLevel,
  18. "panic": zapcore.PanicLevel,
  19. "fatal": zapcore.FatalLevel,
  20. }
  21. func Init() {
  22. config := viper.New()
  23. config.SetConfigFile("./config/settings.yaml")
  24. config.ReadInConfig()
  25. var syncWriters []zapcore.WriteSyncer
  26. level := getLoggerLevel(config.GetString(`settings.log.level`))
  27. fileConfig := &lumberjack.Logger{
  28. Filename: config.GetString(`settings.log.path`), // 日志文件名
  29. MaxSize: config.GetInt(`settings.log.maxsize`), // 日志文件大小
  30. MaxAge: config.GetInt(`settings.log.maxAge`), // 最长保存天数
  31. MaxBackups: config.GetInt(`settings.log.maxBackups`), // 最多备份几个
  32. LocalTime: config.GetBool(`settings.log.localtime`), // 日志时间戳
  33. Compress: config.GetBool(`settings.log.compress`), // 是否压缩文件,使用gzip
  34. }
  35. encoder := zap.NewProductionEncoderConfig()
  36. encoder.EncodeTime = func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
  37. enc.AppendString(t.Format("2006-01-02 15:04:05.000000"))
  38. }
  39. if config.GetBool("settings.log.consoleStdout") {
  40. syncWriters = append(syncWriters, zapcore.AddSync(os.Stdout))
  41. }
  42. if config.GetBool("settings.log.fileStdout") {
  43. syncWriters = append(syncWriters, zapcore.AddSync(fileConfig))
  44. }
  45. core := zapcore.NewCore(
  46. zapcore.NewJSONEncoder(encoder),
  47. zapcore.NewMultiWriteSyncer(syncWriters...),
  48. zap.NewAtomicLevelAt(level))
  49. logger := zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1))
  50. log = logger.Sugar()
  51. }
  52. func getLoggerLevel(lvl string) zapcore.Level {
  53. if level, ok := levelMap[lvl]; ok {
  54. return level
  55. }
  56. return zapcore.InfoLevel
  57. }
  58. func Debug(args ...interface{}) {
  59. log.Debug(args...)
  60. }
  61. func Debugf(format string, args ...interface{}) {
  62. log.Debugf(format, args...)
  63. }
  64. func Info(args ...interface{}) {
  65. log.Info(args...)
  66. }
  67. func Infof(format string, args ...interface{}) {
  68. log.Infof(format, args...)
  69. }
  70. func Warn(args ...interface{}) {
  71. log.Warn(args...)
  72. }
  73. func Warnf(format string, args ...interface{}) {
  74. log.Warnf(format, args...)
  75. }
  76. func Error(args ...interface{}) {
  77. log.Error(args...)
  78. }
  79. func Errorf(format string, args ...interface{}) {
  80. log.Errorf(format, args...)
  81. }
  82. func DPanic(args ...interface{}) {
  83. log.DPanic(args...)
  84. }
  85. func DPanicf(format string, args ...interface{}) {
  86. log.DPanicf(format, args...)
  87. }
  88. func Panic(args ...interface{}) {
  89. log.Panic(args...)
  90. }
  91. func Panicf(format string, args ...interface{}) {
  92. log.Panicf(format, args...)
  93. }
  94. func Fatal(args ...interface{}) {
  95. log.Fatal(args...)
  96. }
  97. func Fatalf(format string, args ...interface{}) {
  98. log.Fatalf(format, args...)
  99. }