loghelper.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package log_helper
  2. import (
  3. "github.com/allanpk716/ChineseSubFinder/internal/pkg/global_value"
  4. rotatelogs "github.com/lestrrat-go/file-rotatelogs"
  5. "github.com/sirupsen/logrus"
  6. easy "github.com/t-tomalak/logrus-easy-formatter"
  7. "io"
  8. "os"
  9. "path/filepath"
  10. "sync"
  11. "time"
  12. )
  13. func NewLogHelper(appName string, level logrus.Level, maxAge time.Duration, rotationTime time.Duration) *logrus.Logger {
  14. Logger := &logrus.Logger{
  15. // Out: os.Stderr,
  16. // Level: logrus.DebugLevel,
  17. Formatter: &easy.Formatter{
  18. TimestampFormat: "2006-01-02 15:04:05",
  19. LogFormat: "[%lvl%]: %time% - %msg%\n",
  20. },
  21. }
  22. nowPath, err := os.Getwd()
  23. if err != nil {
  24. panic(err)
  25. }
  26. pathRoot := filepath.Join(nowPath, "Logs")
  27. fileAbsPath := filepath.Join(pathRoot, appName+".log")
  28. // 下面配置日志每隔 X 分钟轮转一个新文件,保留最近 X 分钟的日志文件,多余的自动清理掉。
  29. writer, _ := rotatelogs.New(
  30. filepath.Join(pathRoot, appName+"--%YLen%m%d%H%M--.log"),
  31. rotatelogs.WithLinkName(fileAbsPath),
  32. rotatelogs.WithMaxAge(maxAge),
  33. rotatelogs.WithRotationTime(rotationTime),
  34. )
  35. Logger.SetLevel(level)
  36. Logger.SetOutput(io.MultiWriter(os.Stderr, writer))
  37. return Logger
  38. }
  39. // SetLoggerName 如果是 ChineseSubFinder 调用则无需使用,其他子程序用的时候,为了区分日子名称,需要设置
  40. func SetLoggerName(logName string) {
  41. if logName == "" {
  42. panic("Need Set Logger Name")
  43. }
  44. logNameBase = logName
  45. }
  46. func GetLogger(reload ...bool) *logrus.Logger {
  47. if len(reload) > 0 {
  48. if reload[0] == true {
  49. logInit()
  50. }
  51. }
  52. oneBase.Do(logInit)
  53. return loggerBase
  54. }
  55. func logInit() {
  56. var level logrus.Level
  57. // 之前是读取配置文件,现在改为,读取当前目录下,是否有一个特殊的文件,有则启动 Debug 日志级别
  58. // 那么怎么写入这个文件,就靠额外的逻辑控制了
  59. if isFile(filepath.Join(global_value.ConfigRootDirFPath, DebugFileName)) == true {
  60. level = logrus.DebugLevel
  61. } else {
  62. level = logrus.InfoLevel
  63. }
  64. if logNameBase == "" {
  65. // 默认不设置的时候就是这个
  66. logNameBase = LogNameChineseSubFinder
  67. }
  68. loggerBase = NewLogHelper(logNameBase, level, time.Duration(7*24)*time.Hour, time.Duration(24)*time.Hour)
  69. }
  70. func isFile(filePath string) bool {
  71. s, err := os.Stat(filePath)
  72. if err != nil {
  73. return false
  74. }
  75. return !s.IsDir()
  76. }
  77. // WriteDebugFile 写入开启 Debug 级别日志记录的特殊文件,注意这个最好是在主程序中调用,这样就跟主程序在一个目录下生成,log 去检测是否存在才有意义
  78. func WriteDebugFile() error {
  79. if isFile(filepath.Join(global_value.ConfigRootDirFPath, DebugFileName)) == true {
  80. return nil
  81. }
  82. f, err := os.Create(filepath.Join(global_value.ConfigRootDirFPath, DebugFileName))
  83. defer func() {
  84. _ = f.Close()
  85. }()
  86. if err != nil {
  87. return err
  88. }
  89. return nil
  90. }
  91. // DeleteDebugFile 删除开启 Debug 级别日志记录的特殊文件
  92. func DeleteDebugFile() error {
  93. if isFile(filepath.Join(global_value.ConfigRootDirFPath, DebugFileName)) == false {
  94. return nil
  95. }
  96. err := os.Remove(filepath.Join(global_value.ConfigRootDirFPath, DebugFileName))
  97. if err != nil {
  98. return err
  99. }
  100. return nil
  101. }
  102. const DebugFileName = "opendebuglog"
  103. const (
  104. LogNameChineseSubFinder = "ChineseSubFinder"
  105. LogNameGetCAPTCHA = "GetCAPTCHA"
  106. LogNameBackEnd = "BackEnd"
  107. )
  108. var (
  109. logNameBase = ""
  110. oneBase = sync.Once{}
  111. loggerBase *logrus.Logger
  112. )