Skip to content

Commit 582007f

Browse files
authored
Add a time.Location to ConsoleWriter. (This allows UTC timestamps.) (#531)
* Add a time.Location to ConsoleWriter. (This allows UTC timestamps.) * add test
1 parent 54ebf46 commit 582007f

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

console.go

+16-8
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ type ConsoleWriter struct {
5757
// TimeFormat specifies the format for timestamp in output.
5858
TimeFormat string
5959

60+
// TimeLocation tells ConsoleWriter’s default FormatTimestamp
61+
// how to localize the time.
62+
TimeLocation *time.Location
63+
6064
// PartsOrder defines the order of parts in output.
6165
PartsOrder []string
6266

@@ -83,9 +87,9 @@ type ConsoleWriter struct {
8387
// NewConsoleWriter creates and initializes a new ConsoleWriter.
8488
func NewConsoleWriter(options ...func(w *ConsoleWriter)) ConsoleWriter {
8589
w := ConsoleWriter{
86-
Out: os.Stdout,
87-
TimeFormat: consoleDefaultTimeFormat,
88-
PartsOrder: consoleDefaultPartsOrder(),
90+
Out: os.Stdout,
91+
TimeFormat: consoleDefaultTimeFormat,
92+
PartsOrder: consoleDefaultPartsOrder(),
8993
}
9094

9195
for _, opt := range options {
@@ -284,7 +288,7 @@ func (w ConsoleWriter) writePart(buf *bytes.Buffer, evt map[string]interface{},
284288
}
285289
case TimestampFieldName:
286290
if w.FormatTimestamp == nil {
287-
f = consoleDefaultFormatTimestamp(w.TimeFormat, w.NoColor)
291+
f = consoleDefaultFormatTimestamp(w.TimeFormat, w.TimeLocation, w.NoColor)
288292
} else {
289293
f = w.FormatTimestamp
290294
}
@@ -352,19 +356,23 @@ func consoleDefaultPartsOrder() []string {
352356
}
353357
}
354358

355-
func consoleDefaultFormatTimestamp(timeFormat string, noColor bool) Formatter {
359+
func consoleDefaultFormatTimestamp(timeFormat string, location *time.Location, noColor bool) Formatter {
356360
if timeFormat == "" {
357361
timeFormat = consoleDefaultTimeFormat
358362
}
363+
if location == nil {
364+
location = time.Local
365+
}
366+
359367
return func(i interface{}) string {
360368
t := "<nil>"
361369
switch tt := i.(type) {
362370
case string:
363-
ts, err := time.ParseInLocation(TimeFieldFormat, tt, time.Local)
371+
ts, err := time.ParseInLocation(TimeFieldFormat, tt, location)
364372
if err != nil {
365373
t = tt
366374
} else {
367-
t = ts.Local().Format(timeFormat)
375+
t = ts.In(location).Format(timeFormat)
368376
}
369377
case json.Number:
370378
i, err := tt.Int64()
@@ -385,7 +393,7 @@ func consoleDefaultFormatTimestamp(timeFormat string, noColor bool) Formatter {
385393
}
386394

387395
ts := time.Unix(sec, nsec)
388-
t = ts.Format(timeFormat)
396+
t = ts.In(location).Format(timeFormat)
389397
}
390398
}
391399
return colorize(t, colorDarkGray, noColor)

console_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,35 @@ func TestConsoleWriterConfiguration(t *testing.T) {
343343
}
344344
})
345345

346+
t.Run("Sets TimeFormat and TimeLocation", func(t *testing.T) {
347+
locs := []*time.Location{ time.Local, time.UTC }
348+
349+
for _, location := range locs {
350+
buf := &bytes.Buffer{}
351+
w := zerolog.ConsoleWriter{
352+
Out: buf,
353+
NoColor: true,
354+
TimeFormat: time.RFC3339,
355+
TimeLocation: location,
356+
}
357+
358+
ts := time.Unix(0, 0)
359+
d := ts.UTC().Format(time.RFC3339)
360+
evt := `{"time": "` + d + `", "level": "info", "message": "Foobar"}`
361+
362+
_, err := w.Write([]byte(evt))
363+
if err != nil {
364+
t.Errorf("Unexpected error when writing output: %s", err)
365+
}
366+
367+
expectedOutput := ts.In(location).Format(time.RFC3339) + " INF Foobar\n"
368+
actualOutput := buf.String()
369+
if actualOutput != expectedOutput {
370+
t.Errorf("Unexpected output %q, want: %q (location=%s)", actualOutput, expectedOutput, location)
371+
}
372+
}
373+
})
374+
346375
t.Run("Sets PartsOrder", func(t *testing.T) {
347376
buf := &bytes.Buffer{}
348377
w := zerolog.ConsoleWriter{Out: buf, NoColor: true, PartsOrder: []string{"message", "level"}}

0 commit comments

Comments
 (0)