Skip to content

Commit

Permalink
added logging filters for IL
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Tichák authored and teo committed Jul 18, 2024
1 parent ba1f643 commit 03a5022
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 25 deletions.
9 changes: 5 additions & 4 deletions cmd/o2-aliecs-core/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,18 @@ func init() {
ForceFormatting: true,
})
log.SetOutput(os.Stdout)
ilHook, err := infologger.NewDirectHook("ECS", "core")
if err == nil {
log.AddHook(ilHook)
}
}

func main() {
if err := core.NewConfig(); err != nil {
log.Fatal(err)
}

ilHook, err := infologger.NewDirectHook("ECS", "core", nil)
if err == nil {
log.AddHook(ilHook)
}

if err := core.Run(); err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/o2-aliecs-executor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (

func init() {
logrus.SetOutput(os.Stdout)
ilHook, err := infologger.NewDirectHook("ECS", "executor")
ilHook, err := infologger.NewDirectHook("ECS", "executor", logrus.AllLevels)
if err == nil {
logrus.AddHook(ilHook)
}
Expand Down
15 changes: 8 additions & 7 deletions cmd/o2-apricot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,27 @@ import (

func init() {
log.SetFormatter(&prefixed.TextFormatter{
FullTimestamp: true,
SpacePadding: 20,
PrefixPadding: 12,
FullTimestamp: true,
SpacePadding: 20,
PrefixPadding: 12,

// Needed for colored stdout/stderr in GoLand, IntelliJ, etc.
ForceColors: true,
ForceFormatting: true,
})
log.SetOutput(os.Stdout)
ilHook, err := infologger.NewDirectHook("ECS", "apricot")
if err == nil {
log.AddHook(ilHook)
}
}

func main() {
if err := apricot.NewConfig(); err != nil {
log.Fatal(err)
}

ilHook, err := infologger.NewDirectHook("ECS", "apricot", nil)
if err == nil {
log.AddHook(ilHook)
}

if err := apricot.Run(); err != nil {
log.Fatal(err)
}
Expand Down
43 changes: 30 additions & 13 deletions common/logger/infologger/directhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (

"github.com/AliceO2Group/Control/common/utils"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)

const INFOLOGGER_MAX_MESSAGE_SIZE = 1024
Expand All @@ -46,8 +47,26 @@ var (
hostname string
Pid string
username string

logILInfoLevel = []logrus.Level{
logrus.PanicLevel,
logrus.FatalLevel,
logrus.ErrorLevel,
logrus.WarnLevel,
logrus.InfoLevel,
}
logILAllLevel = logrus.AllLevels
currentIlLevel = logILInfoLevel
)

func setCurrentILLevelFromViper() {
if viper.GetBool("logAllIL") {
currentIlLevel = logILAllLevel
} else {
currentIlLevel = logILInfoLevel
}
}

var lineBreaksRe = regexp.MustCompile(`\r?\n`)

func init() {
Expand Down Expand Up @@ -140,7 +159,14 @@ func guessSocketPath() string {
}
}

func NewDirectHook(defaultSystem string, defaultFacility string) (*DirectHook, error) {
func NewDirectHook(defaultSystem string, defaultFacility string, levelsToLog []logrus.Level) (*DirectHook, error) {

if levelsToLog == nil {
setCurrentILLevelFromViper()
} else {
currentIlLevel = levelsToLog
}

socketPath := guessSocketPath()
sender := newSender(socketPath)
if sender == nil {
Expand All @@ -154,25 +180,16 @@ func NewDirectHook(defaultSystem string, defaultFacility string) (*DirectHook, e
}, nil
}

func NewDirectHookWithRole(defaultSystem string, defaultFacility string, defaultRole string) (*DirectHook, error) {
dh, err := NewDirectHook(defaultSystem, defaultFacility)
func NewDirectHookWithRole(defaultSystem string, defaultFacility string, defaultRole string, levelsToLog []logrus.Level) (*DirectHook, error) {
dh, err := NewDirectHook(defaultSystem, defaultFacility, levelsToLog)
if dh != nil {
dh.role = defaultRole
}
return dh, err
}

func (h *DirectHook) Levels() []logrus.Level {
// Everything except Trace
return []logrus.Level{
logrus.PanicLevel,
logrus.FatalLevel,
logrus.ErrorLevel,
logrus.WarnLevel,
logrus.InfoLevel,
logrus.DebugLevel,
logrus.TraceLevel,
}
return currentIlLevel
}

func (h *DirectHook) Fire(e *logrus.Entry) error {
Expand Down
2 changes: 2 additions & 0 deletions core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func setDefaults() error {
viper.SetDefault("taskClassCacheTTL", 7*24*time.Hour)
viper.SetDefault("kafkaEndpoints", []string{"localhost:9092"})
viper.SetDefault("enableKafka", true)
viper.SetDefault("logAllIL", false)
return nil
}

Expand Down Expand Up @@ -190,6 +191,7 @@ func setFlags() error {
pflag.Duration("taskClassCacheTTL", viper.GetDuration("taskClassCacheTTL"), "TTL for task class cache entries")
pflag.StringSlice("kafkaEndpoints", viper.GetStringSlice("kafkaEndpoints"), "List of Kafka endpoints to connect to (default: localhost:9092)")
pflag.Bool("enableKafka", viper.GetBool("enableKafka"), "Turn on the kafka messaging")
pflag.Bool("logAllIL", viper.GetBool("logAllIL"), "Send all the logs into IL, including Debug and Trace messages")

pflag.Parse()
return viper.BindPFlags(pflag.CommandLine)
Expand Down

0 comments on commit 03a5022

Please sign in to comment.