[go] log: add flag LUTC, to use UTC time zone for time stamp

128 views
Skip to first unread message

Rob Pike (Gerrit)

unread,
Apr 10, 2015, 4:35:16 PM4/10/15
to Ian Lance Taylor, Rob Pike, golang-co...@googlegroups.com
Rob Pike uploaded a change:
https://go-review.googlesource.com/8761

log: add flag LUTC, to use UTC time zone for time stamp

Issue 9483 suggests several approaches to correlating logs from
machines in different time zones. This approach is the simplest and
really should be sufficient: provide a way to clamp the time stamps
to UTC.

Fixes issue #9483.

Change-Id: If540b991d758c4d845a719779f8255ece7c452e7
---
M src/log/log.go
M src/log/log_test.go
2 files changed, 33 insertions(+), 3 deletions(-)



diff --git a/src/log/log.go b/src/log/log.go
index 9b68008..35d8813 100644
--- a/src/log/log.go
+++ b/src/log/log.go
@@ -32,11 +32,12 @@
// 2009/01/23 01:23:23 message
// while flags Ldate | Ltime | Lmicroseconds | Llongfile produce,
// 2009/01/23 01:23:23.123123 /a/b/c/d.go:23: message
- Ldate = 1 << iota // the date: 2009/01/23
- Ltime // the time: 01:23:23
+ Ldate = 1 << iota // the date in the local time zone:
2009/01/23
+ Ltime // the time in the local time zone: 01:23:23
Lmicroseconds // microsecond resolution:
01:23:23.123123. assumes Ltime.
Llongfile // full file name and line number:
/a/b/c/d.go:23
Lshortfile // final file name element and line number:
d.go:23. overrides Llongfile
+ LUTC // if Ldate or Ltime is set, use UTC rather
than the local time zone
LstdFlags = Ldate | Ltime // initial values for the standard logger
)

@@ -90,6 +91,9 @@
*buf = append(*buf, l.prefix...)
if l.flag&(Ldate|Ltime|Lmicroseconds) != 0 {
if l.flag&Ldate != 0 {
+ if l.flag&LUTC != 0 {
+ t = t.UTC()
+ }
year, month, day := t.Date()
itoa(buf, year, 4)
*buf = append(*buf, '/')
diff --git a/src/log/log_test.go b/src/log/log_test.go
index d7d2490..709de1e 100644
--- a/src/log/log_test.go
+++ b/src/log/log_test.go
@@ -8,17 +8,19 @@

import (
"bytes"
+ "fmt"
"os"
"regexp"
"strings"
"testing"
+ "time"
)

const (
Rdate = `[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]`
Rtime = `[0-9][0-9]:[0-9][0-9]:[0-9][0-9]`
Rmicroseconds = `\.[0-9][0-9][0-9][0-9][0-9][0-9]`
- Rline = `(55|57):` // must update if the calls to l.Printf /
l.Print below move
+ Rline = `(57|59):` // must update if the calls to l.Printf /
l.Print below move
Rlongfile = `.*/[A-Za-z0-9_\-]+\.go:` + Rline
Rshortfile = `[A-Za-z0-9_\-]+\.go:` + Rline
)
@@ -119,6 +121,30 @@
}
}

+func TestUTCFlag(t *testing.T) {
+ var b bytes.Buffer
+ l := New(&b, "Test:", LstdFlags)
+ l.SetFlags(Ldate | Ltime | LUTC)
+ // Verify a log message looks right in the right time zone. Quantize to
the second only.
+ now := time.Now().UTC()
+ l.Print("hello")
+ want := fmt.Sprintf("Test:%d/%.2d/%.2d %.2d:%.2d:%.2d hello\n",
+ now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(),
now.Second())
+ got := b.String()
+ if got == want {
+ return
+ }
+ // It's possible we crossed a second boundary between getting now and
logging,
+ // so add a second and try again. This should very nearly always work.
+ now.Add(time.Second)
+ want = fmt.Sprintf("Test:%d/%.2d/%.2d %.2d:%.2d:%.2d hello\n",
+ now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute())
+ if got == want {
+ return
+ }
+ t.Errorf("got %q; want %q", got, want)
+}
+
func TestEmptyPrintCreatesLine(t *testing.T) {
var b bytes.Buffer
l := New(&b, "Header:", LstdFlags)

--
https://go-review.googlesource.com/8761

Ian Lance Taylor (Gerrit)

unread,
Apr 10, 2015, 5:39:24 PM4/10/15
to Rob Pike, golang-co...@googlegroups.com
Ian Lance Taylor has posted comments on this change.

log: add flag LUTC, to use UTC time zone for time stamp

Patch Set 1:

(1 comment)

https://go-review.googlesource.com/#/c/8761/1/src/log/log.go
File src/log/log.go:

Line 94: if l.flag&LUTC != 0 {
Seems like the wrong thing happens for Ltime|LUTC. Since Ldate is not set
this conversion never happens.


--
https://go-review.googlesource.com/8761
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-HasComments: Yes

Rob Pike (Gerrit)

unread,
Apr 10, 2015, 6:57:53 PM4/10/15
to Rob Pike, Ian Lance Taylor, golang-co...@googlegroups.com
Rob Pike uploaded a new patch set:
https://go-review.googlesource.com/8761

log: add flag LUTC, to use UTC time zone for time stamp

Issue 9483 suggests several approaches to correlating logs from
machines in different time zones. This approach is the simplest and
really should be sufficient: provide a way to clamp the time stamps
to UTC.

Fixes #9483.

Change-Id: If540b991d758c4d845a719779f8255ece7c452e7
---
M src/log/log.go
M src/log/log_test.go
2 files changed, 33 insertions(+), 3 deletions(-)


Rob Pike (Gerrit)

unread,
Apr 10, 2015, 6:58:02 PM4/10/15
to Rob Pike, Ian Lance Taylor, golang-co...@googlegroups.com
Rob Pike has posted comments on this change.

log: add flag LUTC, to use UTC time zone for time stamp

Patch Set 1:

(1 comment)

https://go-review.googlesource.com/#/c/8761/1/src/log/log.go
File src/log/log.go:

Line 94: if l.flag&LUTC != 0 {
> Seems like the wrong thing happens for Ltime|LUTC. Since Ldate is not set
Done


--
https://go-review.googlesource.com/8761
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Reviewer: Rob Pike <r...@golang.org>
Gerrit-HasComments: Yes

Ian Lance Taylor (Gerrit)

unread,
Apr 10, 2015, 7:52:52 PM4/10/15
to Rob Pike, golang-co...@googlegroups.com
Ian Lance Taylor has posted comments on this change.

log: add flag LUTC, to use UTC time zone for time stamp

Patch Set 2: Code-Review+2

--
https://go-review.googlesource.com/8761
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Reviewer: Rob Pike <r...@golang.org>
Gerrit-HasComments: No

Rob Pike (Gerrit)

unread,
Apr 10, 2015, 10:30:28 PM4/10/15
to Rob Pike, golang-...@googlegroups.com, Ian Lance Taylor, golang-co...@googlegroups.com
Rob Pike has submitted this change and it was merged.

log: add flag LUTC, to use UTC time zone for time stamp

Issue 9483 suggests several approaches to correlating logs from
machines in different time zones. This approach is the simplest and
really should be sufficient: provide a way to clamp the time stamps
to UTC.

Fixes #9483.

Change-Id: If540b991d758c4d845a719779f8255ece7c452e7
Reviewed-on: https://go-review.googlesource.com/8761
Reviewed-by: Ian Lance Taylor <ia...@golang.org>
---
M src/log/log.go
M src/log/log_test.go
2 files changed, 33 insertions(+), 3 deletions(-)

Approvals:
Ian Lance Taylor: Looks good to me, approved
Reply all
Reply to author
Forward
0 new messages