This repository has been archived on 2024-04-10. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
elektron/elektronLogging/schedPolicySwitchLogger.go

52 lines
1.2 KiB
Go
Raw Normal View History

2019-11-13 11:19:04 -05:00
package elektronLogging
import (
log "github.com/sirupsen/logrus"
2019-11-20 13:33:46 -05:00
"os"
2019-11-21 15:26:57 -05:00
"path/filepath"
"strings"
2019-11-13 11:19:04 -05:00
)
type SchedPolicySwitchLogger struct {
LoggerImpl
}
func NewSchedPolicySwitchLogger(logType int, prefix string) *SchedPolicySwitchLogger {
sLog := &SchedPolicySwitchLogger{}
2019-11-13 11:19:04 -05:00
sLog.Type = logType
sLog.CreateLogFile(prefix)
2019-11-13 11:19:04 -05:00
return sLog
}
2019-11-21 18:04:59 -05:00
func (sLog SchedPolicySwitchLogger) Log(logType int, level log.Level, logData log.Fields, message string) {
2019-11-13 11:19:04 -05:00
if sLog.Type == logType {
logger.SetLevel(level)
2019-11-20 13:33:46 -05:00
if sLog.AllowOnConsole {
logger.SetOutput(os.Stdout)
logger.WithFields(logData).Println(message)
}
2019-11-13 11:19:04 -05:00
logger.SetOutput(sLog.LogFile)
logger.WithFields(logData).Println(message)
2019-11-13 11:19:04 -05:00
}
if sLog.next != nil {
sLog.next.Log(logType, level, logData, message)
}
}
func (sLog *SchedPolicySwitchLogger) CreateLogFile(prefix string) {
2019-11-13 11:19:04 -05:00
2019-11-21 18:04:59 -05:00
filename := strings.Join([]string{prefix, config.SPSConfig.FilenameExtension}, "")
2019-11-21 14:02:47 -05:00
dirName := logDir.getDirName()
if dirName != "" {
2019-11-21 18:04:59 -05:00
if logFile, err := os.Create(filepath.Join(dirName, filename)); err != nil {
log.Fatal("Unable to create logFile: ", err)
} else {
sLog.LogFile = logFile
2019-11-21 18:04:59 -05:00
sLog.AllowOnConsole = config.SPSConfig.AllowOnConsole
}
2019-11-13 11:19:04 -05:00
}
}