summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAakriti Gupta <agupta@gitlab.com>2020-02-05 15:43:57 +0100
committerAsh McKenzie <amckenzie@gitlab.com>2020-02-11 19:31:17 +1100
commit354ae8d88d7ef447baf3d4fd6f5cba211df7efd1 (patch)
tree29dcd0065a41edfeea5c668d8bbdee39a74aa79e
parenta7b6307397c0bf5414a102052042d405c3b40162 (diff)
downloadgitlab-shell-ag-update-timestamps.tar.gz
Format timestamp in UTC with a precision of a milisecondag-update-timestamps
-rw-r--r--internal/logger/logger.go23
-rw-r--r--internal/logger/logger_test.go27
2 files changed, 49 insertions, 1 deletions
diff --git a/internal/logger/logger.go b/internal/logger/logger.go
index f1db4b0..4115641 100644
--- a/internal/logger/logger.go
+++ b/internal/logger/logger.go
@@ -21,6 +21,15 @@ var (
ProgName string
)
+type UTCFormatter struct {
+ defaultFormatter log.Formatter
+}
+
+func (f *UTCFormatter) Format(entry *log.Entry) ([]byte, error) {
+ entry.Time = entry.Time.UTC()
+ return f.defaultFormatter.Format(entry)
+}
+
func Configure(cfg *config.Config) error {
mutex.Lock()
defer mutex.Unlock()
@@ -34,8 +43,20 @@ func Configure(cfg *config.Config) error {
}
log.SetOutput(logWriter)
+
if cfg.LogFormat == "json" {
- log.SetFormatter(&log.JSONFormatter{})
+ log.SetFormatter(&UTCFormatter{
+ defaultFormatter: &log.JSONFormatter{
+ TimestampFormat: "2020-02-05 15:04:05.999Z",
+ },
+ })
+ } else {
+ log.SetFormatter(&UTCFormatter{
+ defaultFormatter: &log.TextFormatter{
+ TimestampFormat: "2020-02-05 15:04:05.999Z",
+ FullTimestamp: true,
+ },
+ })
}
return nil
diff --git a/internal/logger/logger_test.go b/internal/logger/logger_test.go
new file mode 100644
index 0000000..f93ec9b
--- /dev/null
+++ b/internal/logger/logger_test.go
@@ -0,0 +1,27 @@
+package logger
+
+import (
+ "fmt"
+ "github.com/sirupsen/logrus"
+ "github.com/sirupsen/logrus/hooks/test"
+ "github.com/stretchr/testify/assert"
+ "testing"
+)
+
+func TestSomething(t *testing.T){
+ logger, hook := test.NewNullLogger()
+
+ logger.Error("Helloerror")
+
+ assert.Equal(t, 1, len(hook.Entries))
+ assert.Equal(t, logrus.ErrorLevel, hook.LastEntry().Level)
+ assert.Equal(t, "Helloerror", hook.LastEntry().Message)
+
+ // TODO Check timestamp format here
+ assert.Equal(t, "", hook.LastEntry().Time)
+
+ fmt.Println(hook.LastEntry().Time)
+
+ hook.Reset()
+ assert.Nil(t, hook.LastEntry())
+}