Skip to content

Commit

Permalink
switch to UTC on flag
Browse files Browse the repository at this point in the history
  • Loading branch information
binaek committed Dec 15, 2021
1 parent 26c3b98 commit 6e655f4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
5 changes: 5 additions & 0 deletions intlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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()
Expand Down
3 changes: 3 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 46 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 6e655f4

Please sign in to comment.