From 1b52a311dcb35853007d0075b7d0be449f486d5a Mon Sep 17 00:00:00 2001 From: Mmx233 Date: Thu, 1 Sep 2022 14:31:11 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=94=B9=20debug=20=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E4=B8=BA=20log=20=E6=A8=A1=E5=9D=97=20#11?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- global/config.go | 4 ++-- global/log.go | 42 +++++++++++++++++++++++------------------- models/config.go | 15 ++++++++------- 3 files changed, 33 insertions(+), 28 deletions(-) diff --git a/global/config.go b/global/config.go index b10bac1..1e9dee9 100644 --- a/global/config.go +++ b/global/config.go @@ -36,8 +36,8 @@ func readConfig() { Guardian: srunModels.Guardian{ Duration: 300, }, - Debug: srunModels.Debug{ - LogPath: "./", + Log: srunModels.Log{ + FilePath: "./", }, }) diff --git a/global/log.go b/global/log.go index 4a31754..98a9151 100644 --- a/global/log.go +++ b/global/log.go @@ -9,27 +9,31 @@ import ( ) func initLog() { - if Config.Settings.Debug.Enable { + if Config.Settings.Log.DebugLevel { log.SetLevel(log.DebugLevel) + } - if Config.Settings.Debug.WriteLog { - //日志路径初始化与处理 - if !strings.HasSuffix(Config.Settings.Debug.LogPath, "/") { - Config.Settings.Debug.LogPath += "/" - } - e := os.MkdirAll(Config.Settings.Debug.LogPath, os.ModePerm) - if e != nil { - log.Fatalln(e) - } - - f, e := os.OpenFile(Config.Settings.Debug.LogPath+time.Now().Format("2006.01.02-15.04.05")+".log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 700) - if e != nil { - log.Fatalln(e) - } - - //设置双重输出 - mw := io.MultiWriter(os.Stdout, f) - log.SetOutput(mw) + if Config.Settings.Log.WriteFile { + //日志路径初始化与处理 + if !strings.HasSuffix(Config.Settings.Log.FilePath, "/") { + Config.Settings.Log.FilePath += "/" } + e := os.MkdirAll(Config.Settings.Log.FilePath, os.ModePerm) + if e != nil { + log.Fatalln(e) + } + + if Config.Settings.Log.FileName == "" { + Config.Settings.Log.FileName = time.Now().Format("2006.01.02-15.04.05") + ".log" + } + + f, e := os.OpenFile(Config.Settings.Log.FilePath+Config.Settings.Log.FileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 700) + if e != nil { + log.Fatalln(e) + } + + //设置双重输出 + mw := io.MultiWriter(os.Stdout, f) + log.SetOutput(mw) } } diff --git a/models/config.go b/models/config.go index 5177504..3a0b18b 100644 --- a/models/config.go +++ b/models/config.go @@ -19,17 +19,18 @@ type Basic struct { Interfaces string `json:"interfaces" yaml:"interfaces" mapstructure:"interfaces"` } +type Log struct { + DebugLevel bool `json:"debug_level" yaml:"debug_level" mapstructure:"debug_level"` + WriteFile bool `json:"write_file" yaml:"write_file" mapstructure:"write_file"` + FilePath string `json:"log_path" yaml:"log_path" mapstructure:"log_path"` + FileName string `json:"log_name" yaml:"log_name" mapstructure:"log_name"` +} + type Settings struct { Basic Basic `json:"basic" yaml:"basic" mapstructure:"basic"` Guardian Guardian `json:"guardian" yaml:"guardian" mapstructure:"guardian"` Daemon Daemon `json:"daemon" yaml:"daemon" mapstructure:"daemon"` - Debug Debug `json:"debug" yaml:"debug" mapstructure:"debug"` -} - -type Debug struct { - Enable bool `json:"enable" yaml:"enable" mapstructure:"enable"` - WriteLog bool `json:"write_log" yaml:"write_log" mapstructure:"write_log"` - LogPath string `json:"log_path" yaml:"log_path" mapstructure:"log_path"` + Log Log } type Config struct {