Code formatting
This commit is contained in:
parent
ef8bd6ac1f
commit
4d15e59d4c
41 changed files with 472 additions and 475 deletions
|
@ -3,9 +3,9 @@ package elektronLogging
|
|||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/fatih/color"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"strings"
|
||||
"github.com/fatih/color"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type ElektronFormatter struct {
|
||||
|
@ -13,18 +13,18 @@ type ElektronFormatter struct {
|
|||
}
|
||||
|
||||
func (f ElektronFormatter) getColor(entry *log.Entry) *color.Color {
|
||||
switch entry.Level {
|
||||
case log.InfoLevel:
|
||||
return color.New(color.FgGreen, color.Bold)
|
||||
case log.WarnLevel:
|
||||
return color.New(color.FgYellow, color.Bold)
|
||||
case log.ErrorLevel:
|
||||
return color.New(color.FgRed, color.Bold)
|
||||
case log.FatalLevel:
|
||||
return color.New(color.FgRed, color.Bold)
|
||||
default:
|
||||
return color.New(color.FgWhite, color.Bold)
|
||||
}
|
||||
switch entry.Level {
|
||||
case log.InfoLevel:
|
||||
return color.New(color.FgGreen, color.Bold)
|
||||
case log.WarnLevel:
|
||||
return color.New(color.FgYellow, color.Bold)
|
||||
case log.ErrorLevel:
|
||||
return color.New(color.FgRed, color.Bold)
|
||||
case log.FatalLevel:
|
||||
return color.New(color.FgRed, color.Bold)
|
||||
default:
|
||||
return color.New(color.FgWhite, color.Bold)
|
||||
}
|
||||
}
|
||||
func (f ElektronFormatter) Format(entry *log.Entry) ([]byte, error) {
|
||||
var b *bytes.Buffer
|
||||
|
@ -35,19 +35,18 @@ func (f ElektronFormatter) Format(entry *log.Entry) ([]byte, error) {
|
|||
b = &bytes.Buffer{}
|
||||
}
|
||||
|
||||
levelColor := f.getColor(entry)
|
||||
level := levelColor.Sprintf("[%s]:",strings.ToUpper(entry.Level.String()))
|
||||
message := fmt.Sprintf("%s %s ",level,entry.Time.Format(f.TimestampFormat))
|
||||
if entry.Message != "" {
|
||||
message = fmt.Sprintf("%s %s %s ",level,entry.Time.Format(f.TimestampFormat), entry.Message)
|
||||
}
|
||||
levelColor := f.getColor(entry)
|
||||
level := levelColor.Sprintf("[%s]:", strings.ToUpper(entry.Level.String()))
|
||||
message := fmt.Sprintf("%s %s ", level, entry.Time.Format(f.TimestampFormat))
|
||||
if entry.Message != "" {
|
||||
message = fmt.Sprintf("%s %s %s ", level, entry.Time.Format(f.TimestampFormat), entry.Message)
|
||||
}
|
||||
var formattedFields []string
|
||||
for key, value := range entry.Data {
|
||||
formattedFields = append(formattedFields,
|
||||
strings.Join([]string{key, fmt.Sprintf("%s", value)}, "="))
|
||||
formattedFields = append(formattedFields,
|
||||
strings.Join([]string{key, fmt.Sprintf("%s", value)}, "="))
|
||||
}
|
||||
|
||||
|
||||
|
||||
b.WriteString(message)
|
||||
b.WriteString(strings.Join(formattedFields, ", "))
|
||||
b.WriteByte('\n')
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package elektronLogging
|
||||
|
||||
import (
|
||||
"os"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"os"
|
||||
)
|
||||
|
||||
type ClsfnTaskDistOverheadLogger struct {
|
||||
|
@ -16,16 +16,16 @@ func NewClsfnTaskDistOverheadLogger(logType int, prefix string) *ClsfnTaskDistOv
|
|||
return cLog
|
||||
}
|
||||
|
||||
func (cLog *ClsfnTaskDistOverheadLogger) Log(logType int, level log.Level, logData log.Fields,message string) {
|
||||
func (cLog *ClsfnTaskDistOverheadLogger) Log(logType int, level log.Level, logData log.Fields, message string) {
|
||||
if cLog.Type == logType {
|
||||
|
||||
logger.SetLevel(level)
|
||||
|
||||
if cLog.AllowOnConsole {
|
||||
logger.SetOutput(os.Stdout)
|
||||
logger.WithFields(logData).Println(message)
|
||||
}
|
||||
|
||||
|
||||
if cLog.AllowOnConsole {
|
||||
logger.SetOutput(os.Stdout)
|
||||
logger.WithFields(logData).Println(message)
|
||||
}
|
||||
|
||||
logger.SetOutput(cLog.LogFileName)
|
||||
logger.WithFields(logData).Println(message)
|
||||
}
|
||||
|
@ -44,6 +44,6 @@ func (cLog *ClsfnTaskDistOverheadLogger) SetLogFile(prefix string) {
|
|||
log.Fatal("Unable to create logFile: ", err)
|
||||
} else {
|
||||
cLog.LogFileName = logFile
|
||||
cLog.AllowOnConsole = config.TaskDistConfig.AllowOnConsole
|
||||
cLog.AllowOnConsole = config.TaskDistConfig.AllowOnConsole
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package elektronLogging
|
||||
|
||||
import (
|
||||
"os"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"os"
|
||||
)
|
||||
|
||||
type ConsoleLogger struct {
|
||||
|
@ -20,10 +20,10 @@ func (cLog *ConsoleLogger) Log(logType int, level log.Level, logData log.Fields,
|
|||
|
||||
//logFields := cloneFields(logData)
|
||||
logger.SetLevel(level)
|
||||
|
||||
|
||||
logger.SetOutput(os.Stdout)
|
||||
logger.WithFields(logData).Println(message)
|
||||
|
||||
|
||||
logger.SetOutput(cLog.LogFileName)
|
||||
logger.WithFields(logData).Println(message)
|
||||
}
|
||||
|
@ -42,6 +42,6 @@ func (cLog *ConsoleLogger) SetLogFile(prefix string) {
|
|||
log.Fatal("Unable to create logFile: ", err)
|
||||
} else {
|
||||
cLog.LogFileName = logFile
|
||||
cLog.AllowOnConsole = true
|
||||
cLog.AllowOnConsole = true
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
package elektronLogging
|
||||
|
||||
import (
|
||||
logrus "github.com/sirupsen/logrus"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
logrus "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var logDir string
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
package elektronLogging
|
||||
|
||||
import (
|
||||
"time"
|
||||
"fmt"
|
||||
"strconv"
|
||||
. "github.com/spdfg/elektron/elektronLogging/types"
|
||||
"fmt"
|
||||
log "github.com/sirupsen/logrus"
|
||||
. "github.com/spdfg/elektron/elektronLogging/types"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var config LoggerConfig
|
||||
|
@ -14,25 +14,25 @@ var formatter ElektronFormatter
|
|||
|
||||
func BuildLogger() *LoggerImpl {
|
||||
|
||||
// read configuration from yaml
|
||||
// read configuration from yaml
|
||||
config.GetConfig()
|
||||
|
||||
// create the log directory
|
||||
|
||||
// create the log directory
|
||||
startTime := time.Now()
|
||||
formatter.TimestampFormat = "2006-01-02 15:04:05"
|
||||
GetLogDir(startTime, "_")
|
||||
|
||||
prefix := fmt.Sprintf("_%d%d%s%s%s%s",startTime.Year(), startTime.Month(),strconv.Itoa(startTime.Day()),
|
||||
strconv.Itoa(startTime.Hour()),strconv.Itoa(startTime.Minute()),strconv.Itoa(startTime.Second()))
|
||||
|
||||
//create a single logrus instance and set its formatter to ElektronFormatter
|
||||
prefix := fmt.Sprintf("_%d%d%s%s%s%s", startTime.Year(), startTime.Month(), strconv.Itoa(startTime.Day()),
|
||||
strconv.Itoa(startTime.Hour()), strconv.Itoa(startTime.Minute()), strconv.Itoa(startTime.Second()))
|
||||
|
||||
//create a single logrus instance and set its formatter to ElektronFormatter
|
||||
logger = log.New()
|
||||
logger.SetFormatter(&formatter)
|
||||
|
||||
// create a chain of loggers
|
||||
// create a chain of loggers
|
||||
head := new(LoggerImpl)
|
||||
cLog := NewConsoleLogger(CONSOLE,prefix)
|
||||
pLog := NewPcpLogger(PCP,prefix)
|
||||
cLog := NewConsoleLogger(CONSOLE, prefix)
|
||||
pLog := NewPcpLogger(PCP, prefix)
|
||||
schedTraceLog := NewSchedTraceLogger(SCHED_TRACE, prefix)
|
||||
spsLog := NewSchedPolicySwitchLogger(SPS, prefix)
|
||||
schedWindowLog := NewSchedWindowLogger(SCHED_WINDOW, prefix)
|
||||
|
|
|
@ -12,10 +12,10 @@ type Logger interface {
|
|||
SetLogFile(prefix string)
|
||||
}
|
||||
type LoggerImpl struct {
|
||||
Type int
|
||||
AllowOnConsole bool
|
||||
LogFileName *os.File
|
||||
next Logger
|
||||
Type int
|
||||
AllowOnConsole bool
|
||||
LogFileName *os.File
|
||||
next Logger
|
||||
}
|
||||
|
||||
func (l *LoggerImpl) SetNext(logType Logger) {
|
||||
|
|
|
@ -1,68 +1,68 @@
|
|||
package elektronLogging
|
||||
|
||||
import (
|
||||
"gopkg.in/yaml.v2"
|
||||
"io/ioutil"
|
||||
log "github.com/sirupsen/logrus"
|
||||
elekEnv "github.com/spdfg/elektron/environment"
|
||||
log "github.com/sirupsen/logrus"
|
||||
elekEnv "github.com/spdfg/elektron/environment"
|
||||
"gopkg.in/yaml.v2"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
type LoggerConfig struct {
|
||||
|
||||
SchedTraceConfig struct {
|
||||
Enabled bool `yaml:"enabled"`
|
||||
FilenameExtension string `yaml:"filenameExtension"`
|
||||
EnableColumnHeaders bool `yaml:"enableColumnHeaders"`
|
||||
AllowOnConsole bool `yaml:"allowOnConsole"`
|
||||
Enabled bool `yaml:"enabled"`
|
||||
FilenameExtension string `yaml:"filenameExtension"`
|
||||
EnableColumnHeaders bool `yaml:"enableColumnHeaders"`
|
||||
AllowOnConsole bool `yaml:"allowOnConsole"`
|
||||
} `yaml:"schedTrace"`
|
||||
|
||||
PCPConfig struct {
|
||||
Enabled bool `yaml:"enabled"`
|
||||
FilenameExtension string `yaml:"filenameExtension"`
|
||||
EnableColumnHeaders bool `yaml:"enableColumnHeaders"`
|
||||
AllowOnConsole bool `yaml:"allowOnConsole"`
|
||||
Enabled bool `yaml:"enabled"`
|
||||
FilenameExtension string `yaml:"filenameExtension"`
|
||||
EnableColumnHeaders bool `yaml:"enableColumnHeaders"`
|
||||
AllowOnConsole bool `yaml:"allowOnConsole"`
|
||||
} `yaml:"pcp"`
|
||||
|
||||
ConsoleConfig struct {
|
||||
Enabled bool `yaml:"enabled"`
|
||||
FilenameExtension string `yaml:"filenameExtension"`
|
||||
EnableColumnHeaders bool `yaml:"enableColumnHeaders"`
|
||||
MinLogLevel string `yaml:"minLogLevel"`
|
||||
Enabled bool `yaml:"enabled"`
|
||||
FilenameExtension string `yaml:"filenameExtension"`
|
||||
EnableColumnHeaders bool `yaml:"enableColumnHeaders"`
|
||||
MinLogLevel string `yaml:"minLogLevel"`
|
||||
} `yaml:"console"`
|
||||
|
||||
SPSConfig struct {
|
||||
Enabled bool `yaml:"enabled"`
|
||||
FilenameExtension string `yaml:"filenameExtension"`
|
||||
EnableColumnHeaders bool `yaml:"enableColumnHeaders"`
|
||||
AllowOnConsole bool `yaml:"allowOnConsole"`
|
||||
Enabled bool `yaml:"enabled"`
|
||||
FilenameExtension string `yaml:"filenameExtension"`
|
||||
EnableColumnHeaders bool `yaml:"enableColumnHeaders"`
|
||||
AllowOnConsole bool `yaml:"allowOnConsole"`
|
||||
} `yaml:"sps"`
|
||||
|
||||
TaskDistConfig struct {
|
||||
Enabled bool `yaml:"enabled"`
|
||||
FilenameExtension string `yaml:"filenameExtension"`
|
||||
EnableColumnHeaders bool `yaml:"enableColumnHeaders"`
|
||||
AllowOnConsole bool `yaml:"allowOnConsole"`
|
||||
Enabled bool `yaml:"enabled"`
|
||||
FilenameExtension string `yaml:"filenameExtension"`
|
||||
EnableColumnHeaders bool `yaml:"enableColumnHeaders"`
|
||||
AllowOnConsole bool `yaml:"allowOnConsole"`
|
||||
} `yaml:"clsfnTaskDistOverhead"`
|
||||
|
||||
|
||||
SchedWindowConfig struct {
|
||||
Enabled bool `yaml:"enabled"`
|
||||
FilenameExtension string `yaml:"filenameExtension"`
|
||||
EnableColumnHeaders bool `yaml:"enableColumnHeaders"`
|
||||
AllowOnConsole bool `yaml:"allowOnConsole"`
|
||||
Enabled bool `yaml:"enabled"`
|
||||
FilenameExtension string `yaml:"filenameExtension"`
|
||||
EnableColumnHeaders bool `yaml:"enableColumnHeaders"`
|
||||
AllowOnConsole bool `yaml:"allowOnConsole"`
|
||||
} `yaml:"schedWindow"`
|
||||
|
||||
Format[] string `yaml:"format"`
|
||||
|
||||
Format []string `yaml:"format"`
|
||||
}
|
||||
|
||||
func (c *LoggerConfig) GetConfig() *LoggerConfig {
|
||||
|
||||
yamlFile, err := ioutil.ReadFile(elekEnv.LogConfigYaml)
|
||||
if err != nil {
|
||||
log.Printf("yamlFile.Get err #%v ", err)
|
||||
}
|
||||
err = yaml.Unmarshal(yamlFile, c)
|
||||
if err != nil {
|
||||
log.Fatalf("Unmarshal: %v", err)
|
||||
}
|
||||
yamlFile, err := ioutil.ReadFile(elekEnv.LogConfigYaml)
|
||||
if err != nil {
|
||||
log.Printf("yamlFile.Get err #%v ", err)
|
||||
}
|
||||
err = yaml.Unmarshal(yamlFile, c)
|
||||
if err != nil {
|
||||
log.Fatalf("Unmarshal: %v", err)
|
||||
}
|
||||
|
||||
return c
|
||||
return c
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package elektronLogging
|
||||
|
||||
import (
|
||||
"os"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"os"
|
||||
)
|
||||
|
||||
type PcpLogger struct {
|
||||
|
@ -20,11 +20,11 @@ func (pLog *PcpLogger) Log(logType int, level log.Level, logData log.Fields, mes
|
|||
if pLog.Type == logType {
|
||||
|
||||
logger.SetLevel(level)
|
||||
|
||||
if pLog.AllowOnConsole {
|
||||
logger.SetOutput(os.Stdout)
|
||||
logger.WithFields(logData).Println(message)
|
||||
}
|
||||
|
||||
if pLog.AllowOnConsole {
|
||||
logger.SetOutput(os.Stdout)
|
||||
logger.WithFields(logData).Println(message)
|
||||
}
|
||||
|
||||
logger.SetOutput(pLog.LogFileName)
|
||||
logger.WithFields(logData).Println(message)
|
||||
|
@ -44,6 +44,6 @@ func (plog *PcpLogger) SetLogFile(prefix string) {
|
|||
log.Fatal("Unable to create logFile: ", err)
|
||||
} else {
|
||||
plog.LogFileName = logFile
|
||||
plog.AllowOnConsole = config.PCPConfig.AllowOnConsole
|
||||
plog.AllowOnConsole = config.PCPConfig.AllowOnConsole
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package elektronLogging
|
||||
|
||||
import (
|
||||
"os"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"os"
|
||||
)
|
||||
|
||||
type SchedPolicySwitchLogger struct {
|
||||
|
@ -20,11 +20,11 @@ func (sLog *SchedPolicySwitchLogger) Log(logType int, level log.Level, logData l
|
|||
if sLog.Type == logType {
|
||||
|
||||
logger.SetLevel(level)
|
||||
|
||||
if sLog.AllowOnConsole {
|
||||
logger.SetOutput(os.Stdout)
|
||||
logger.WithFields(logData).Println(message)
|
||||
}
|
||||
|
||||
if sLog.AllowOnConsole {
|
||||
logger.SetOutput(os.Stdout)
|
||||
logger.WithFields(logData).Println(message)
|
||||
}
|
||||
|
||||
logger.SetOutput(sLog.LogFileName)
|
||||
logger.WithFields(logData).Println(message)
|
||||
|
@ -44,6 +44,6 @@ func (sLog *SchedPolicySwitchLogger) SetLogFile(prefix string) {
|
|||
log.Fatal("Unable to create logFile: ", err)
|
||||
} else {
|
||||
sLog.LogFileName = logFile
|
||||
sLog.AllowOnConsole = config.SPSConfig.AllowOnConsole
|
||||
sLog.AllowOnConsole = config.SPSConfig.AllowOnConsole
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package elektronLogging
|
||||
|
||||
import (
|
||||
"os"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"os"
|
||||
)
|
||||
|
||||
type SchedTraceLogger struct {
|
||||
|
@ -20,12 +20,12 @@ func (sLog *SchedTraceLogger) Log(logType int, level log.Level, logData log.Fiel
|
|||
if sLog.Type == logType {
|
||||
|
||||
logger.SetLevel(level)
|
||||
|
||||
if sLog.AllowOnConsole {
|
||||
logger.SetOutput(os.Stdout)
|
||||
logger.WithFields(logData).Println(message)
|
||||
}
|
||||
|
||||
|
||||
if sLog.AllowOnConsole {
|
||||
logger.SetOutput(os.Stdout)
|
||||
logger.WithFields(logData).Println(message)
|
||||
}
|
||||
|
||||
logger.SetOutput(sLog.LogFileName)
|
||||
logger.WithFields(logData).Println(message)
|
||||
}
|
||||
|
@ -44,6 +44,6 @@ func (sLog *SchedTraceLogger) SetLogFile(prefix string) {
|
|||
log.Fatal("Unable to create logFile: ", err)
|
||||
} else {
|
||||
sLog.LogFileName = logFile
|
||||
sLog.AllowOnConsole = config.SchedTraceConfig.AllowOnConsole
|
||||
sLog.AllowOnConsole = config.SchedTraceConfig.AllowOnConsole
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package elektronLogging
|
||||
|
||||
import (
|
||||
"os"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"os"
|
||||
)
|
||||
|
||||
type SchedWindowLogger struct {
|
||||
|
@ -21,10 +21,10 @@ func (sLog *SchedWindowLogger) Log(logType int, level log.Level, logData log.Fie
|
|||
|
||||
logger.SetLevel(level)
|
||||
if sLog.AllowOnConsole {
|
||||
logger.SetOutput(os.Stdout)
|
||||
logger.WithFields(logData).Println(message)
|
||||
}
|
||||
|
||||
logger.SetOutput(os.Stdout)
|
||||
logger.WithFields(logData).Println(message)
|
||||
}
|
||||
|
||||
logger.SetOutput(sLog.LogFileName)
|
||||
logger.WithFields(logData).Println(message)
|
||||
}
|
||||
|
@ -43,6 +43,6 @@ func (sLog *SchedWindowLogger) SetLogFile(prefix string) {
|
|||
log.Fatal("Unable to create logFile: ", err)
|
||||
} else {
|
||||
sLog.LogFileName = logFile
|
||||
sLog.AllowOnConsole = config.SchedWindowConfig.AllowOnConsole
|
||||
sLog.AllowOnConsole = config.SchedWindowConfig.AllowOnConsole
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,16 +1,14 @@
|
|||
package elektronLogging
|
||||
|
||||
|
||||
const (
|
||||
ERROR = iota
|
||||
WARNING = iota
|
||||
GENERAL = iota
|
||||
SUCCESS = iota
|
||||
CONSOLE = iota
|
||||
PCP = iota
|
||||
SCHED_TRACE = iota
|
||||
SPS = iota
|
||||
SCHED_WINDOW = iota
|
||||
CLSFN_TASKDIST_OVERHEAD = iota
|
||||
ERROR = iota
|
||||
WARNING = iota
|
||||
GENERAL = iota
|
||||
SUCCESS = iota
|
||||
CONSOLE = iota
|
||||
PCP = iota
|
||||
SCHED_TRACE = iota
|
||||
SPS = iota
|
||||
SCHED_WINDOW = iota
|
||||
CLSFN_TASKDIST_OVERHEAD = iota
|
||||
)
|
||||
|
||||
|
|
Reference in a new issue