Gerrit Bot has uploaded this change for review.
log: support millisecond timestamp resolution
Existing implementation had only microsecond or second resolution, but microsecond resolution could be seen as too precise (access to logs could be used for side channel attacks) while second resolution is not precise enough.
This adds Lmilliseconds flag.
Change-Id: Ib2c56afa4a7211a8ae473fe781909d8ff2819397
GitHub-Last-Rev: 520c71f94b335ffe2f07d908011c3dda78f2be4c
GitHub-Pull-Request: golang/go#60246
---
M src/log/log.go
M src/log/log_test.go
2 files changed, 14 insertions(+), 3 deletions(-)
diff --git a/src/log/log.go b/src/log/log.go
index 9d5440e..4124ddb 100644
--- a/src/log/log.go
+++ b/src/log/log.go
@@ -47,6 +47,7 @@
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
Lmsgprefix // move the "prefix" from the beginning of the line to before the message
+ Lmilliseconds // millisecond resolution: 01:23:23.123. assumes Ltime.
LstdFlags = Ldate | Ltime // initial values for the standard logger
)
@@ -115,7 +116,7 @@
if flag&Lmsgprefix == 0 {
*buf = append(*buf, prefix...)
}
- if flag&(Ldate|Ltime|Lmicroseconds) != 0 {
+ if flag&(Ldate|Ltime|Lmicroseconds|Lmilliseconds) != 0 {
if flag&LUTC != 0 {
t = t.UTC()
}
@@ -128,7 +129,7 @@
itoa(buf, day, 2)
*buf = append(*buf, ' ')
}
- if flag&(Ltime|Lmicroseconds) != 0 {
+ if flag&(Ltime|Lmicroseconds|Lmilliseconds) != 0 {
hour, min, sec := t.Clock()
itoa(buf, hour, 2)
*buf = append(*buf, ':')
@@ -138,6 +139,9 @@
if flag&Lmicroseconds != 0 {
*buf = append(*buf, '.')
itoa(buf, t.Nanosecond()/1e3, 6)
+ } else if flag&Lmilliseconds != 0 {
+ *buf = append(*buf, '.')
+ itoa(buf, t.Nanosecond()/1e6, 3)
}
*buf = append(*buf, ' ')
}
diff --git a/src/log/log_test.go b/src/log/log_test.go
index c7fa78f..c879b90 100644
--- a/src/log/log_test.go
+++ b/src/log/log_test.go
@@ -23,7 +23,8 @@
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 = `(63|65):` // must update if the calls to l.Printf / l.Print below move
+ Rmilliseconds = `\.[0-9][0-9][0-9]`
+ Rline = `(70|72):` // 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
)
@@ -43,6 +44,8 @@
{Ltime | Lmsgprefix, "XXX", Rtime + " XXX"},
{Ltime | Lmicroseconds, "", Rtime + Rmicroseconds + " "},
{Lmicroseconds, "", Rtime + Rmicroseconds + " "}, // microsec implies time
+ {Ltime | Lmilliseconds, "", Rtime + Rmilliseconds + " "},
+ {Lmilliseconds, "", Rtime + Rmilliseconds + " "}, // millisec implies time
{Llongfile, "", Rlongfile + " "},
{Lshortfile, "", Rshortfile + " "},
{Llongfile | Lshortfile, "", Rshortfile + " "}, // shortfile overrides longfile
@@ -51,6 +54,10 @@
{Ldate | Ltime | Lmicroseconds | Lshortfile, "XXX", "XXX" + Rdate + " " + Rtime + Rmicroseconds + " " + Rshortfile + " "},
{Ldate | Ltime | Lmicroseconds | Llongfile | Lmsgprefix, "XXX", Rdate + " " + Rtime + Rmicroseconds + " " + Rlongfile + " XXX"},
{Ldate | Ltime | Lmicroseconds | Lshortfile | Lmsgprefix, "XXX", Rdate + " " + Rtime + Rmicroseconds + " " + Rshortfile + " XXX"},
+ {Ldate | Ltime | Lmilliseconds | Llongfile, "XXX", "XXX" + Rdate + " " + Rtime + Rmilliseconds + " " + Rlongfile + " "},
+ {Ldate | Ltime | Lmilliseconds | Lshortfile, "XXX", "XXX" + Rdate + " " + Rtime + Rmilliseconds + " " + Rshortfile + " "},
+ {Ldate | Ltime | Lmilliseconds | Llongfile | Lmsgprefix, "XXX", Rdate + " " + Rtime + Rmilliseconds + " " + Rlongfile + " XXX"},
+ {Ldate | Ltime | Lmilliseconds | Lshortfile | Lmsgprefix, "XXX", Rdate + " " + Rtime + Rmilliseconds + " " + Rshortfile + " XXX"},
}
// Test using Println("hello", 23, "world") or using Printf("hello %d world", 23)
To view, visit change 495298. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Rob Pike.
Patch set 1:Hold +1
1 comment:
Patchset:
Thanks. New public API like this is required to go through the proposal process; see https://go.dev/s/proposal.
Putting on hold until there is an accepted proposal.
To view, visit change 495298. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Ian Lance Taylor, Rob Pike.
1 comment:
Patchset:
Thanks. New public API like this is required to go through the proposal process; see https://go. […]
Thanks. I made https://github.com/golang/go/issues/60249.
To view, visit change 495298. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Rob Pike.
1 comment:
Commit Message:
Please add a line
Fixes #60249
to link the change and the issue. Thanks.
To view, visit change 495298. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Ian Lance Taylor, Rob Pike.
1 comment:
Commit Message:
Please add a line […]
Done.
To view, visit change 495298. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Ian Lance Taylor, Rob Pike.
Gerrit Bot uploaded patch set #2 to this change.
log: support millisecond timestamp resolution
Existing implementation had only microsecond or second resolution, but microsecond resolution could be seen as too precise (access to logs could be used for side channel attacks) while second resolution is not precise enough.
This adds Lmilliseconds flag.
Fixes #60249
Change-Id: Ib2c56afa4a7211a8ae473fe781909d8ff2819397
GitHub-Last-Rev: e57d2bce9dee2e1fb7af1503feb21e3cdaa3fbe5
GitHub-Pull-Request: golang/go#60246
---
M src/log/log.go
M src/log/log_test.go
2 files changed, 14 insertions(+), 3 deletions(-)
To view, visit change 495298. To unsubscribe, or for help writing mail filters, visit settings.
Sean Liao abandoned this change.
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |