From c531afb89367fe4a77de436b09157654fc338bdc Mon Sep 17 00:00:00 2001 From: Starnop Date: Fri, 15 Nov 2019 20:54:15 +0800 Subject: [PATCH] feature: make the log related parameter can be configured Signed-off-by: Starnop --- cmd/dfdaemon/app/init.go | 2 +- cmd/dfget/app/root.go | 2 +- cmd/dfget/app/server.go | 2 +- cmd/supernode/app/root.go | 8 ++++---- dfdaemon/config/config.go | 3 +++ dfget/config/config.go | 4 ++++ pkg/dflog/log.go | 29 ++++++++++++++++++++++++++--- pkg/dflog/log_test.go | 2 +- supernode/config/config.go | 3 +++ 9 files changed, 44 insertions(+), 11 deletions(-) diff --git a/cmd/dfdaemon/app/init.go b/cmd/dfdaemon/app/init.go index be886daaf..7ed9151c7 100644 --- a/cmd/dfdaemon/app/init.go +++ b/cmd/dfdaemon/app/init.go @@ -83,7 +83,7 @@ func initLogger(cfg config.Properties) error { logFilePath := filepath.Join(cfg.WorkHome, "logs", "dfdaemon.log") opts := []dflog.Option{ - dflog.WithLogFile(logFilePath), + dflog.WithLogFile(logFilePath, cfg.LogConfig.MaxSize, cfg.LogConfig.MaxBackups), dflog.WithSign(fmt.Sprintf("%d", os.Getpid())), dflog.WithDebug(cfg.Verbose), } diff --git a/cmd/dfget/app/root.go b/cmd/dfget/app/root.go index 992281739..9f87ce355 100644 --- a/cmd/dfget/app/root.go +++ b/cmd/dfget/app/root.go @@ -188,7 +188,7 @@ func initClientLog() error { logFilePath := filepath.Join(cfg.WorkHome, "logs", "dfclient.log") opts := []dflog.Option{ - dflog.WithLogFile(logFilePath), + dflog.WithLogFile(logFilePath, cfg.LogConfig.MaxSize, cfg.LogConfig.MaxBackups), dflog.WithSign(cfg.Sign), dflog.WithDebug(cfg.Verbose), } diff --git a/cmd/dfget/app/server.go b/cmd/dfget/app/server.go index 3e7ebadff..a38344daf 100644 --- a/cmd/dfget/app/server.go +++ b/cmd/dfget/app/server.go @@ -85,7 +85,7 @@ func initServerLog() error { logFilePath := filepath.Join(cfg.WorkHome, "logs", "dfserver.log") opts := []dflog.Option{ - dflog.WithLogFile(logFilePath), + dflog.WithLogFile(logFilePath, cfg.LogConfig.MaxSize, cfg.LogConfig.MaxBackups), dflog.WithSign(cfg.Sign), dflog.WithDebug(cfg.Verbose), } diff --git a/cmd/supernode/app/root.go b/cmd/supernode/app/root.go index b8287f618..6ec50dc06 100644 --- a/cmd/supernode/app/root.go +++ b/cmd/supernode/app/root.go @@ -73,13 +73,13 @@ var rootCmd = &cobra.Command{ } // initialize supernode logger. - if err := initLog(logrus.StandardLogger(), "app.log"); err != nil { + if err := initLog(logrus.StandardLogger(), "app.log", cfg.LogConfig); err != nil { return err } // initialize dfget logger. dfgetLogger := logrus.New() - if err := initLog(dfgetLogger, "dfget.log"); err != nil { + if err := initLog(dfgetLogger, "dfget.log", cfg.LogConfig); err != nil { return err } @@ -326,11 +326,11 @@ func decodeWithYAML(types ...reflect.Type) mapstructure.DecodeHookFunc { } // initLog initializes log Level and log format. -func initLog(logger *logrus.Logger, logPath string) error { +func initLog(logger *logrus.Logger, logPath string, logConfig dflog.LogConfig) error { logFilePath := filepath.Join(supernodeViper.GetString("base.homeDir"), "logs", logPath) opts := []dflog.Option{ - dflog.WithLogFile(logFilePath), + dflog.WithLogFile(logFilePath, logConfig.MaxSize, logConfig.MaxBackups), dflog.WithSign(fmt.Sprintf("%d", os.Getpid())), dflog.WithDebug(supernodeViper.GetBool("base.debug")), } diff --git a/dfdaemon/config/config.go b/dfdaemon/config/config.go index 2b46bcba6..2ac0105e0 100644 --- a/dfdaemon/config/config.go +++ b/dfdaemon/config/config.go @@ -26,6 +26,7 @@ import ( "regexp" "github.com/dragonflyoss/Dragonfly/dfdaemon/constant" + "github.com/dragonflyoss/Dragonfly/pkg/dflog" dferr "github.com/dragonflyoss/Dragonfly/pkg/errortypes" "github.com/dragonflyoss/Dragonfly/pkg/rate" @@ -109,6 +110,8 @@ type Properties struct { WorkHome string `yaml:"workHome" json:"workHome,omitempty"` DFRepo string `yaml:"localrepo" json:"localrepo"` DFPath string `yaml:"dfpath" json:"dfpath"` + + LogConfig dflog.LogConfig `yaml:"logConfig" json:"logConfig"` } // Validate validates the config diff --git a/dfget/config/config.go b/dfget/config/config.go index 2ff03e350..73d35f41e 100644 --- a/dfget/config/config.go +++ b/dfget/config/config.go @@ -27,12 +27,14 @@ import ( "syscall" "time" + "github.com/dragonflyoss/Dragonfly/pkg/dflog" "github.com/dragonflyoss/Dragonfly/pkg/errortypes" "github.com/dragonflyoss/Dragonfly/pkg/fileutils" "github.com/dragonflyoss/Dragonfly/pkg/netutils" "github.com/dragonflyoss/Dragonfly/pkg/printer" "github.com/dragonflyoss/Dragonfly/pkg/rate" "github.com/dragonflyoss/Dragonfly/pkg/stringutils" + "github.com/pkg/errors" "gopkg.in/gcfg.v1" "gopkg.in/warnings.v0" @@ -83,6 +85,8 @@ type Properties struct { // WorkHome work home path, // default: `$HOME/.small-dragonfly`. WorkHome string `yaml:"workHome" json:"workHome,omitempty"` + + LogConfig dflog.LogConfig `yaml:"logConfig" json:"logConfig"` } // NewProperties creates a new properties with default values. diff --git a/pkg/dflog/log.go b/pkg/dflog/log.go index 14b554f97..1758719f8 100644 --- a/pkg/dflog/log.go +++ b/pkg/dflog/log.go @@ -27,6 +27,16 @@ import ( "gopkg.in/natefinch/lumberjack.v2" ) +// LogConfig holds all configurable properties of log. +type LogConfig struct { + // MaxSize is the maximum size in megabytes of the log file before it gets rotated. + // It defaults to 40 megabytes. + MaxSize int `yaml:"maxSize" json:"maxSize"` + // MaxBackups is the maximum number of old log files to retain. + // The default value is 1. + MaxBackups int `yaml:"maxBackups" json:"maxBackups"` +} + // DefaultLogTimeFormat defines the timestamp format. const DefaultLogTimeFormat = "2006-01-02 15:04:05.000" @@ -51,18 +61,31 @@ func getLumberjack(l *logrus.Logger) *lumberjack.Logger { } // WithLogFile sets the logger to output to the given file, with log rotation. +// // If the given file is empty, nothing will be done. -func WithLogFile(f string) Option { +// +// The maxSize is the maximum size in megabytes of the log file before it gets rotated. +// It defaults to 40 megabytes. +// +// The maxBackups is the maximum number of old log files to retain. +// The default value is 1. +func WithLogFile(f string, maxSize, maxBackups int) Option { return func(l *logrus.Logger) error { if f == "" { return nil } + if maxSize <= 0 { + maxSize = 40 + } + if maxBackups <= 0 { + maxBackups = 1 + } if logger := getLumberjack(l); logger == nil { l.SetOutput(&lumberjack.Logger{ Filename: f, - MaxSize: 40, // mb - MaxBackups: 1, + MaxSize: maxSize, // mb + MaxBackups: maxBackups, }) } else { logger.Filename = f diff --git a/pkg/dflog/log_test.go b/pkg/dflog/log_test.go index 21ac0321c..d9f0be19e 100644 --- a/pkg/dflog/log_test.go +++ b/pkg/dflog/log_test.go @@ -52,7 +52,7 @@ func (ts *logTestSuite) TestLogFile() { defer os.RemoveAll(f.Name()) l := logrus.New() - r.Nil(Init(l, WithLogFile(f.Name()))) + r.Nil(Init(l, WithLogFile(f.Name(), -1, -1))) lumberjack := getLumberjack(l) r.NotNil(lumberjack) r.Equal(f.Name(), lumberjack.Filename) diff --git a/supernode/config/config.go b/supernode/config/config.go index 4c4ebd294..290daf237 100644 --- a/supernode/config/config.go +++ b/supernode/config/config.go @@ -22,6 +22,7 @@ import ( "strings" "time" + "github.com/dragonflyoss/Dragonfly/pkg/dflog" "github.com/dragonflyoss/Dragonfly/pkg/fileutils" "github.com/dragonflyoss/Dragonfly/pkg/rate" @@ -246,4 +247,6 @@ type BaseProperties struct { // // default: 1 CleanRatio int + + LogConfig dflog.LogConfig `yaml:"logConfig" json:"logConfig"` }