From b69dcb62e329b20dd96511a95780e12054148208 Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Sun, 22 Aug 2021 18:57:10 -0700 Subject: [PATCH] `LOG_LEVEL` variable (#577) --- Dockerfile | 1 + cmd/gluetun/main.go | 1 + internal/configuration/log.go | 30 +++++++++++++++++++++++++ internal/configuration/settings.go | 7 ++++++ internal/configuration/settings_test.go | 2 ++ 5 files changed, 41 insertions(+) create mode 100644 internal/configuration/log.go diff --git a/Dockerfile b/Dockerfile index c266a7a0..5a6c1ba0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -68,6 +68,7 @@ LABEL \ org.opencontainers.image.description="VPN swiss-knife like client to tunnel to multiple VPN servers using OpenVPN, IPtables, DNS over TLS, Shadowsocks, an HTTP proxy and Alpine Linux" ENV VPNSP=pia \ VERSION_INFORMATION=on \ + LOG_LEVEL=info \ VPN_TYPE=openvpn \ PROTOCOL=udp \ OPENVPN_VERSION=2.5 \ diff --git a/cmd/gluetun/main.go b/cmd/gluetun/main.go index e05382ee..1bb1d9d8 100644 --- a/cmd/gluetun/main.go +++ b/cmd/gluetun/main.go @@ -141,6 +141,7 @@ func _main(ctx context.Context, buildInfo models.BuildInformation, if err != nil { return err } + logger.PatchLevel(allSettings.Log.Level) puid, pgid := allSettings.System.PUID, allSettings.System.PGID diff --git a/internal/configuration/log.go b/internal/configuration/log.go new file mode 100644 index 00000000..143c3b31 --- /dev/null +++ b/internal/configuration/log.go @@ -0,0 +1,30 @@ +package configuration + +import ( + "fmt" + + "github.com/qdm12/golibs/logging" + "github.com/qdm12/golibs/params" +) + +type Log struct { + Level logging.Level `json:"level"` +} + +func (settings *Log) lines() (lines []string) { + lines = append(lines, lastIndent+"Log:") + + lines = append(lines, indent+lastIndent+"Level: "+settings.Level.String()) + + return lines +} + +func (settings *Log) read(env params.Interface) (err error) { + defaultLevel := logging.LevelInfo.String() + settings.Level, err = env.LogLevel("LOG_LEVEL", params.Default(defaultLevel)) + if err != nil { + return fmt.Errorf("environment variable LOG_LEVEL: %w", err) + } + + return nil +} diff --git a/internal/configuration/settings.go b/internal/configuration/settings.go index 2674a8cf..33e9e071 100644 --- a/internal/configuration/settings.go +++ b/internal/configuration/settings.go @@ -22,6 +22,7 @@ type Settings struct { VersionInformation bool ControlServer ControlServer Health Health + Log Log } func (settings *Settings) String() string { @@ -33,6 +34,7 @@ func (settings *Settings) lines() (lines []string) { lines = append(lines, settings.VPN.lines()...) lines = append(lines, settings.DNS.lines()...) lines = append(lines, settings.Firewall.lines()...) + lines = append(lines, settings.Log.lines()...) lines = append(lines, settings.System.lines()...) lines = append(lines, settings.HTTPProxy.lines()...) lines = append(lines, settings.ShadowSocks.lines()...) @@ -57,6 +59,7 @@ var ( ErrUpdater = errors.New("cannot read Updater settings") ErrPublicIP = errors.New("cannot read Public IP getter settings") ErrHealth = errors.New("cannot read health settings") + ErrLog = errors.New("cannot read log settings") ) // Read obtains all configuration options for the program and returns an error as soon @@ -113,5 +116,9 @@ func (settings *Settings) Read(env params.Interface, logger logging.Logger) (err return fmt.Errorf("%w: %s", ErrHealth, err) } + if err := settings.Log.read(r.env); err != nil { + return fmt.Errorf("%w: %s", ErrLog, err) + } + return nil } diff --git a/internal/configuration/settings_test.go b/internal/configuration/settings_test.go index eb51f943..4f43d6e7 100644 --- a/internal/configuration/settings_test.go +++ b/internal/configuration/settings_test.go @@ -43,6 +43,8 @@ func Test_Settings_lines(t *testing.T) { " |--Protocol: udp", "|--DNS:", "|--Firewall: disabled ⚠️", + "|--Log:", + " |--Level: DEBUG", "|--System:", " |--Process user ID: 0", " |--Process group ID: 0",