Added enabled check while creating log files and logging. Do not allow to create log file or log if the log type is disabled.
55 lines
1.3 KiB
Go
55 lines
1.3 KiB
Go
package elektronLogging
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
type SchedPolicySwitchLogger struct {
|
|
LoggerImpl
|
|
}
|
|
|
|
func NewSchedPolicySwitchLogger(logType int, prefix string) *SchedPolicySwitchLogger {
|
|
sLog := &SchedPolicySwitchLogger{}
|
|
sLog.Type = logType
|
|
sLog.CreateLogFile(prefix)
|
|
return sLog
|
|
}
|
|
|
|
func (sLog SchedPolicySwitchLogger) Log(logType int, level log.Level, logData log.Fields, message string) {
|
|
if config.SPSConfig.Enabled {
|
|
if sLog.Type == logType {
|
|
|
|
logger.SetLevel(level)
|
|
|
|
if sLog.AllowOnConsole {
|
|
logger.SetOutput(os.Stdout)
|
|
logger.WithFields(logData).Println(message)
|
|
}
|
|
|
|
logger.SetOutput(sLog.LogFile)
|
|
logger.WithFields(logData).Println(message)
|
|
}
|
|
if sLog.next != nil {
|
|
sLog.next.Log(logType, level, logData, message)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (sLog *SchedPolicySwitchLogger) CreateLogFile(prefix string) {
|
|
if config.SPSConfig.Enabled {
|
|
filename := strings.Join([]string{prefix, config.SPSConfig.FilenameExtension}, "")
|
|
dirName := 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
|
|
sLog.AllowOnConsole = config.SPSConfig.AllowOnConsole
|
|
}
|
|
}
|
|
}
|
|
}
|