0_base.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package log
  2. import (
  3. "cockData/server/config"
  4. "go.uber.org/zap"
  5. "go.uber.org/zap/zapcore"
  6. "os"
  7. )
  8. var _logger *zap.SugaredLogger
  9. func Init(config *config.LoggerConfig) {
  10. atomicLevel := initAtomicLevel(config.Level)
  11. encoderConfig := zap.NewProductionEncoderConfig()
  12. encoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05.000")
  13. encoder := zapcore.NewConsoleEncoder(encoderConfig)
  14. var zapCore zapcore.Core
  15. consoleSyncer := zapcore.AddSync(os.Stdout)
  16. if config.Lumberjack != nil {
  17. writerSyncer := zapcore.AddSync(config.Lumberjack)
  18. zapCore = zapcore.NewTee(
  19. zapcore.NewCore(encoder, writerSyncer, atomicLevel),
  20. zapcore.NewCore(encoder, consoleSyncer, atomicLevel),
  21. )
  22. } else {
  23. zapCore = zapcore.NewCore(encoder, consoleSyncer, atomicLevel)
  24. }
  25. logger := zap.New(zapCore)
  26. _logger = logger.Sugar()
  27. _logger.Info("日志管理器初始化完毕...")
  28. }
  29. func initAtomicLevel(level string) zap.AtomicLevel {
  30. atomicLevel := zap.NewAtomicLevel()
  31. switch level {
  32. case "DEBUG":
  33. atomicLevel.SetLevel(zapcore.DebugLevel)
  34. case "INFO":
  35. atomicLevel.SetLevel(zapcore.InfoLevel)
  36. case "WARN":
  37. atomicLevel.SetLevel(zapcore.WarnLevel)
  38. case "ERROR":
  39. atomicLevel.SetLevel(zapcore.ErrorLevel)
  40. case "DPANIC":
  41. atomicLevel.SetLevel(zapcore.DPanicLevel)
  42. case "PANIC":
  43. atomicLevel.SetLevel(zapcore.PanicLevel)
  44. case "FATAL":
  45. atomicLevel.SetLevel(zapcore.FatalLevel)
  46. default:
  47. atomicLevel.SetLevel(zapcore.DebugLevel)
  48. }
  49. return atomicLevel
  50. }