Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

Commit

Permalink
feature: make the log related parameter can be configured
Browse files Browse the repository at this point in the history
Signed-off-by: Starnop <[email protected]>
  • Loading branch information
starnop committed Nov 15, 2019
1 parent 17187dc commit c531afb
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cmd/dfdaemon/app/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/dfget/app/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/dfget/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/supernode/app/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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")),
}
Expand Down
3 changes: 3 additions & 0 deletions dfdaemon/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions dfget/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down
29 changes: 26 additions & 3 deletions pkg/dflog/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/dflog/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions supernode/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -246,4 +247,6 @@ type BaseProperties struct {
//
// default: 1
CleanRatio int

LogConfig dflog.LogConfig `yaml:"logConfig" json:"logConfig"`
}

0 comments on commit c531afb

Please sign in to comment.