summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Thomas <nick@gitlab.com>2021-09-23 10:41:12 +0100
committerNick Thomas <nick@gitlab.com>2021-09-23 10:44:31 +0100
commit4f64306d5118606a2ff464e1994ce8683b52fd15 (patch)
tree24b741801917ebdaadb9cb05382c37d5d9ae08b2
parent882c55eabf74eb5996098c9898045099927803a1 (diff)
downloadgitlab-shell-4f64306d5118606a2ff464e1994ce8683b52fd15.tar.gz
Respect log-level configuration again
This was lost in the move from Ruby to Go. Restore it now. Changelog: fixed
-rw-r--r--internal/config/config.go2
-rw-r--r--internal/logger/logger.go1
-rw-r--r--internal/logger/logger_test.go28
3 files changed, 29 insertions, 2 deletions
diff --git a/internal/config/config.go b/internal/config/config.go
index c67f60a..5185736 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -46,6 +46,7 @@ type Config struct {
RootDir string
LogFile string `yaml:"log_file,omitempty"`
LogFormat string `yaml:"log_format,omitempty"`
+ LogLevel string `yaml:"log_level,omitempty"`
GitlabUrl string `yaml:"gitlab_url"`
GitlabRelativeURLRoot string `yaml:"gitlab_relative_url_root"`
GitlabTracing string `yaml:"gitlab_tracing"`
@@ -66,6 +67,7 @@ var (
DefaultConfig = Config{
LogFile: "gitlab-shell.log",
LogFormat: "json",
+ LogLevel: "info",
Server: DefaultServerConfig,
User: "git",
}
diff --git a/internal/logger/logger.go b/internal/logger/logger.go
index 1165680..748fce0 100644
--- a/internal/logger/logger.go
+++ b/internal/logger/logger.go
@@ -35,6 +35,7 @@ func buildOpts(cfg *config.Config) []log.LoggerOption {
log.WithFormatter(logFmt(cfg.LogFormat)),
log.WithOutputName(logFile(cfg.LogFile)),
log.WithTimezone(time.UTC),
+ log.WithLogLevel(cfg.LogLevel),
}
}
diff --git a/internal/logger/logger_test.go b/internal/logger/logger_test.go
index bda36d9..4ea8c1f 100644
--- a/internal/logger/logger_test.go
+++ b/internal/logger/logger_test.go
@@ -3,7 +3,6 @@ package logger
import (
"os"
"regexp"
- "strings"
"testing"
"github.com/stretchr/testify/require"
@@ -26,12 +25,37 @@ func TestConfigure(t *testing.T) {
defer closer.Close()
log.Info("this is a test")
+ log.WithFields(log.Fields{}).Debug("debug log message")
tmpFile.Close()
data, err := os.ReadFile(tmpFile.Name())
require.NoError(t, err)
- require.True(t, strings.Contains(string(data), `msg":"this is a test"`))
+ require.Contains(t, string(data), `msg":"this is a test"`)
+ require.NotContains(t, string(data), `msg:":"debug log message"`)
+}
+
+func TestConfigureWithDebugLogLevel(t *testing.T) {
+ tmpFile, err := os.CreateTemp(os.TempDir(), "logtest-")
+ require.NoError(t, err)
+ defer tmpFile.Close()
+
+ config := config.Config{
+ LogFile: tmpFile.Name(),
+ LogFormat: "json",
+ LogLevel: "debug",
+ }
+
+ closer := Configure(&config)
+ defer closer.Close()
+
+ log.WithFields(log.Fields{}).Debug("debug log message")
+
+ tmpFile.Close()
+
+ data, err := os.ReadFile(tmpFile.Name())
+ require.NoError(t, err)
+ require.Contains(t, string(data), `msg":"debug log message"`)
}
func TestConfigureWithPermissionError(t *testing.T) {