Skip to content

Commit

Permalink
adds support for log level (#66)
Browse files Browse the repository at this point in the history
Signed-off-by: Juliano Fantozzi <[email protected]>
  • Loading branch information
jufantozzi authored Oct 26, 2022
1 parent 94cc209 commit 876952c
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 2 deletions.
5 changes: 5 additions & 0 deletions cmd/harvester/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -90,4 +91,8 @@ func (c *Config) setDefaults() {
if c.Harvester.BundleUpdatesInterval == "" {
c.Harvester.BundleUpdatesInterval = defaultBundleUpdatesInterval
}

if c.Harvester.LogLevel == "" {
c.Harvester.LogLevel = "INFO"
}
}
8 changes: 8 additions & 0 deletions cmd/harvester/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"syscall"

"github.com/HewlettPackard/galadriel/pkg/harvester"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions cmd/server/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -94,4 +95,8 @@ func (c *Config) setDefaults() {
if c.Server.SocketPath == "" {
c.Server.SocketPath = defaultSocketPath
}

if c.Server.LogLevel == "" {
c.Server.LogLevel = "INFO"
}
}
8 changes: 7 additions & 1 deletion cmd/server/cli/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"bytes"
"errors"
"io"
"strings"
"testing"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)

Expand All @@ -20,6 +22,7 @@ func TestNewServerConfig(t *testing.T) {
ListenAddress: "localhost",
ListenPort: 8000,
SocketPath: "/example",
LogLevel: "INFO",
}}

sc, err := NewServerConfig(&config)
Expand All @@ -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) {
Expand All @@ -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",
},
},
},
Expand All @@ -59,6 +64,7 @@ func TestNew(t *testing.T) {
ListenAddress: "0.0.0.0",
ListenPort: 8085,
SocketPath: defaultSocketPath,
LogLevel: "INFO",
},
},
},
Expand Down
7 changes: 7 additions & 0 deletions cmd/server/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"syscall"

"github.com/HewlettPackard/galadriel/pkg/server"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 4 additions & 0 deletions conf/harvester/harvester.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
6 changes: 5 additions & 1 deletion conf/server/server.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

# log_level: Application log level. One of: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, PANIC
# Default: INFO
log_level = "INFO"
}

0 comments on commit 876952c

Please sign in to comment.