Skip to content

Commit

Permalink
[vendor] refs #298 - Keep references of packages loggers
Browse files Browse the repository at this point in the history
This allows to changes logger properties and propagate this changes across all modules's logger
  • Loading branch information
AntiD2ta committed Jan 18, 2020
1 parent 2346a76 commit f3a006e
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/util/logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
// Logger wraps logrus.FieldLogger
type Logger struct {
logrus.FieldLogger

moduleName string
}

// Critical adds special critical-level fields for specially highlighted logging,
Expand All @@ -26,6 +28,8 @@ func (logger *Logger) WithTime(t time.Time) *logrus.Entry {
// MasterLogger wraps logrus.Logger and is able to create new package-aware loggers
type MasterLogger struct {
*logrus.Logger

packageModules []*Logger
}

// NewMasterLogger creates a new package-aware logger with formatting string
Expand All @@ -46,14 +50,18 @@ func NewMasterLogger() *MasterLogger {
Hooks: hooks,
Level: logrus.DebugLevel,
},
packageModules: []*Logger{},
}
}

// PackageLogger instantiates a package-aware logger
func (logger *MasterLogger) PackageLogger(moduleName string) *Logger {
return &Logger{
pkgLogger := &Logger{
FieldLogger: logger.WithField(logModuleKey, moduleName),
moduleName: moduleName,
}
logger.packageModules = append(logger.packageModules, pkgLogger)
return pkgLogger
}

// AddHook adds a logrus.Hook to the logger and its module loggers
Expand All @@ -64,6 +72,9 @@ func (logger *MasterLogger) AddHook(hook logrus.Hook) {
// SetLevel sets the log level for the logger and its module loggers
func (logger *MasterLogger) SetLevel(level logrus.Level) {
logger.Level = level
for _, pl := range logger.packageModules {
pl.FieldLogger = logger.WithField(logModuleKey, pl.moduleName)
}
}

// EnableColors enables colored logging
Expand Down

0 comments on commit f3a006e

Please sign in to comment.