From 5562eaeeb9a37a6abbbba9ae179f8fd1b8fa0417 Mon Sep 17 00:00:00 2001 From: M1andy's debian Date: Thu, 20 Oct 2022 21:00:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0logger=20config?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/config.go | 69 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 61 insertions(+), 8 deletions(-) diff --git a/utils/config.go b/utils/config.go index 5143d90..74d76ef 100644 --- a/utils/config.go +++ b/utils/config.go @@ -6,14 +6,23 @@ import ( "path" "time" + "github.com/sirupsen/logrus" "gopkg.in/ini.v1" ) -var loadConfigOptions = ini.LoadOptions{ - IgnoreInlineComment: true, -} +var ( + logger = logrus.New() + loadConfigOptions = ini.LoadOptions{ + IgnoreInlineComment: true} +) // config里位于common分区里的设置 +type Config struct { + Common commonConfig + AdvancedSleep advancedSleepConfig + Proxy proxyConfig + Logger loggerCfg +} type commonConfig struct { MainMode int `ini:"main_mode"` SourceFolder string `ini:"source_folder"` @@ -49,10 +58,9 @@ type proxyConfig struct { CacertFile string `ini:"cacert_file"` } -type Config struct { - Common commonConfig - AdvancedSleep advancedSleepConfig - Proxy proxyConfig +type loggerCfg struct { + logLevel string `ini:log_level` + logPath string `ini:"log_path"` } func PathExists(path string) (bool, error) { @@ -131,5 +139,50 @@ func LoadConfig() (*Config, error) { } -func init() { +func setLogger(env string, cfg *loggerCfg) { + // 预留未来构建桌面应用的对应参数 + // TODO 添加默认获取proxy的信息 + if env == "desktop" { + logger.SetFormatter(&logrus.JSONFormatter{}) + } else { + logger.SetFormatter(&logrus.TextFormatter{}) + } + + // 终端默认设置 + // 设置等级 + switch cfg.logLevel { + case "info": + logger.SetLevel(logrus.InfoLevel) + case "debug": + logger.SetLevel(logrus.InfoLevel) + case "error": + logger.SetLevel(logrus.InfoLevel) + default: + logger.SetLevel(logrus.DebugLevel) + } + + // 设置输出 + logger.SetOutput(os.Stdout) + + // 设置默认fields + + // 检测log目录是否存在,不存在则创建 + if ok, err := PathExists(cfg.logPath); err != nil { + logger.Error(err) + if !ok { + err = os.Mkdir(cfg.logPath, os.FileMode(0666)) + logger.Error(err) + } + } + + // TODO 添加生成文件log的hook + +} + +func init() { + allCfg, err := LoadConfig() + if err != nil { + fmt.Printf("loading config error: %v\n", err) + } + setLogger("terminal", &allCfg.Logger) }