zap.go 3.1 KB

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