1. Instead of maintaining a global config, each specialized logger now stores its config. 2. Refactored logInterface to elektronLogger. 3. Refactored loggerImpl to baseElektronLogger to be consistent with the rest of the code base. 4. Wrapped elektronLogger#Log(...) and elektronLogf(...) so that we do not have to use the instance of elektronLogger everytime we want to log. Instead, we just do logging.Log(...) or logging.Logf(...). 5. Wrapped elektronLogger#WithFields(...) and elektronLogger#WithField(...). 6. Refactored codebase to adhere to the changes.
99 lines
2.3 KiB
Go
99 lines
2.3 KiB
Go
package logging
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
type SchedPolicySwitchLogger struct {
|
|
baseElektronLogger
|
|
}
|
|
|
|
func NewSchedPolicySwitchLogger(
|
|
config *LoggerConfig,
|
|
b *baseLogData,
|
|
logType int,
|
|
prefix string,
|
|
logger *log.Logger,
|
|
logDir *logDirectory) *SchedPolicySwitchLogger {
|
|
|
|
sLog := &SchedPolicySwitchLogger{
|
|
baseElektronLogger: baseElektronLogger{
|
|
baseLogData: b,
|
|
config: struct {
|
|
Enabled bool
|
|
FilenameExtension string
|
|
AllowOnConsole bool
|
|
}{
|
|
Enabled: config.SPSConfig.Enabled,
|
|
FilenameExtension: config.SPSConfig.FilenameExtension,
|
|
AllowOnConsole: config.SPSConfig.AllowOnConsole,
|
|
},
|
|
logType: logType,
|
|
next: nil,
|
|
logger: logger,
|
|
logDir: logDir,
|
|
},
|
|
}
|
|
|
|
sLog.createLogFile(prefix)
|
|
return sLog
|
|
}
|
|
|
|
func (sLog SchedPolicySwitchLogger) Log(logType int, level log.Level, message string) {
|
|
if sLog.logType == logType {
|
|
if sLog.isEnabled() {
|
|
if sLog.config.AllowOnConsole {
|
|
sLog.logger.SetOutput(os.Stdout)
|
|
sLog.logger.WithFields(sLog.data).Log(level, message)
|
|
}
|
|
|
|
sLog.logger.SetOutput(sLog.logFile)
|
|
sLog.logger.WithFields(sLog.data).Log(level, message)
|
|
}
|
|
}
|
|
if sLog.next != nil {
|
|
sLog.next.Log(logType, level, message)
|
|
} else {
|
|
// Clearing the fields.
|
|
sLog.resetFields()
|
|
}
|
|
}
|
|
|
|
func (sLog SchedPolicySwitchLogger) Logf(logType int, level log.Level, msgFmtString string, args ...interface{}) {
|
|
if sLog.logType == logType {
|
|
if sLog.isEnabled() {
|
|
if sLog.config.AllowOnConsole {
|
|
sLog.logger.SetOutput(os.Stdout)
|
|
sLog.logger.WithFields(sLog.data).Logf(level, msgFmtString, args...)
|
|
}
|
|
|
|
sLog.logger.SetOutput(sLog.logFile)
|
|
sLog.logger.WithFields(sLog.data).Logf(level, msgFmtString, args...)
|
|
}
|
|
}
|
|
// Forwarding to next logger
|
|
if sLog.next != nil {
|
|
sLog.next.Logf(logType, level, msgFmtString, args...)
|
|
} else {
|
|
// Clearing the fields.
|
|
sLog.resetFields()
|
|
}
|
|
}
|
|
|
|
func (sLog *SchedPolicySwitchLogger) createLogFile(prefix string) {
|
|
if sLog.isEnabled() {
|
|
filename := strings.Join([]string{prefix, sLog.config.FilenameExtension}, "")
|
|
dirName := sLog.logDir.getDirName()
|
|
if dirName != "" {
|
|
if logFile, err := os.Create(filepath.Join(dirName, filename)); err != nil {
|
|
log.Fatal("Unable to create logFile: ", err)
|
|
} else {
|
|
sLog.logFile = logFile
|
|
}
|
|
}
|
|
}
|
|
}
|