@@ -57,6 +57,10 @@ type ConsoleWriter struct {
57
57
// TimeFormat specifies the format for timestamp in output.
58
58
TimeFormat string
59
59
60
+ // TimeLocation tells ConsoleWriter’s default FormatTimestamp
61
+ // how to localize the time.
62
+ TimeLocation * time.Location
63
+
60
64
// PartsOrder defines the order of parts in output.
61
65
PartsOrder []string
62
66
@@ -83,9 +87,9 @@ type ConsoleWriter struct {
83
87
// NewConsoleWriter creates and initializes a new ConsoleWriter.
84
88
func NewConsoleWriter (options ... func (w * ConsoleWriter )) ConsoleWriter {
85
89
w := ConsoleWriter {
86
- Out : os .Stdout ,
87
- TimeFormat : consoleDefaultTimeFormat ,
88
- PartsOrder : consoleDefaultPartsOrder (),
90
+ Out : os .Stdout ,
91
+ TimeFormat : consoleDefaultTimeFormat ,
92
+ PartsOrder : consoleDefaultPartsOrder (),
89
93
}
90
94
91
95
for _ , opt := range options {
@@ -284,7 +288,7 @@ func (w ConsoleWriter) writePart(buf *bytes.Buffer, evt map[string]interface{},
284
288
}
285
289
case TimestampFieldName :
286
290
if w .FormatTimestamp == nil {
287
- f = consoleDefaultFormatTimestamp (w .TimeFormat , w .NoColor )
291
+ f = consoleDefaultFormatTimestamp (w .TimeFormat , w .TimeLocation , w . NoColor )
288
292
} else {
289
293
f = w .FormatTimestamp
290
294
}
@@ -352,19 +356,23 @@ func consoleDefaultPartsOrder() []string {
352
356
}
353
357
}
354
358
355
- func consoleDefaultFormatTimestamp (timeFormat string , noColor bool ) Formatter {
359
+ func consoleDefaultFormatTimestamp (timeFormat string , location * time. Location , noColor bool ) Formatter {
356
360
if timeFormat == "" {
357
361
timeFormat = consoleDefaultTimeFormat
358
362
}
363
+ if location == nil {
364
+ location = time .Local
365
+ }
366
+
359
367
return func (i interface {}) string {
360
368
t := "<nil>"
361
369
switch tt := i .(type ) {
362
370
case string :
363
- ts , err := time .ParseInLocation (TimeFieldFormat , tt , time . Local )
371
+ ts , err := time .ParseInLocation (TimeFieldFormat , tt , location )
364
372
if err != nil {
365
373
t = tt
366
374
} else {
367
- t = ts .Local ( ).Format (timeFormat )
375
+ t = ts .In ( location ).Format (timeFormat )
368
376
}
369
377
case json.Number :
370
378
i , err := tt .Int64 ()
@@ -385,7 +393,7 @@ func consoleDefaultFormatTimestamp(timeFormat string, noColor bool) Formatter {
385
393
}
386
394
387
395
ts := time .Unix (sec , nsec )
388
- t = ts .Format (timeFormat )
396
+ t = ts .In ( location ). Format (timeFormat )
389
397
}
390
398
}
391
399
return colorize (t , colorDarkGray , noColor )
0 commit comments