Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add log format/level CLI arg #391

Merged
merged 2 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions cmd/cannon.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/creasty/defaults"
"github.com/ethpandaops/xatu/pkg/cannon"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v3"
)
Expand All @@ -24,21 +23,14 @@ var cannonCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
initCommon()

log.WithField("location", cannonCfgFile).Info("Loading config")

config, err := loadcannonConfigFromFile(cannonCfgFile)
if err != nil {
log.Fatal(err)
}

log.Info("Config loaded")

logLevel, err := logrus.ParseLevel(config.LoggingLevel)
if err != nil {
log.WithField("logLevel", config.LoggingLevel).Fatal("invalid logging level")
}
log = getLogger(config.LoggingLevel, "")

log.SetLevel(logLevel)
log.WithField("location", cannonCfgFile).Info("Loaded config")

cannon, err := cannon.New(cmd.Context(), log, config)
if err != nil {
Expand Down
12 changes: 2 additions & 10 deletions cmd/cl-mimicry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/creasty/defaults"
"github.com/ethpandaops/xatu/pkg/clmimicry"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v3"
)
Expand All @@ -24,21 +23,14 @@ var clMimicryCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
initCommon()

log.WithField("location", clMimicryCfgFile).Info("Loading config")

config, err := loadCLMimicryConfigFromFile(clMimicryCfgFile)
if err != nil {
log.Fatal(err)
}

log.Info("Config loaded")

logLevel, err := logrus.ParseLevel(config.LoggingLevel)
if err != nil {
log.WithField("logLevel", config.LoggingLevel).Fatal("invalid logging level")
}
log = getLogger(config.LoggingLevel, "")

log.SetLevel(logLevel)
log.WithField("location", clMimicryCfgFile).Info("Loaded config")

mimicry, err := clmimicry.New(cmd.Context(), log, config)
if err != nil {
Expand Down
13 changes: 2 additions & 11 deletions cmd/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/creasty/defaults"
"github.com/ethpandaops/xatu/pkg/discovery"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v3"
)
Expand All @@ -24,22 +23,14 @@ var discoveryCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
initCommon()

log.WithField("location", discoveryCfgFile).Info("Loading config")

config, err := loadDiscoveryConfigFromFile(discoveryCfgFile)
if err != nil {
log.Fatal(err)
}

log.Info("Config loaded")

logLevel, err := logrus.ParseLevel(config.LoggingLevel)
if err != nil {
log.WithField("logLevel", config.LoggingLevel).Fatal("invalid logging level")
}

log.SetLevel(logLevel)
log = getLogger(config.LoggingLevel, "")

log.WithField("location", discoveryCfgFile).Info("Loaded config")
discovery, err := discovery.New(cmd.Context(), log, config)
if err != nil {
log.Fatal(err)
Expand Down
12 changes: 2 additions & 10 deletions cmd/mimicry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/creasty/defaults"
"github.com/ethpandaops/xatu/pkg/mimicry"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v3"
)
Expand All @@ -24,21 +23,14 @@ var mimicryCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
initCommon()

log.WithField("location", mimicryCfgFile).Info("Loading config")

config, err := loadMimicryConfigFromFile(mimicryCfgFile)
if err != nil {
log.Fatal(err)
}

log.Info("Config loaded")

logLevel, err := logrus.ParseLevel(config.LoggingLevel)
if err != nil {
log.WithField("logLevel", config.LoggingLevel).Fatal("invalid logging level")
}
log = getLogger(config.LoggingLevel, "")

log.SetLevel(logLevel)
log.WithField("location", mimicryCfgFile).Info("Loaded config")

mimicry, err := mimicry.New(cmd.Context(), log, config)
if err != nil {
Expand Down
12 changes: 2 additions & 10 deletions cmd/relay-monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/creasty/defaults"
"github.com/ethpandaops/xatu/pkg/relaymonitor"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v3"
)
Expand All @@ -24,21 +23,14 @@ var relayMonitorCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
initCommon()

log.WithField("location", relayMonitorCfgFile).Info("Loading config")

config, err := loadRelayMonitorConfigFromFile(relayMonitorCfgFile)
if err != nil {
log.Fatal(err)
}

log.Info("Config loaded")

logLevel, err := logrus.ParseLevel(config.LoggingLevel)
if err != nil {
log.WithField("logLevel", config.LoggingLevel).Fatal("invalid logging level")
}
log = getLogger(config.LoggingLevel, "")

log.SetLevel(logLevel)
log.WithField("location", relayMonitorCfgFile).Info("Loaded config")

monitor, err := relaymonitor.New(cmd.Context(), log, config)
if err != nil {
Expand Down
47 changes: 42 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ package cmd

import (
"os"
"strings"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var (
log = logrus.New()
log = logrus.New()
logLevelFlag string
logFormatFlag string

defaultLogLevel = "info"
defaultLogFormat = "text"
)

// rootCmd represents the base command when called without any subcommands
Expand All @@ -21,23 +27,54 @@ var rootCmd = &cobra.Command{
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
initCommon()

err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}

func init() {
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
rootCmd.PersistentFlags().StringVar(&logLevelFlag, "log-level", "", "Log level (debug, info, warn, error, fatal, panic)")
rootCmd.PersistentFlags().StringVar(&logFormatFlag, "log-format", "", "Log format (text, json)")
}

func initCommon() {
log.SetFormatter(&logrus.TextFormatter{})
}

logLevel, err := logrus.ParseLevel(logrus.InfoLevel.String())
func getLogger(configLevel, configFormat string) *logrus.Logger {
// Prefer the cli args over whatever has been provided in the config file
finalLevel := defaultLogLevel
if logLevelFlag != "" {
finalLevel = logLevelFlag
} else if configLevel != "" {
finalLevel = configLevel
}

logLevel, err := logrus.ParseLevel(strings.ToLower(finalLevel))
if err != nil {
log.Fatal("invalid logging level")
log.Fatalf("invalid logging level: %v", err)
}

log.SetLevel(logLevel)

finalFormat := defaultLogFormat
if logFormatFlag != "" {
finalFormat = logFormatFlag
} else if configFormat != "" {
finalFormat = configFormat
}

if finalFormat == "json" {
log.SetFormatter(&logrus.JSONFormatter{})
} else {
log.SetFormatter(&logrus.TextFormatter{})
}

log.WithFields(logrus.Fields{
"level": log.GetLevel(),
}).Info("Logger initialized")

return log
}
12 changes: 2 additions & 10 deletions cmd/sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/creasty/defaults"
"github.com/ethpandaops/xatu/pkg/sentry"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -121,8 +120,6 @@ var sentryCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
initCommon()

log.WithField("location", sentryCfgFile).Info("Loading config")

overrides := &sentry.Override{}
for _, o := range SentryOverrides {
if errr := o.Setter(cmd, overrides); errr != nil {
Expand All @@ -140,6 +137,8 @@ var sentryCmd = &cobra.Command{
log.Fatal(err)
}

log = getLogger(config.LoggingLevel, "")

// If we have a valid preset configuration, use it to override the config
if cmd.Flags().Changed(presetFlag) {
log.Info("Overriding sentry configuration with preset")
Expand All @@ -149,13 +148,6 @@ var sentryCmd = &cobra.Command{

log.Info("Config loaded")

logLevel, err := logrus.ParseLevel(config.LoggingLevel)
if err != nil {
log.WithField("logLevel", config.LoggingLevel).Fatal("invalid logging level")
}

log.SetLevel(logLevel)

sentry, err := sentry.New(cmd.Context(), log, config, overrides)
if err != nil {
log.Fatal(err)
Expand Down
12 changes: 2 additions & 10 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/creasty/defaults"
"github.com/ethpandaops/xatu/pkg/server"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v3"
)
Expand All @@ -23,21 +22,14 @@ var serverCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
initCommon()

log.WithField("location", serverCfgFile).Info("Loading config")

config, err := loadServerConfigFromFile(serverCfgFile)
if err != nil {
log.Fatal(err)
}

log.Info("Config loaded")

logLevel, err := logrus.ParseLevel(config.LoggingLevel)
if err != nil {
log.WithField("logLevel", config.LoggingLevel).Fatal("invalid logging level")
}
log = getLogger(config.LoggingLevel, "")

log.SetLevel(logLevel)
log.WithField("location", serverCfgFile).Info("Loaded config")

server, err := server.NewXatu(cmd.Context(), log, config)
if err != nil {
Expand Down
Loading