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.

161 lines
3.5 KiB

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. if _, err := toml.DecodeFile(ConfigFile, &logCfg); err != nil {
  37. log.Fatalf("Read config file err:%s", err.Error())
  38. }
  39. cfg = logCfg.Logs
  40. cfg.OutputPaths = cfg.LogPath + cfg.APPName + ".log"
  41. cfg.ErrorOutputPaths = cfg.LogPath + cfg.APPName + ".error"
  42. err := InitLog(cfg.LogConf)
  43. if err != nil {
  44. log.Printf("[InitLog] warn: %v", err)
  45. }
  46. }
  47. func ReloadLogger(configFile string) {
  48. var (
  49. logCfg GlobalCfg
  50. )
  51. log.Println("config file name -->", configFile)
  52. if _, err := toml.DecodeFile(configFile, &logCfg); err != nil {
  53. log.Fatalf("Read config file err:%s", err.Error())
  54. }
  55. cfg = logCfg.Logs
  56. cfg.OutputPaths = cfg.LogPath + cfg.APPName + ".log"
  57. cfg.ErrorOutputPaths = cfg.LogPath + cfg.APPName + ".error"
  58. err := InitLog(cfg.LogConf)
  59. if err != nil {
  60. log.Printf("[ReloadLogger] warn: %v", err)
  61. }
  62. }
  63. func InitLog(logConfFile string) error {
  64. if logConfFile == "" {
  65. InitLogger(nil)
  66. return fmt.Errorf("log configure file name is nil")
  67. }
  68. if path.Ext(logConfFile) != ".yml" {
  69. InitLogger(nil)
  70. return fmt.Errorf("log configure file name{%s} suffix must be .yml", logConfFile)
  71. }
  72. confFileStream, err := ioutil.ReadFile(logConfFile)
  73. if err != nil {
  74. InitLogger(nil)
  75. return fmt.Errorf("ioutil.ReadFile(file:%s) = error:%v", logConfFile, err)
  76. }
  77. conf := &zap.Config{}
  78. err = yaml.Unmarshal(confFileStream, conf)
  79. if err != nil {
  80. InitLogger(nil)
  81. return fmt.Errorf("[Unmarshal]init logger error: %v", err)
  82. }
  83. log.Println("to set yuanex logger ...")
  84. conf.OutputPaths = append(conf.OutputPaths, cfg.OutputPaths)
  85. conf.ErrorOutputPaths = append(conf.ErrorOutputPaths, cfg.ErrorOutputPaths)
  86. log.Println("ErrorOutputPahts -->", conf.ErrorOutputPaths)
  87. InitLogger(conf)
  88. return nil
  89. }
  90. func InitLogger(conf *zap.Config) {
  91. var zapLoggerConfig zap.Config
  92. if conf == nil {
  93. zapLoggerConfig = zap.NewDevelopmentConfig()
  94. zapLoggerEncoderConfig := zapcore.EncoderConfig{
  95. TimeKey: "time",
  96. LevelKey: "level",
  97. NameKey: "logger",
  98. CallerKey: "caller",
  99. MessageKey: "message",
  100. StacktraceKey: "stacktrace",
  101. EncodeTime: zapcore.ISO8601TimeEncoder,
  102. EncodeDuration: zapcore.SecondsDurationEncoder,
  103. EncodeCaller: zapcore.ShortCallerEncoder,
  104. }
  105. zapLoggerConfig.EncoderConfig = zapLoggerEncoderConfig
  106. } else {
  107. zapLoggerConfig = *conf
  108. }
  109. zapLogger, _ := zapLoggerConfig.Build(zap.AddCallerSkip(1))
  110. //logger = zapLogger.Sugar()
  111. logger = &YLogger{Logger: zapLogger.Sugar(), dynamicLevel: zapLoggerConfig.Level}
  112. }
  113. func SetLogger(log Logger) {
  114. logger = log
  115. }
  116. func GetLogger() Logger {
  117. return logger
  118. }
  119. func SetLoggerLevel(level string) bool {
  120. if l, ok := logger.(OpsLogger); ok {
  121. l.SetLoggerLevel(level)
  122. return true
  123. }
  124. return false
  125. }
  126. type OpsLogger interface {
  127. Logger
  128. SetLoggerLevel(level string)
  129. }
  130. func (dl *YLogger) SetLoggerLevel(level string) {
  131. l := new(zapcore.Level)
  132. l.Set(level)
  133. dl.dynamicLevel.SetLevel(*l)
  134. }