Browse code

Move RFC3339NanoFixed to a more appropriate package.

Signed-off-by: Daniel Nephin <dnephin@docker.com>

Daniel Nephin authored on 2017/09/23 03:40:10
Showing 7 changed files
... ...
@@ -11,7 +11,7 @@ import (
11 11
 	"github.com/docker/docker/api/types"
12 12
 	"github.com/docker/docker/api/types/backend"
13 13
 	"github.com/docker/docker/pkg/ioutils"
14
-	"github.com/docker/docker/pkg/jsonlog"
14
+	"github.com/docker/docker/pkg/jsonmessage"
15 15
 	"github.com/docker/docker/pkg/stdcopy"
16 16
 )
17 17
 
... ...
@@ -49,11 +49,7 @@ func WriteLogStream(_ context.Context, w io.Writer, msgs <-chan *backend.LogMess
49 49
 			logLine = append(logLine, msg.Line...)
50 50
 		}
51 51
 		if config.Timestamps {
52
-			// TODO(dperny) the format is defined in
53
-			// daemon/logger/logger.go as logger.TimeFormat. importing
54
-			// logger is verboten (not part of backend) so idk if just
55
-			// importing the same thing from jsonlog is good enough
56
-			logLine = append([]byte(msg.Timestamp.Format(jsonlog.RFC3339NanoFixed)+" "), logLine...)
52
+			logLine = append([]byte(msg.Timestamp.Format(jsonmessage.RFC3339NanoFixed)+" "), logLine...)
57 53
 		}
58 54
 		if msg.Source == "stdout" && config.ShowStdout {
59 55
 			outStream.Write(logLine)
... ...
@@ -38,7 +38,7 @@ import (
38 38
 	"github.com/docker/docker/libcontainerd"
39 39
 	dopts "github.com/docker/docker/opts"
40 40
 	"github.com/docker/docker/pkg/authorization"
41
-	"github.com/docker/docker/pkg/jsonlog"
41
+	"github.com/docker/docker/pkg/jsonmessage"
42 42
 	"github.com/docker/docker/pkg/pidfile"
43 43
 	"github.com/docker/docker/pkg/plugingetter"
44 44
 	"github.com/docker/docker/pkg/signal"
... ...
@@ -94,7 +94,7 @@ func (cli *DaemonCli) start(opts *daemonOptions) (err error) {
94 94
 	}
95 95
 
96 96
 	logrus.SetFormatter(&logrus.TextFormatter{
97
-		TimestampFormat: jsonlog.RFC3339NanoFixed,
97
+		TimestampFormat: jsonmessage.RFC3339NanoFixed,
98 98
 		DisableColors:   cli.Config.RawLogs,
99 99
 		FullTimestamp:   true,
100 100
 	})
... ...
@@ -12,7 +12,6 @@ import (
12 12
 	"time"
13 13
 
14 14
 	"github.com/docker/docker/api/types/backend"
15
-	"github.com/docker/docker/pkg/jsonlog"
16 15
 )
17 16
 
18 17
 // ErrReadLogsNotSupported is returned when the underlying log driver does not support reading
... ...
@@ -26,8 +25,6 @@ func (ErrReadLogsNotSupported) Error() string {
26 26
 func (ErrReadLogsNotSupported) NotImplemented() {}
27 27
 
28 28
 const (
29
-	// TimeFormat is the time format used for timestamps sent to log readers.
30
-	TimeFormat           = jsonlog.RFC3339NanoFixed
31 29
 	logWatcherBufferSize = 4096
32 30
 )
33 31
 
... ...
@@ -10,7 +10,7 @@ import (
10 10
 
11 11
 	"github.com/docker/docker/integration-cli/checker"
12 12
 	"github.com/docker/docker/integration-cli/cli"
13
-	"github.com/docker/docker/pkg/jsonlog"
13
+	"github.com/docker/docker/pkg/jsonmessage"
14 14
 	"github.com/go-check/check"
15 15
 	"github.com/gotestyourself/gotestyourself/icmd"
16 16
 )
... ...
@@ -55,7 +55,7 @@ func (s *DockerSuite) TestLogsTimestamps(c *check.C) {
55 55
 
56 56
 	for _, l := range lines {
57 57
 		if l != "" {
58
-			_, err := time.Parse(jsonlog.RFC3339NanoFixed+" ", ts.FindString(l))
58
+			_, err := time.Parse(jsonmessage.RFC3339NanoFixed+" ", ts.FindString(l))
59 59
 			c.Assert(err, checker.IsNil, check.Commentf("Failed to parse timestamp from %v", l))
60 60
 			// ensure we have padded 0's
61 61
 			c.Assert(l[29], checker.Equals, uint8('Z'))
... ...
@@ -1,20 +1,13 @@
1
-// Package jsonlog provides helper functions to parse and print time (time.Time) as JSON.
2 1
 package jsonlog
3 2
 
4 3
 import (
5
-	"errors"
6 4
 	"time"
7
-)
8 5
 
9
-const (
10
-	// RFC3339NanoFixed is our own version of RFC339Nano because we want one
11
-	// that pads the nano seconds part with zeros to ensure
12
-	// the timestamps are aligned in the logs.
13
-	RFC3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
14
-	// JSONFormat is the format used by FastMarshalJSON
15
-	JSONFormat = `"` + time.RFC3339Nano + `"`
6
+	"github.com/pkg/errors"
16 7
 )
17 8
 
9
+const jsonFormat = `"` + time.RFC3339Nano + `"`
10
+
18 11
 // FastTimeMarshalJSON avoids one of the extra allocations that
19 12
 // time.MarshalJSON is making.
20 13
 func FastTimeMarshalJSON(t time.Time) (string, error) {
... ...
@@ -23,5 +16,5 @@ func FastTimeMarshalJSON(t time.Time) (string, error) {
23 23
 		// See golang.org/issue/4556#c15 for more discussion.
24 24
 		return "", errors.New("time.MarshalJSON: year outside of range [0,9999]")
25 25
 	}
26
-	return t.Format(JSONFormat), nil
26
+	return t.Format(jsonFormat), nil
27 27
 }
... ...
@@ -9,11 +9,14 @@ import (
9 9
 	"time"
10 10
 
11 11
 	gotty "github.com/Nvveen/Gotty"
12
-	"github.com/docker/docker/pkg/jsonlog"
13 12
 	"github.com/docker/docker/pkg/term"
14 13
 	units "github.com/docker/go-units"
15 14
 )
16 15
 
16
+// RFC3339NanoFixed is time.RFC3339Nano with nanoseconds padded using zeros to
17
+// ensure the formatted time isalways the same number of characters.
18
+const RFC3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
19
+
17 20
 // JSONError wraps a concrete Code and Message, `Code` is
18 21
 // is an integer error code, `Message` is the error message.
19 22
 type JSONError struct {
... ...
@@ -199,9 +202,9 @@ func (jm *JSONMessage) Display(out io.Writer, termInfo termInfo) error {
199 199
 		return nil
200 200
 	}
201 201
 	if jm.TimeNano != 0 {
202
-		fmt.Fprintf(out, "%s ", time.Unix(0, jm.TimeNano).Format(jsonlog.RFC3339NanoFixed))
202
+		fmt.Fprintf(out, "%s ", time.Unix(0, jm.TimeNano).Format(RFC3339NanoFixed))
203 203
 	} else if jm.Time != 0 {
204
-		fmt.Fprintf(out, "%s ", time.Unix(jm.Time, 0).Format(jsonlog.RFC3339NanoFixed))
204
+		fmt.Fprintf(out, "%s ", time.Unix(jm.Time, 0).Format(RFC3339NanoFixed))
205 205
 	}
206 206
 	if jm.ID != "" {
207 207
 		fmt.Fprintf(out, "%s: ", jm.ID)
... ...
@@ -8,7 +8,6 @@ import (
8 8
 	"testing"
9 9
 	"time"
10 10
 
11
-	"github.com/docker/docker/pkg/jsonlog"
12 11
 	"github.com/docker/docker/pkg/term"
13 12
 	"github.com/stretchr/testify/assert"
14 13
 )
... ...
@@ -115,8 +114,8 @@ func TestJSONMessageDisplay(t *testing.T) {
115 115
 			From:   "From",
116 116
 			Status: "status",
117 117
 		}: {
118
-			fmt.Sprintf("%v ID: (from From) status\n", time.Unix(now.Unix(), 0).Format(jsonlog.RFC3339NanoFixed)),
119
-			fmt.Sprintf("%v ID: (from From) status\n", time.Unix(now.Unix(), 0).Format(jsonlog.RFC3339NanoFixed)),
118
+			fmt.Sprintf("%v ID: (from From) status\n", time.Unix(now.Unix(), 0).Format(RFC3339NanoFixed)),
119
+			fmt.Sprintf("%v ID: (from From) status\n", time.Unix(now.Unix(), 0).Format(RFC3339NanoFixed)),
120 120
 		},
121 121
 		// General, with nano precision time
122 122
 		{
... ...
@@ -125,8 +124,8 @@ func TestJSONMessageDisplay(t *testing.T) {
125 125
 			From:     "From",
126 126
 			Status:   "status",
127 127
 		}: {
128
-			fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(jsonlog.RFC3339NanoFixed)),
129
-			fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(jsonlog.RFC3339NanoFixed)),
128
+			fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(RFC3339NanoFixed)),
129
+			fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(RFC3339NanoFixed)),
130 130
 		},
131 131
 		// General, with both times Nano is preferred
132 132
 		{
... ...
@@ -136,8 +135,8 @@ func TestJSONMessageDisplay(t *testing.T) {
136 136
 			From:     "From",
137 137
 			Status:   "status",
138 138
 		}: {
139
-			fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(jsonlog.RFC3339NanoFixed)),
140
-			fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(jsonlog.RFC3339NanoFixed)),
139
+			fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(RFC3339NanoFixed)),
140
+			fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(RFC3339NanoFixed)),
141 141
 		},
142 142
 		// Stream over status
143 143
 		{