diff --git a/cmd/harvester/cli/config.go b/cmd/harvester/cli/config.go index e7c77674..21075d5e 100644 --- a/cmd/harvester/cli/config.go +++ b/cmd/harvester/cli/config.go @@ -26,6 +26,7 @@ type harvesterConfig struct { SpireSocketPath string `hcl:"spire_socket_path"` ServerAddress string `hcl:"server_address"` BundleUpdatesInterval string `hcl:"bundle_updates_interval"` + LogLevel string `hcl:"log_level"` } // ParseConfig reads a configuration from the Reader and parses it @@ -90,4 +91,8 @@ func (c *Config) setDefaults() { if c.Harvester.BundleUpdatesInterval == "" { c.Harvester.BundleUpdatesInterval = defaultBundleUpdatesInterval } + + if c.Harvester.LogLevel == "" { + c.Harvester.LogLevel = "INFO" + } } diff --git a/cmd/harvester/cli/run.go b/cmd/harvester/cli/run.go index 90c704a7..0ab7b193 100644 --- a/cmd/harvester/cli/run.go +++ b/cmd/harvester/cli/run.go @@ -8,6 +8,7 @@ import ( "syscall" "github.com/HewlettPackard/galadriel/pkg/harvester" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -71,6 +72,13 @@ func LoadConfig(cmd *cobra.Command) (*harvester.Config, error) { return nil, fmt.Errorf("failed to build harvester configuration: %w", err) } + logLevel, err := logrus.ParseLevel(c.Harvester.LogLevel) + if err != nil { + return nil, err + } + + logrus.SetLevel(logLevel) + hc.AccessToken = token return hc, nil diff --git a/cmd/server/cli/config.go b/cmd/server/cli/config.go index ef61e3bd..ea4729f0 100644 --- a/cmd/server/cli/config.go +++ b/cmd/server/cli/config.go @@ -25,6 +25,7 @@ type serverConfig struct { ListenAddress string `hcl:"listen_address"` ListenPort int `hcl:"listen_port"` SocketPath string `hcl:"socket_path"` + LogLevel string `hcl:"log_level"` } // ParseConfig reads a configuration from the Reader and parses it @@ -94,4 +95,8 @@ func (c *Config) setDefaults() { if c.Server.SocketPath == "" { c.Server.SocketPath = defaultSocketPath } + + if c.Server.LogLevel == "" { + c.Server.LogLevel = "INFO" + } } diff --git a/cmd/server/cli/config_test.go b/cmd/server/cli/config_test.go index 43f36c86..17513601 100644 --- a/cmd/server/cli/config_test.go +++ b/cmd/server/cli/config_test.go @@ -4,8 +4,10 @@ import ( "bytes" "errors" "io" + "strings" "testing" + "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" ) @@ -20,6 +22,7 @@ func TestNewServerConfig(t *testing.T) { ListenAddress: "localhost", ListenPort: 8000, SocketPath: "/example", + LogLevel: "INFO", }} sc, err := NewServerConfig(&config) @@ -30,6 +33,7 @@ func TestNewServerConfig(t *testing.T) { assert.Equal(t, "127.0.0.1", sc.TCPAddress.IP.String()) assert.Equal(t, config.Server.ListenPort, sc.TCPAddress.Port) assert.Equal(t, config.Server.SocketPath, sc.LocalAddress.String()) + assert.Equal(t, strings.ToLower(config.Server.LogLevel), logrus.GetLevel().String()) } func TestNew(t *testing.T) { @@ -42,12 +46,13 @@ func TestNew(t *testing.T) { { name: "ok", config: bytes.NewBuffer([]byte( - `server { listen_address = "127.0.0.1" listen_port = 2222 socket_path = "/tmp/api.sock" log_level = "DEBUG"}`)), + `server { listen_address = "127.0.0.1" listen_port = 2222 socket_path = "/tmp/api.sock" log_level = "INFO"}`)), expected: &Config{ Server: &serverConfig{ ListenAddress: "127.0.0.1", ListenPort: 2222, SocketPath: "/tmp/api.sock", + LogLevel: "INFO", }, }, }, @@ -59,6 +64,7 @@ func TestNew(t *testing.T) { ListenAddress: "0.0.0.0", ListenPort: 8085, SocketPath: defaultSocketPath, + LogLevel: "INFO", }, }, }, diff --git a/cmd/server/cli/run.go b/cmd/server/cli/run.go index ea1286c1..91bcab03 100644 --- a/cmd/server/cli/run.go +++ b/cmd/server/cli/run.go @@ -8,6 +8,7 @@ import ( "syscall" "github.com/HewlettPackard/galadriel/pkg/server" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -67,6 +68,12 @@ func LoadConfig(cmd *cobra.Command) (*server.Config, error) { return nil, fmt.Errorf("failed to build server configuration: %w", err) } + logLevel, err := logrus.ParseLevel(c.Server.LogLevel) + if err != nil { + return nil, err + } + logrus.SetLevel(logLevel) + return sc, nil } diff --git a/conf/harvester/harvester.conf b/conf/harvester/harvester.conf index 4cb19930..f5bf9f77 100644 --- a/conf/harvester/harvester.conf +++ b/conf/harvester/harvester.conf @@ -13,4 +13,8 @@ harvester { # Default: 30s # E.g: 12h, 5m, 90000ms bundle_updates_interval = "5s" + + # log_level: Application log level. One of: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, PANIC + # Default: INFO + log_level = "INFO" } diff --git a/conf/server/server.conf b/conf/server/server.conf index 3c9417f9..6659b25e 100644 --- a/conf/server/server.conf +++ b/conf/server/server.conf @@ -9,4 +9,8 @@ server { # socket_path: Path to bind the Galadriel Server API socket to. # Default: /tmp/galadriel-server/api.sock. socket_path = "/tmp/galadriel-server/api.sock" -} \ No newline at end of file + + # log_level: Application log level. One of: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, PANIC + # Default: INFO + log_level = "INFO" +}