From bf81246e0e2b6f1228d3b8f4564b168f3616ecdd Mon Sep 17 00:00:00 2001 From: YanzheL Date: Fri, 27 Dec 2019 18:50:34 +0800 Subject: [PATCH] Configure logfile path via 'logConfig.path' property Signed-off-by: YanzheL --- cmd/dfdaemon/app/init.go | 6 ++---- pkg/dflog/log.go | 7 ++++++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/cmd/dfdaemon/app/init.go b/cmd/dfdaemon/app/init.go index 7ed9151c7..d2fc234d2 100644 --- a/cmd/dfdaemon/app/init.go +++ b/cmd/dfdaemon/app/init.go @@ -80,15 +80,13 @@ func initLogger(cfg config.Properties) error { cfg.WorkHome = filepath.Join(current.HomeDir, ".small-dragonfly") } - logFilePath := filepath.Join(cfg.WorkHome, "logs", "dfdaemon.log") - opts := []dflog.Option{ - dflog.WithLogFile(logFilePath, cfg.LogConfig.MaxSize, cfg.LogConfig.MaxBackups), + dflog.WithLogFile(cfg.LogConfig.Path, cfg.LogConfig.MaxSize, cfg.LogConfig.MaxBackups), dflog.WithSign(fmt.Sprintf("%d", os.Getpid())), dflog.WithDebug(cfg.Verbose), } - logrus.Debugf("use log file %s", logFilePath) + logrus.Debugf("use log file %s", cfg.LogConfig.Path) return errors.Wrap(dflog.Init(logrus.StandardLogger(), opts...), "init log") } diff --git a/pkg/dflog/log.go b/pkg/dflog/log.go index 1758719f8..af97c9ee5 100644 --- a/pkg/dflog/log.go +++ b/pkg/dflog/log.go @@ -20,6 +20,7 @@ import ( "bytes" "fmt" "os" + "path/filepath" "strings" "github.com/pkg/errors" @@ -35,6 +36,10 @@ type LogConfig struct { // MaxBackups is the maximum number of old log files to retain. // The default value is 1. MaxBackups int `yaml:"maxBackups" json:"maxBackups"` + + // Path is the location of log file + // The default value is logs/dfdaemon.log + Path string `yaml:"path" json:"path"` } // DefaultLogTimeFormat defines the timestamp format. @@ -72,7 +77,7 @@ func getLumberjack(l *logrus.Logger) *lumberjack.Logger { func WithLogFile(f string, maxSize, maxBackups int) Option { return func(l *logrus.Logger) error { if f == "" { - return nil + f = filepath.Join("logs", "dfdaemon.log") } if maxSize <= 0 { maxSize = 40