logger.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package parser
  4. import (
  5. "adm-task/config"
  6. "fmt"
  7. "git.getensh.com/common/gopkgsv2/logger"
  8. "git.getensh.com/common/gopkgsv2/tasker"
  9. )
  10. var logConfig config.LogConfig
  11. func LoggerHandler(conf *config.Configure) {
  12. // logConfig.Path 不为空表示已经初始化
  13. if logConfig.Path != "" {
  14. if logConfig.Path == conf.Log.Path {
  15. return
  16. } else {
  17. // 刷盘
  18. // logger.Sync()
  19. }
  20. }
  21. // 取出Stacktrace和Level
  22. logStacktrace := conf.Rpc.AdmTask.LogStacktrace
  23. logLevel := conf.Rpc.AdmTask.LogLevel
  24. if logLevel == "" {
  25. logLevel = conf.Log.Level
  26. }
  27. ms, _ := conf.Log.MaxSize.Int64()
  28. mb, _ := conf.Log.MaxBackups.Int64()
  29. ma, _ := conf.Log.MaxAge.Int64()
  30. maxSize := int(ms)
  31. maxBackups := int(mb)
  32. maxAge := int(ma)
  33. disableStacktrace := (logStacktrace == "true")
  34. // 新建全局日志器
  35. logger.New(conf.RunMode,
  36. logLevel,
  37. fmt.Sprintf("%s/%s.log", conf.Log.Path, conf.Rpc.AdmTask.ServiceName),
  38. maxSize,
  39. maxBackups,
  40. maxAge,
  41. disableStacktrace,
  42. true)
  43. // access日志器
  44. accessLogger := logger.New(conf.RunMode,
  45. logLevel,
  46. fmt.Sprintf("%s/%s-access.log", conf.Log.Path, conf.Rpc.AdmTask.ServiceName),
  47. maxSize,
  48. maxBackups,
  49. maxAge,
  50. disableStacktrace,
  51. false)
  52. // 设置访问日志
  53. tasker.SetLogger(accessLogger)
  54. logConfig = conf.Log
  55. }