Skip to content

Commit

Permalink
rework: log level moved to custom set value
Browse files Browse the repository at this point in the history
serve
  • Loading branch information
daniel-burghardt committed May 27, 2024
1 parent 72a0dbb commit 30ec11a
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 2 deletions.
9 changes: 9 additions & 0 deletions cmd/ingest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
2 changes: 0 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/stellar/go/support/log"
)
Expand Down Expand Up @@ -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())
Expand Down
9 changes: 9 additions & 0 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ func (c *serveCmd) Command() *cobra.Command {
FlagDefault: "http://localhost:8000",
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: "wallet-signing-key",
Usage: "The public key of the Stellar account that signs the payloads when making HTTP Request to this server.",
Expand Down
26 changes: 26 additions & 0 deletions cmd/utils/custom_set_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,42 @@ 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"
)

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)

Expand Down
52 changes: 52 additions & 0 deletions cmd/utils/custom_set_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 }{}

Expand Down
2 changes: 2 additions & 0 deletions internal/ingest/ingest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -26,6 +27,7 @@ type Configs struct {
LedgerCursorName string
StartLedger int
EndLedger int
LogLevel logrus.Level
}

func Ingest(cfg Configs) error {
Expand Down
2 changes: 2 additions & 0 deletions internal/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -21,6 +22,7 @@ type Configs struct {
DatabaseURL string
ServerBaseURL string
WalletSigningKey string
LogLevel logrus.Level
}

type handlerDeps struct {
Expand Down

0 comments on commit 30ec11a

Please sign in to comment.