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

129 lines
4.6 KiB

  1. package core
  2. import (
  3. "fmt"
  4. "gin-vue-admin/global"
  5. "gin-vue-admin/utils"
  6. zaprotatelogs "github.com/lestrrat-go/file-rotatelogs"
  7. "go.uber.org/zap"
  8. "go.uber.org/zap/zapcore"
  9. "os"
  10. "time"
  11. )
  12. var (
  13. err error
  14. level zapcore.Level
  15. writer zapcore.WriteSyncer
  16. )
  17. func init() {
  18. if ok, _ := utils.PathExists(global.GVA_CONFIG.Zap.Director); !ok { // 判断是否有Director文件夹
  19. fmt.Printf("create %v directory\n", global.GVA_CONFIG.Zap.Director)
  20. _ = os.Mkdir(global.GVA_CONFIG.Zap.Director, os.ModePerm)
  21. }
  22. switch global.GVA_CONFIG.Zap.Level { // 初始化配置文件的Level
  23. case "debug":
  24. level = zap.DebugLevel
  25. case "info":
  26. level = zap.InfoLevel
  27. case "warn":
  28. level = zap.WarnLevel
  29. case "error":
  30. level = zap.ErrorLevel
  31. case "dpanic":
  32. level = zap.DPanicLevel
  33. case "panic":
  34. level = zap.PanicLevel
  35. case "fatal":
  36. level = zap.FatalLevel
  37. default:
  38. level = zap.InfoLevel
  39. }
  40. writer, err = getWriteSyncer() // 使用file-rotatelogs进行日志分割
  41. if err != nil {
  42. fmt.Printf("Get Write Syncer Failed err:%v", err.Error())
  43. return
  44. }
  45. if level == zap.DebugLevel || level == zap.ErrorLevel {
  46. global.GVA_LOG = zap.New(getEncoderCore(), zap.AddStacktrace(level))
  47. } else {
  48. global.GVA_LOG = zap.New(getEncoderCore())
  49. }
  50. if global.GVA_CONFIG.Zap.ShowLine {
  51. global.GVA_LOG.WithOptions(zap.AddCaller())
  52. }
  53. }
  54. // getWriteSyncer zap logger中加入file-rotatelogs
  55. func getWriteSyncer() (zapcore.WriteSyncer, error) {
  56. fileWriter, err := zaprotatelogs.New(
  57. global.GVA_CONFIG.Zap.Director+string(os.PathSeparator)+"%Y-%m-%d.log",
  58. zaprotatelogs.WithLinkName(global.GVA_CONFIG.Zap.LinkName),
  59. zaprotatelogs.WithMaxAge(7*24*time.Hour),
  60. zaprotatelogs.WithRotationTime(24*time.Hour),
  61. )
  62. if global.GVA_CONFIG.Zap.LogInConsole {
  63. return zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(fileWriter)), err
  64. }
  65. return zapcore.AddSync(fileWriter), err
  66. }
  67. // getEncoderConfig 获取zapcore.EncoderConfig
  68. func getEncoderConfig() (config zapcore.EncoderConfig) {
  69. config = zapcore.EncoderConfig{
  70. MessageKey: "message",
  71. LevelKey: "level",
  72. TimeKey: "time",
  73. NameKey: "logger",
  74. CallerKey: "caller",
  75. StacktraceKey: global.GVA_CONFIG.Zap.StacktraceKey,
  76. LineEnding: zapcore.DefaultLineEnding,
  77. EncodeLevel: zapcore.CapitalLevelEncoder,
  78. EncodeTime: CustomTimeEncoder,
  79. EncodeDuration: zapcore.SecondsDurationEncoder,
  80. EncodeCaller: zapcore.FullCallerEncoder,
  81. }
  82. switch {
  83. case global.GVA_CONFIG.Zap.EncodeLevel == "LowercaseLevelEncoder" && global.GVA_CONFIG.Zap.Format == "console" : // console小写编码器
  84. config.EncodeLevel = zapcore.LowercaseLevelEncoder
  85. case global.GVA_CONFIG.Zap.EncodeLevel == "LowercaseLevelEncoder" && global.GVA_CONFIG.Zap.Format == "json" : // json小写编码器
  86. config.EncodeLevel = zapcore.LowercaseLevelEncoder
  87. case global.GVA_CONFIG.Zap.EncodeLevel == "LowercaseColorLevelEncoder" && global.GVA_CONFIG.Zap.Format == "console": // console小写编码器带颜色
  88. config.EncodeLevel = zapcore.LowercaseColorLevelEncoder
  89. case global.GVA_CONFIG.Zap.EncodeLevel == "LowercaseColorLevelEncoder" && global.GVA_CONFIG.Zap.Format == "json": // json小写编码器带颜色
  90. config.EncodeLevel = zapcore.LowercaseColorLevelEncoder
  91. case global.GVA_CONFIG.Zap.EncodeLevel == "CapitalLevelEncoder" && global.GVA_CONFIG.Zap.Format == "console": // console大写编码器
  92. config.EncodeLevel = zapcore.CapitalLevelEncoder
  93. case global.GVA_CONFIG.Zap.EncodeLevel == "CapitalLevelEncoder" && global.GVA_CONFIG.Zap.Format == "json": // json大写编码器
  94. config.EncodeLevel = zapcore.CapitalLevelEncoder
  95. case global.GVA_CONFIG.Zap.EncodeLevel == "CapitalColorLevelEncoder" && global.GVA_CONFIG.Zap.Format == "console": // console 大写编码器带颜色
  96. config.EncodeLevel = zapcore.CapitalColorLevelEncoder
  97. case global.GVA_CONFIG.Zap.EncodeLevel == "CapitalColorLevelEncoder" && global.GVA_CONFIG.Zap.Format == "json": // json 大写编码器带颜色
  98. config.EncodeLevel = zapcore.CapitalColorLevelEncoder
  99. default:
  100. config.EncodeLevel = zapcore.LowercaseLevelEncoder
  101. }
  102. return config
  103. }
  104. // getEncoder 获取zapcore.Encoder
  105. func getEncoder() zapcore.Encoder {
  106. if global.GVA_CONFIG.Zap.Format == "json" {
  107. return zapcore.NewJSONEncoder(getEncoderConfig())
  108. }
  109. return zapcore.NewConsoleEncoder(getEncoderConfig())
  110. }
  111. // getEncoderCore 获取Encoder的zapcore.Core
  112. func getEncoderCore() (core zapcore.Core) {
  113. return zapcore.NewCore(getEncoder(), writer, level)
  114. }
  115. // 自定义日志输出时间格式
  116. func CustomTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
  117. enc.AppendString(t.Format(global.GVA_CONFIG.Zap.Prefix + "2006/01/02 - 15:04:05.000"))
  118. }