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.

167 lines
3.7 KiB

3 years ago
3 years ago
3 years ago
  1. package log
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "path"
  7. "github.com/BurntSushi/toml"
  8. "go.uber.org/zap"
  9. "go.uber.org/zap/zapcore"
  10. "gopkg.in/yaml.v2"
  11. )
  12. var (
  13. logger Logger
  14. //OutputPaths = ""
  15. //ErrOutputPaths = ""
  16. )
  17. type YLogger struct {
  18. Logger
  19. dynamicLevel zap.AtomicLevel
  20. }
  21. type Logger interface {
  22. Info(args ...interface{})
  23. Warn(args ...interface{})
  24. Error(args ...interface{})
  25. Debug(args ...interface{})
  26. Infof(fmt string, args ...interface{})
  27. Warnf(fmt string, args ...interface{})
  28. Errorf(fmt string, args ...interface{})
  29. Debugf(fmt string, args ...interface{})
  30. }
  31. func init() {
  32. var (
  33. logCfg GlobalCfg
  34. )
  35. log.Println("config file name -->", ConfigFile)
  36. log.Println("starting ...")
  37. if _, err := toml.DecodeFile(ConfigFile, &logCfg); err != nil {
  38. //log.Fatalf("Read config file err:%s", err.Error())
  39. log.Println("failed to open config file")
  40. if err = InitLog(""); nil != err {
  41. log.Println(err.Error())
  42. }
  43. return
  44. }
  45. cfg = logCfg.Logs
  46. cfg.OutputPaths = cfg.LogPath + cfg.APPName + ".log"
  47. cfg.ErrorOutputPaths = cfg.LogPath + cfg.APPName + ".error"
  48. err := InitLog(cfg.LogConf)
  49. if err != nil {
  50. log.Printf("[InitLog] warn: %v", err)
  51. }
  52. }
  53. func ReloadLogger(configFile string) {
  54. var (
  55. logCfg GlobalCfg
  56. )
  57. log.Println("config file name -->", configFile)
  58. if _, err := toml.DecodeFile(configFile, &logCfg); err != nil {
  59. log.Fatalf("Read config file err:%s", err.Error())
  60. }
  61. cfg = logCfg.Logs
  62. cfg.OutputPaths = cfg.LogPath + cfg.APPName + ".log"
  63. cfg.ErrorOutputPaths = cfg.LogPath + cfg.APPName + ".error"
  64. err := InitLog(cfg.LogConf)
  65. if err != nil {
  66. log.Printf("[ReloadLogger] warn: %v", err)
  67. }
  68. }
  69. func InitLog(logConfFile string) error {
  70. if logConfFile == "" {
  71. InitLogger(nil)
  72. return fmt.Errorf("log configure file name is nil")
  73. }
  74. if path.Ext(logConfFile) != ".yml" {
  75. InitLogger(nil)
  76. return fmt.Errorf("log configure file name{%s} suffix must be .yml", logConfFile)
  77. }
  78. confFileStream, err := ioutil.ReadFile(logConfFile)
  79. if err != nil {
  80. InitLogger(nil)
  81. return fmt.Errorf("ioutil.ReadFile(file:%s) = error:%v", logConfFile, err)
  82. }
  83. conf := &zap.Config{}
  84. err = yaml.Unmarshal(confFileStream, conf)
  85. if err != nil {
  86. InitLogger(nil)
  87. return fmt.Errorf("[Unmarshal]init logger error: %v", err)
  88. }
  89. log.Println("to set yuanex logger ...")
  90. conf.OutputPaths = append(conf.OutputPaths, cfg.OutputPaths)
  91. conf.ErrorOutputPaths = append(conf.ErrorOutputPaths, cfg.ErrorOutputPaths)
  92. log.Println("ErrorOutputPahts -->", conf.ErrorOutputPaths)
  93. InitLogger(conf)
  94. return nil
  95. }
  96. func InitLogger(conf *zap.Config) {
  97. var zapLoggerConfig zap.Config
  98. if conf == nil {
  99. zapLoggerConfig = zap.NewDevelopmentConfig()
  100. zapLoggerEncoderConfig := zapcore.EncoderConfig{
  101. TimeKey: "time",
  102. LevelKey: "level",
  103. NameKey: "logger",
  104. CallerKey: "caller",
  105. MessageKey: "message",
  106. StacktraceKey: "stacktrace",
  107. EncodeTime: zapcore.ISO8601TimeEncoder,
  108. EncodeDuration: zapcore.SecondsDurationEncoder,
  109. EncodeCaller: zapcore.ShortCallerEncoder,
  110. }
  111. zapLoggerConfig.EncoderConfig = zapLoggerEncoderConfig
  112. } else {
  113. zapLoggerConfig = *conf
  114. }
  115. zapLogger, _ := zapLoggerConfig.Build(zap.AddCallerSkip(1))
  116. //logger = zapLogger.Sugar()
  117. logger = &YLogger{Logger: zapLogger.Sugar(), dynamicLevel: zapLoggerConfig.Level}
  118. }
  119. func SetLogger(log Logger) {
  120. logger = log
  121. }
  122. func GetLogger() Logger {
  123. return logger
  124. }
  125. func SetLoggerLevel(level string) bool {
  126. if l, ok := logger.(OpsLogger); ok {
  127. l.SetLoggerLevel(level)
  128. return true
  129. }
  130. return false
  131. }
  132. type OpsLogger interface {
  133. Logger
  134. SetLoggerLevel(level string)
  135. }
  136. func (dl *YLogger) SetLoggerLevel(level string) {
  137. l := new(zapcore.Level)
  138. l.Set(level)
  139. dl.dynamicLevel.SetLevel(*l)
  140. }