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/pcpLogger.go

52 lines
1.1 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"
"strings"
2019-11-13 11:19:04 -05:00
)
type PcpLogger struct {
LoggerImpl
}
func NewPcpLogger(logType int, prefix string) *PcpLogger {
pLog := &PcpLogger{}
2019-11-13 11:19:04 -05:00
pLog.Type = logType
pLog.SetLogFile(prefix)
return pLog
}
func (pLog *PcpLogger) Log(logType int, level log.Level, logData log.Fields, message string) {
2019-11-13 11:19:04 -05:00
if pLog.Type == logType {
logger.SetLevel(level)
2019-11-20 13:33:46 -05:00
if pLog.AllowOnConsole {
logger.SetOutput(os.Stdout)
logger.WithFields(logData).Println(message)
}
2019-11-13 11:19:04 -05:00
logger.SetOutput(pLog.LogFileName)
logger.WithFields(logData).Println(message)
2019-11-13 11:19:04 -05:00
}
if pLog.next != nil {
pLog.next.Log(logType, level, logData, message)
}
}
func (plog *PcpLogger) SetLogFile(prefix string) {
pcpLogPrefix := strings.Join([]string{prefix, config.PCPConfig.FilenameExtension}, "")
2019-11-21 14:02:47 -05:00
dirName := logDir.getDirName()
if dirName != "" {
pcpLogPrefix = strings.Join([]string{dirName, pcpLogPrefix}, "/")
2019-11-13 11:19:04 -05:00
}
if logFile, err := os.Create(pcpLogPrefix); err != nil {
log.Fatal("Unable to create logFile: ", err)
2019-11-13 11:19:04 -05:00
} else {
plog.LogFileName = logFile
2019-11-20 13:33:46 -05:00
plog.AllowOnConsole = config.PCPConfig.AllowOnConsole
2019-11-13 11:19:04 -05:00
}
}