summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGary Holtz <gholtz@gitlab.com>2021-06-18 13:57:09 -0500
committerGary Holtz <gholtz@gitlab.com>2021-06-18 13:57:09 -0500
commit7ae7047deb7f8f0d6f75bb4698b7f3e2d00b3224 (patch)
treee2959470ea5990c2af0195bc888917f11e137d83
parenta9c25c17ea0a511ec6554a4dd3bcd9f5012d59d5 (diff)
downloadgitlab-shell-7ae7047deb7f8f0d6f75bb4698b7f3e2d00b3224.tar.gz
Adding a UTC converter and test
-rw-r--r--internal/logger/logger.go14
-rw-r--r--internal/logger/logger_test.go25
2 files changed, 38 insertions, 1 deletions
diff --git a/internal/logger/logger.go b/internal/logger/logger.go
index 651aa08..4e735e7 100644
--- a/internal/logger/logger.go
+++ b/internal/logger/logger.go
@@ -10,9 +10,21 @@ import (
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
)
+type UTCFormatter struct {
+ log.Formatter
+}
+
+func (u UTCFormatter) Format(e *log.Entry) ([]byte, error) {
+ e.Time = e.Time.UTC()
+
+ return u.Formatter.Format(e)
+}
+
func configureLogFormat(cfg *config.Config) {
if cfg.LogFormat == "json" {
- log.SetFormatter(&log.JSONFormatter{})
+ log.SetFormatter(UTCFormatter{&log.JSONFormatter{}})
+ } else {
+ log.SetFormatter(UTCFormatter{&log.TextFormatter{}})
}
}
diff --git a/internal/logger/logger_test.go b/internal/logger/logger_test.go
index 9bffad2..6360ad9 100644
--- a/internal/logger/logger_test.go
+++ b/internal/logger/logger_test.go
@@ -3,6 +3,7 @@ package logger
import (
"io/ioutil"
"os"
+ "regexp"
"strings"
"testing"
@@ -44,3 +45,27 @@ func TestConfigureWithPermissionError(t *testing.T) {
Configure(&config)
log.Info("this is a test")
}
+
+func TestLogInUTC(t *testing.T) {
+ tmpFile, err := ioutil.TempFile(os.TempDir(), "logtest-")
+ require.NoError(t, err)
+ defer tmpFile.Close()
+ defer os.Remove(tmpFile.Name())
+
+ config := config.Config{
+ LogFile: tmpFile.Name(),
+ LogFormat: "json",
+ }
+
+ Configure(&config)
+ log.Info("this is a test")
+
+ data, err := ioutil.ReadFile(tmpFile.Name())
+ require.NoError(t, err)
+
+ utc := `[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z`
+ r, e := regexp.MatchString(utc, string(data))
+
+ require.NoError(t, e)
+ require.True(t, r)
+}