diff --git a/cmd/ingest.go b/cmd/ingest.go index 6dc95b4..7d53fc8 100644 --- a/cmd/ingest.go +++ b/cmd/ingest.go @@ -33,6 +33,15 @@ func (c *ingestCmd) Command() *cobra.Command { FlagDefault: network.TestNetworkPassphrase, Required: true, }, + { + Name: "log-level", + Usage: `The log level used in this project. Options: "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL", or "PANIC".`, + OptType: types.String, + FlagDefault: "TRACE", + ConfigKey: &cfg.LogLevel, + CustomSetValue: utils.SetConfigOptionLogLevel, + Required: false, + }, { Name: "captive-core-bin-path", Usage: "Path to Captive Core's binary file.", diff --git a/cmd/root.go b/cmd/root.go index 3fc1ade..1e3967d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,7 +1,6 @@ package cmd import ( - "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/stellar/go/support/log" ) @@ -29,7 +28,6 @@ func Execute() { func init() { log.DefaultLogger = log.New() - log.DefaultLogger.SetLevel(logrus.TraceLevel) rootCmd.AddCommand((&serveCmd{}).Command()) rootCmd.AddCommand((&ingestCmd{}).Command()) diff --git a/cmd/utils/custom_set_value.go b/cmd/utils/custom_set_value.go index 37a241f..c702dff 100644 --- a/cmd/utils/custom_set_value.go +++ b/cmd/utils/custom_set_value.go @@ -6,9 +6,11 @@ import ( "os" "path" + "github.com/sirupsen/logrus" "github.com/spf13/viper" "github.com/stellar/go/keypair" "github.com/stellar/go/support/config" + "github.com/stellar/go/support/log" "github.com/stellar/wallet-backend/internal/ingest" ) @@ -16,6 +18,30 @@ func unexpectedTypeError(key any, co *config.ConfigOption) error { return fmt.Errorf("the expected type for the config key in %s is %T, but a %T was provided instead", co.Name, key, co.ConfigKey) } +func SetConfigOptionLogLevel(co *config.ConfigOption) error { + logLevelStr := viper.GetString(co.Name) + logLevel, err := logrus.ParseLevel(logLevelStr) + if err != nil { + return fmt.Errorf("couldn't parse log level in %s: %w", co.Name, err) + } + + key, ok := co.ConfigKey.(*logrus.Level) + if !ok { + return fmt.Errorf("%s configKey has an invalid type %T", co.Name, co.ConfigKey) + } + *key = logLevel + + // Log for debugging + if config.IsExplicitlySet(co) { + log.Debugf("Setting log level to: %s", logLevel) + log.DefaultLogger.SetLevel(*key) + } else { + log.Debugf("Using default log level: %s", logLevel) + } + + return nil +} + func SetConfigOptionStellarPublicKey(co *config.ConfigOption) error { publicKey := viper.GetString(co.Name) diff --git a/cmd/utils/custom_set_value_test.go b/cmd/utils/custom_set_value_test.go index e130238..0d15ecc 100644 --- a/cmd/utils/custom_set_value_test.go +++ b/cmd/utils/custom_set_value_test.go @@ -6,6 +6,7 @@ import ( "strings" "testing" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/stellar/go/support/config" "github.com/stellar/wallet-backend/internal/utils" @@ -125,6 +126,57 @@ func TestSetConfigOptionStellarPublicKey(t *testing.T) { } } +func Test_SetConfigOptionLogLevel(t *testing.T) { + opts := struct{ logrusLevel logrus.Level }{} + + co := config.ConfigOption{ + Name: "log-level", + OptType: types.String, + CustomSetValue: SetConfigOptionLogLevel, + ConfigKey: &opts.logrusLevel, + } + + testCases := []customSetterTestCase[logrus.Level]{ + { + name: "returns an error if the log level is empty", + args: []string{}, + wantErrContains: `couldn't parse log level in log-level: not a valid logrus Level: ""`, + }, + { + name: "returns an error if the log level is invalid", + args: []string{"--log-level", "test"}, + wantErrContains: `couldn't parse log level in log-level: not a valid logrus Level: "test"`, + }, + { + name: "handles messenger type TRACE (through CLI args)", + args: []string{"--log-level", "TRACE"}, + wantResult: logrus.TraceLevel, + }, + { + name: "handles messenger type TRACE (through ENV vars)", + envValue: "TRACE", + wantResult: logrus.TraceLevel, + }, + { + name: "handles messenger type INFO (through CLI args)", + args: []string{"--log-level", "iNfO"}, + wantResult: logrus.InfoLevel, + }, + { + name: "handles messenger type INFO (through ENV vars)", + envValue: "INFO", + wantResult: logrus.InfoLevel, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + opts.logrusLevel = 0 + customSetterTester[logrus.Level](t, tc, co) + }) + } +} + func TestSetConfigOptionCaptiveCoreBinPath(t *testing.T) { opts := struct{ binPath string }{} diff --git a/internal/ingest/ingest.go b/internal/ingest/ingest.go index 8bdd5fe..43910b2 100644 --- a/internal/ingest/ingest.go +++ b/internal/ingest/ingest.go @@ -5,6 +5,7 @@ import ( "fmt" "path" + "github.com/sirupsen/logrus" "github.com/stellar/go/ingest/ledgerbackend" "github.com/stellar/go/network" "github.com/stellar/go/support/log" @@ -26,6 +27,7 @@ type Configs struct { LedgerCursorName string StartLedger int EndLedger int + LogLevel logrus.Level } func Ingest(cfg Configs) error { diff --git a/internal/serve/serve.go b/internal/serve/serve.go index 1ade460..a2d2326 100644 --- a/internal/serve/serve.go +++ b/internal/serve/serve.go @@ -5,6 +5,7 @@ import ( "net/http" "github.com/go-chi/chi" + "github.com/sirupsen/logrus" supporthttp "github.com/stellar/go/support/http" "github.com/stellar/go/support/log" "github.com/stellar/go/support/render/health" @@ -21,6 +22,7 @@ type Configs struct { DatabaseURL string ServerBaseURL string WalletSigningKey string + LogLevel logrus.Level } type handlerDeps struct {