Skip to content

Commit

Permalink
重构,日志模块,不直接依赖 config 文件去设置日志的记录级别,转为使用特殊文件
Browse files Browse the repository at this point in the history
Signed-off-by: allan716 <525223688@qq.com>
  • Loading branch information
allanpk716 committed Jan 5, 2022
1 parent ea1e8a9 commit 6cf46ea
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
16 changes: 14 additions & 2 deletions cmd/chinesesubfinder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ import (

func init() {

config = config2.GetConfig()
// 根据配置文件的情况去生成是否需要开启日志 Debug 记录级别的特殊文件
if config.DebugMode == true {
err := log_helper.WriteDebugFile()
if err != nil {
panic("log_helper.WriteDebugFile " + err.Error())
}
} else {
err := log_helper.DeleteDebugFile()
if err != nil {
panic("log_helper.DeleteDebugFile " + err.Error())
}
}

log = log_helper.GetLogger()

log.Infoln("ChineseSubFinder Version:", AppVersion)
Expand All @@ -28,8 +42,6 @@ you need implement getDbName() in file: internal/dao/init.go
and
implement getSpeFileName() in internal/logic/forced_scan_and_down_sub/forced_scan_and_down_sub.go`)
}

config = config2.GetConfig()
}

func main() {
Expand Down
46 changes: 41 additions & 5 deletions internal/pkg/log_helper/loghelper.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package log_helper

import (
"github.com/allanpk716/ChineseSubFinder/internal/pkg/config"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"github.com/sirupsen/logrus"
easy "github.com/t-tomalak/logrus-easy-formatter"
Expand All @@ -22,11 +21,11 @@ func NewLogHelper(appName string, level logrus.Level, maxAge time.Duration, rota
LogFormat: "[%lvl%]: %time% - %msg%\n",
},
}
nowpath, err := os.Getwd()
nowPath, err := os.Getwd()
if err != nil {
panic(err)
}
pathRoot := filepath.Join(nowpath, "Logs")
pathRoot := filepath.Join(nowPath, "Logs")
fileAbsPath := filepath.Join(pathRoot, appName+".log")
// 下面配置日志每隔 X 分钟轮转一个新文件,保留最近 X 分钟的日志文件,多余的自动清理掉。
writer, _ := rotatelogs.New(
Expand All @@ -46,16 +45,53 @@ func GetLogger() *logrus.Logger {
logOnce.Do(func() {

var level logrus.Level
if config.GetConfig().DebugMode == true {
// 之前是读取配置文件,现在改为,读取当前目录下,是否有一个特殊的文件,有则启动 Debug 日志级别
// 那么怎么写入这个文件,就靠额外的逻辑控制了
if isFile(DebugFileName) == true {
level = logrus.DebugLevel
} else {
level = logrus.InfoLevel
}

logger = NewLogHelper("ChineseSubFinder", level, time.Duration(7*24)*time.Hour, time.Duration(24)*time.Hour)
})
return logger
}

func isFile(filePath string) bool {
s, err := os.Stat(filePath)
if err != nil {
return false
}
return !s.IsDir()
}

// WriteDebugFile 写入开启 Debug 级别日志记录的特殊文件,注意这个最好是在主程序中调用,这样就跟主程序在一个目录下生成,log 去检测是否存在才有意义
func WriteDebugFile() error {
if isFile(DebugFileName) == true {
return nil
}
f, err := os.Create(DebugFileName)
defer f.Close()
if err != nil {
return err
}
return nil
}

// DeleteDebugFile 删除开启 Debug 级别日志记录的特殊文件
func DeleteDebugFile() error {

if isFile(DebugFileName) == false {
return nil
}
err := os.Remove(DebugFileName)
if err != nil {
return err
}
return nil
}

const DebugFileName = "opendebuglog"

var logger *logrus.Logger
var logOnce sync.Once

0 comments on commit 6cf46ea

Please sign in to comment.