From fbde41823e9bdd3e927ed00dabeef5b5e2eed715 Mon Sep 17 00:00:00 2001 From: zhoupengcheng Date: Thu, 21 Mar 2024 16:43:48 +0800 Subject: [PATCH] add log_dir for atuned.cnf --- common/log/log.go | 32 ++++++++++++++++++++++++++++++++ misc/atuned.cnf | 2 ++ 2 files changed, 34 insertions(+) diff --git a/common/log/log.go b/common/log/log.go index 11d7e79..ae25175 100644 --- a/common/log/log.go +++ b/common/log/log.go @@ -19,6 +19,8 @@ import ( "os" "path/filepath" "runtime" + "time" + "strings" "github.com/go-ini/ini" logrus "github.com/sirupsen/logrus" @@ -42,6 +44,10 @@ func InitLogger(modes []string, cfg *ini.File) error { _ = baseLogger.SetLevel(sec.Key("level").MustString("info")) for _, mode := range modes { + logDir := sec.Key("log_dir").String() + if sec.HasKey("log_dir") && logDir != "" && strings.HasPrefix(logDir, "/var/log/") { + mode = "file" + } switch mode { case "console": baseLogger.entry.Logger.Out = os.Stdout @@ -52,12 +58,38 @@ func InitLogger(modes []string, cfg *ini.File) error { baseLogger.Errorf("syslog hook init failed:%v", err) } baseLogger.entry.Logger.AddHook(syslogHook) + case "file": + dir := sec.Key("log_dir").MustString("/var/log/atune") + logFile, err := getLogFile(dir) + if err != nil { + baseLogger.Errorf("log file model init failed:%v", err) + return err + } + baseLogger.entry.Logger.Out = logFile + baseLogger.entry.Logger.Formatter = formatter } } return nil } +func getLogFile(baseDir string) (*os.File, error) { + if _, err := os.Stat(baseDir); os.IsNotExist(err) { + if err := os.MkdirAll(baseDir, 0755); err != nil { + return nil, nil + } + } + today := time.Now().Format("060102") + logFileName := filepath.Join(baseDir, fmt.Sprintf("atune-%s.log", today)) + + // check file exist status, if not create it + file, err := os.OpenFile(logFileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + return nil, err + } + return file, nil +} + func (log logger) withFileField() *logrus.Entry { _, filename, line, ok := runtime.Caller(2) if ok { diff --git a/misc/atuned.cnf b/misc/atuned.cnf index dad7f58..351a118 100755 --- a/misc/atuned.cnf +++ b/misc/atuned.cnf @@ -72,6 +72,8 @@ tlsengineclientkeyfile = /etc/atuned/engine_certs/client.key [log] # either "debug", "info", "warn", "error", "critical", default is "info" level = info +# you can set log_dir to specify the location where the log is written. default location is /var/log/messages +# log_dir = "/var/log/atune" #################################### monitor ############################### [monitor]