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.

102 lines
3.0 KiB

  1. package core
  2. import (
  3. "fmt"
  4. "gin-vue-admin/global"
  5. "gin-vue-admin/utils"
  6. "go.uber.org/zap"
  7. "go.uber.org/zap/zapcore"
  8. "os"
  9. "time"
  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. }