From 6e655f4cb3b60ee88c7718998a8ccb00938a337e Mon Sep 17 00:00:00 2001 From: Binaek Sarkar Date: Wed, 15 Dec 2021 22:15:49 +0530 Subject: [PATCH 1/3] switch to UTC on flag --- intlogger.go | 5 +++++ logger.go | 3 +++ logger_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/intlogger.go b/intlogger.go index e2362e8..4b9b531 100644 --- a/intlogger.go +++ b/intlogger.go @@ -60,6 +60,7 @@ type intLogger struct { callerOffset int name string timeFormat string + useUtcTime bool disableTime bool // This is an interface so that it's shared by any derived loggers, since @@ -116,6 +117,7 @@ func newLogger(opts *LoggerOptions) *intLogger { json: opts.JSONFormat, name: opts.Name, timeFormat: TimeFormat, + useUtcTime: opts.UTCTime, disableTime: opts.DisableTime, mutex: mutex, writer: newWriter(output, opts.Color), @@ -153,6 +155,9 @@ func (l *intLogger) log(name string, level Level, msg string, args ...interface{ } t := time.Now() + if l.useUtcTime { + t = t.UTC() + } l.mutex.Lock() defer l.mutex.Unlock() diff --git a/logger.go b/logger.go index 6a4665b..d16a7e3 100644 --- a/logger.go +++ b/logger.go @@ -248,6 +248,9 @@ type LoggerOptions struct { // The time format to use instead of the default TimeFormat string + // Control whether to use UTC instead of the default local time-zone + UTCTime bool + // Control whether or not to display the time at all. This is required // because setting TimeFormat to empty assumes the default format. DisableTime bool diff --git a/logger_test.go b/logger_test.go index d70ed9e..57cfa55 100644 --- a/logger_test.go +++ b/logger_test.go @@ -249,6 +249,24 @@ func TestLogger(t *testing.T) { assert.Equal(t, str[:dataIdx], time.Now().Format(time.Kitchen)) }) + t.Run("use UTC time zone", func(t *testing.T) { + var buf bytes.Buffer + + logger := New(&LoggerOptions{ + Name: "test", + Output: &buf, + TimeFormat: time.Kitchen, + UTCTime: true, + }) + + logger.Info("this is test", "who", "programmer", "why", "testing is fun") + + str := buf.String() + dataIdx := strings.IndexByte(str, ' ') + + assert.Equal(t, str[:dataIdx], time.Now().UTC().Format(time.Kitchen)) + }) + t.Run("respects DisableTime", func(t *testing.T) { var buf bytes.Buffer @@ -657,6 +675,34 @@ func TestLogger_JSON(t *testing.T) { assert.Equal(t, val, time.Now().Format(time.Kitchen)) }) + t.Run("use UTC time zone", func(t *testing.T) { + var buf bytes.Buffer + + logger := New(&LoggerOptions{ + Name: "test", + Output: &buf, + JSONFormat: true, + TimeFormat: time.Kitchen, + UTCTime: true, + }) + + logger.Info("Lacatan banana") + + b := buf.Bytes() + + var raw map[string]interface{} + if err := json.Unmarshal(b, &raw); err != nil { + t.Fatal(err) + } + + val, ok := raw["@timestamp"] + if !ok { + t.Fatal("missing '@timestamp' key") + } + + assert.Equal(t, val, time.Now().UTC().Format(time.Kitchen)) + }) + t.Run("respects DisableTime", func(t *testing.T) { var buf bytes.Buffer logger := New(&LoggerOptions{ From 13b17eff16fa1bc078162866211973f19a14b80d Mon Sep 17 00:00:00 2001 From: Binaek Sarkar Date: Tue, 21 Dec 2021 17:11:44 +0530 Subject: [PATCH 2/3] add support for custom time function --- global.go | 2 ++ intlogger.go | 12 ++++++------ logger.go | 7 +++++-- logger_test.go | 4 ++-- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/global.go b/global.go index 22ebc57..36888fd 100644 --- a/global.go +++ b/global.go @@ -2,6 +2,7 @@ package hclog import ( "sync" + "time" ) var ( @@ -14,6 +15,7 @@ var ( DefaultOptions = &LoggerOptions{ Level: DefaultLevel, Output: DefaultOutput, + TimeFn: func() time.Time { return time.Now() }, } ) diff --git a/intlogger.go b/intlogger.go index 4b9b531..2d63fae 100644 --- a/intlogger.go +++ b/intlogger.go @@ -60,7 +60,7 @@ type intLogger struct { callerOffset int name string timeFormat string - useUtcTime bool + timeFn TimeFunction disableTime bool // This is an interface so that it's shared by any derived loggers, since @@ -117,7 +117,7 @@ func newLogger(opts *LoggerOptions) *intLogger { json: opts.JSONFormat, name: opts.Name, timeFormat: TimeFormat, - useUtcTime: opts.UTCTime, + timeFn: func() time.Time { return time.Now() }, disableTime: opts.DisableTime, mutex: mutex, writer: newWriter(output, opts.Color), @@ -132,6 +132,9 @@ func newLogger(opts *LoggerOptions) *intLogger { if l.json { l.timeFormat = TimeFormatJSON } + if opts.TimeFn != nil { + l.timeFn = opts.TimeFn + } if opts.TimeFormat != "" { l.timeFormat = opts.TimeFormat } @@ -154,10 +157,7 @@ func (l *intLogger) log(name string, level Level, msg string, args ...interface{ return } - t := time.Now() - if l.useUtcTime { - t = t.UTC() - } + t := l.timeFn() l.mutex.Lock() defer l.mutex.Unlock() diff --git a/logger.go b/logger.go index d16a7e3..0a3f606 100644 --- a/logger.go +++ b/logger.go @@ -5,6 +5,7 @@ import ( "log" "os" "strings" + "time" ) var ( @@ -219,6 +220,8 @@ type StandardLoggerOptions struct { ForceLevel Level } +type TimeFunction = func() time.Time + // LoggerOptions can be used to configure a new logger. type LoggerOptions struct { // Name of the subsystem to prefix logs with @@ -248,8 +251,8 @@ type LoggerOptions struct { // The time format to use instead of the default TimeFormat string - // Control whether to use UTC instead of the default local time-zone - UTCTime bool + // A function which is called to get the time object that is formatted using `TimeFormat` + TimeFn TimeFunction // Control whether or not to display the time at all. This is required // because setting TimeFormat to empty assumes the default format. diff --git a/logger_test.go b/logger_test.go index 57cfa55..b098b7e 100644 --- a/logger_test.go +++ b/logger_test.go @@ -256,7 +256,7 @@ func TestLogger(t *testing.T) { Name: "test", Output: &buf, TimeFormat: time.Kitchen, - UTCTime: true, + TimeFn: func() time.Time { return time.Now().UTC() }, }) logger.Info("this is test", "who", "programmer", "why", "testing is fun") @@ -683,7 +683,7 @@ func TestLogger_JSON(t *testing.T) { Output: &buf, JSONFormat: true, TimeFormat: time.Kitchen, - UTCTime: true, + TimeFn: func() time.Time { return time.Now().UTC() }, }) logger.Info("Lacatan banana") From 7465a0a000005ebe6c224b6f6d7d977363095822 Mon Sep 17 00:00:00 2001 From: Binaek Sarkar Date: Wed, 22 Dec 2021 13:14:53 +0530 Subject: [PATCH 3/3] taking suggestions --- global.go | 2 +- intlogger.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/global.go b/global.go index 36888fd..b9f0021 100644 --- a/global.go +++ b/global.go @@ -15,7 +15,7 @@ var ( DefaultOptions = &LoggerOptions{ Level: DefaultLevel, Output: DefaultOutput, - TimeFn: func() time.Time { return time.Now() }, + TimeFn: time.Now, } ) diff --git a/intlogger.go b/intlogger.go index 2d63fae..9a4ef31 100644 --- a/intlogger.go +++ b/intlogger.go @@ -117,7 +117,7 @@ func newLogger(opts *LoggerOptions) *intLogger { json: opts.JSONFormat, name: opts.Name, timeFormat: TimeFormat, - timeFn: func() time.Time { return time.Now() }, + timeFn: time.Now, disableTime: opts.DisableTime, mutex: mutex, writer: newWriter(output, opts.Color),