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.

97 lines
3.4 KiB

3 years ago
3 years ago
  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. func Zap() (logger *zap.Logger) {
  12. if ok, _ := utils.PathExists(global.GVA_CONFIG.Zap.Director); !ok { // 判断是否有Director文件夹
  13. fmt.Printf("create %v directory\n", global.GVA_CONFIG.Zap.Director)
  14. _ = os.Mkdir(global.GVA_CONFIG.Zap.Director, os.ModePerm)
  15. }
  16. // 调试级别
  17. debugPriority := zap.LevelEnablerFunc(func(lev zapcore.Level) bool {
  18. return lev == zap.DebugLevel
  19. })
  20. // 日志级别
  21. infoPriority := zap.LevelEnablerFunc(func(lev zapcore.Level) bool {
  22. return lev == zap.InfoLevel
  23. })
  24. // 警告级别
  25. warnPriority := zap.LevelEnablerFunc(func(lev zapcore.Level) bool {
  26. return lev == zap.WarnLevel
  27. })
  28. // 错误级别
  29. errorPriority := zap.LevelEnablerFunc(func(lev zapcore.Level) bool {
  30. return lev >= zap.ErrorLevel
  31. })
  32. cores := [...]zapcore.Core{
  33. getEncoderCore(fmt.Sprintf("./%s/server_debug.log", global.GVA_CONFIG.Zap.Director), debugPriority),
  34. getEncoderCore(fmt.Sprintf("./%s/server_info.log", global.GVA_CONFIG.Zap.Director), infoPriority),
  35. getEncoderCore(fmt.Sprintf("./%s/server_warn.log", global.GVA_CONFIG.Zap.Director), warnPriority),
  36. getEncoderCore(fmt.Sprintf("./%s/server_error.log", global.GVA_CONFIG.Zap.Director), errorPriority),
  37. }
  38. logger = zap.New(zapcore.NewTee(cores[:]...), zap.AddCaller())
  39. if global.GVA_CONFIG.Zap.ShowLine {
  40. logger = logger.WithOptions(zap.AddCaller())
  41. }
  42. return logger
  43. }
  44. // getEncoderConfig 获取zapcore.EncoderConfig
  45. func getEncoderConfig() (config zapcore.EncoderConfig) {
  46. config = zapcore.EncoderConfig{
  47. MessageKey: "message",
  48. LevelKey: "level",
  49. TimeKey: "time",
  50. NameKey: "logger",
  51. CallerKey: "caller",
  52. StacktraceKey: global.GVA_CONFIG.Zap.StacktraceKey,
  53. LineEnding: zapcore.DefaultLineEnding,
  54. EncodeLevel: zapcore.LowercaseLevelEncoder,
  55. EncodeTime: CustomTimeEncoder,
  56. EncodeDuration: zapcore.SecondsDurationEncoder,
  57. EncodeCaller: zapcore.FullCallerEncoder,
  58. }
  59. switch {
  60. case global.GVA_CONFIG.Zap.EncodeLevel == "LowercaseLevelEncoder": // 小写编码器(默认)
  61. config.EncodeLevel = zapcore.LowercaseLevelEncoder
  62. case global.GVA_CONFIG.Zap.EncodeLevel == "LowercaseColorLevelEncoder": // 小写编码器带颜色
  63. config.EncodeLevel = zapcore.LowercaseColorLevelEncoder
  64. case global.GVA_CONFIG.Zap.EncodeLevel == "CapitalLevelEncoder": // 大写编码器
  65. config.EncodeLevel = zapcore.CapitalLevelEncoder
  66. case global.GVA_CONFIG.Zap.EncodeLevel == "CapitalColorLevelEncoder": // 大写编码器带颜色
  67. config.EncodeLevel = zapcore.CapitalColorLevelEncoder
  68. default:
  69. config.EncodeLevel = zapcore.LowercaseLevelEncoder
  70. }
  71. return config
  72. }
  73. // getEncoder 获取zapcore.Encoder
  74. func getEncoder() zapcore.Encoder {
  75. if global.GVA_CONFIG.Zap.Format == "json" {
  76. return zapcore.NewJSONEncoder(getEncoderConfig())
  77. }
  78. return zapcore.NewConsoleEncoder(getEncoderConfig())
  79. }
  80. // getEncoderCore 获取Encoder的zapcore.Core
  81. func getEncoderCore(fileName string, level zapcore.LevelEnabler) (core zapcore.Core) {
  82. writer := utils.GetWriteSyncer(fileName) // 使用file-rotatelogs进行日志分割
  83. return zapcore.NewCore(getEncoder(), writer, level)
  84. }
  85. // 自定义日志输出时间格式
  86. func CustomTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
  87. enc.AppendString(t.Format(global.GVA_CONFIG.Zap.Prefix + "2006/01/02 - 15:04:05.000"))
  88. }