From b9117e2ff1b0c410d704f4111daf8a8cbf4f2fd7 Mon Sep 17 00:00:00 2001 From: Tao Zhou Date: Thu, 3 Aug 2017 17:10:50 +0800 Subject: [PATCH] add millisecond to timestamp in log output --- logs/logger.go | 13 ++++++++++--- logs/logger_test.go | 4 ++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/logs/logger.go b/logs/logger.go index b5d7255f..1700901f 100644 --- a/logs/logger.go +++ b/logs/logger.go @@ -87,13 +87,15 @@ const ( mi2 = `012345678901234567890123456789012345678901234567890123456789` s1 = `000000000011111111112222222222333333333344444444445555555555` s2 = `012345678901234567890123456789012345678901234567890123456789` + ns1 = `0123456789` ) func formatTimeHeader(when time.Time) ([]byte, int) { y, mo, d := when.Date() h, mi, s := when.Clock() - //len("2006/01/02 15:04:05 ")==20 - var buf [20]byte + ns := when.Nanosecond()/1000000 + //len("2006/01/02 15:04:05.123 ")==24 + var buf [24]byte buf[0] = y1[y/1000%10] buf[1] = y2[y/100] @@ -114,7 +116,12 @@ func formatTimeHeader(when time.Time) ([]byte, int) { buf[16] = ':' buf[17] = s1[s] buf[18] = s2[s] - buf[19] = ' ' + buf[19] = '.' + buf[20] = ns1[ns/100] + buf[21] = ns1[ns%100/10] + buf[22] = ns1[ns%10] + + buf[23] = ' ' return buf[0:], d } diff --git a/logs/logger_test.go b/logs/logger_test.go index 119b7bd3..69a8f0d2 100644 --- a/logs/logger_test.go +++ b/logs/logger_test.go @@ -31,7 +31,7 @@ func TestFormatHeader_0(t *testing.T) { break } h, _ := formatTimeHeader(tm) - if tm.Format("2006/01/02 15:04:05 ") != string(h) { + if tm.Format("2006/01/02 15:04:05.999 ") != string(h) { t.Log(tm) t.FailNow() } @@ -49,7 +49,7 @@ func TestFormatHeader_1(t *testing.T) { break } h, _ := formatTimeHeader(tm) - if tm.Format("2006/01/02 15:04:05 ") != string(h) { + if tm.Format("2006/01/02 15:04:05.999 ") != string(h) { t.Log(tm) t.FailNow() }