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.

119 lines
3.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.LowercaseLevelEncoder,
  78. EncodeTime: CustomTimeEncoder,
  79. EncodeDuration: zapcore.SecondsDurationEncoder,
  80. EncodeCaller: zapcore.FullCallerEncoder,
  81. }
  82. switch {
  83. case global.GVA_CONFIG.Zap.EncodeLevel == "LowercaseLevelEncoder": // 小写编码器(默认)
  84. config.EncodeLevel = zapcore.LowercaseLevelEncoder
  85. case global.GVA_CONFIG.Zap.EncodeLevel == "LowercaseColorLevelEncoder": // 小写编码器带颜色
  86. config.EncodeLevel = zapcore.LowercaseColorLevelEncoder
  87. case global.GVA_CONFIG.Zap.EncodeLevel == "CapitalLevelEncoder": // 大写编码器
  88. config.EncodeLevel = zapcore.CapitalLevelEncoder
  89. case global.GVA_CONFIG.Zap.EncodeLevel == "CapitalColorLevelEncoder": // 大写编码器带颜色
  90. config.EncodeLevel = zapcore.CapitalColorLevelEncoder
  91. }
  92. return config
  93. }
  94. // getEncoder 获取zapcore.Encoder
  95. func getEncoder() zapcore.Encoder {
  96. if global.GVA_CONFIG.Zap.Format == "json" {
  97. return zapcore.NewJSONEncoder(getEncoderConfig())
  98. }
  99. return zapcore.NewConsoleEncoder(getEncoderConfig())
  100. }
  101. // getEncoderCore 获取Encoder的zapcore.Core
  102. func getEncoderCore() (core zapcore.Core) {
  103. return zapcore.NewCore(getEncoder(), writer, level)
  104. }
  105. // 自定义日志输出时间格式
  106. func CustomTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
  107. enc.AppendString(t.Format(global.GVA_CONFIG.Zap.Prefix + "2006/01/02 - 15:04:05.000"))
  108. }