1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Move RFC3339NanoFixed to a more appropriate package.

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2017-09-22 14:40:10 -04:00
parent c982ee805d
commit 27cfa68af1
7 changed files with 22 additions and 34 deletions

View file

@ -11,7 +11,7 @@ import (
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/backend" "github.com/docker/docker/api/types/backend"
"github.com/docker/docker/pkg/ioutils" "github.com/docker/docker/pkg/ioutils"
"github.com/docker/docker/pkg/jsonlog" "github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/stdcopy" "github.com/docker/docker/pkg/stdcopy"
) )
@ -49,11 +49,7 @@ func WriteLogStream(_ context.Context, w io.Writer, msgs <-chan *backend.LogMess
logLine = append(logLine, msg.Line...) logLine = append(logLine, msg.Line...)
} }
if config.Timestamps { if config.Timestamps {
// TODO(dperny) the format is defined in logLine = append([]byte(msg.Timestamp.Format(jsonmessage.RFC3339NanoFixed)+" "), logLine...)
// daemon/logger/logger.go as logger.TimeFormat. importing
// logger is verboten (not part of backend) so idk if just
// importing the same thing from jsonlog is good enough
logLine = append([]byte(msg.Timestamp.Format(jsonlog.RFC3339NanoFixed)+" "), logLine...)
} }
if msg.Source == "stdout" && config.ShowStdout { if msg.Source == "stdout" && config.ShowStdout {
outStream.Write(logLine) outStream.Write(logLine)

View file

@ -38,7 +38,7 @@ import (
"github.com/docker/docker/libcontainerd" "github.com/docker/docker/libcontainerd"
dopts "github.com/docker/docker/opts" dopts "github.com/docker/docker/opts"
"github.com/docker/docker/pkg/authorization" "github.com/docker/docker/pkg/authorization"
"github.com/docker/docker/pkg/jsonlog" "github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/pidfile" "github.com/docker/docker/pkg/pidfile"
"github.com/docker/docker/pkg/plugingetter" "github.com/docker/docker/pkg/plugingetter"
"github.com/docker/docker/pkg/signal" "github.com/docker/docker/pkg/signal"
@ -94,7 +94,7 @@ func (cli *DaemonCli) start(opts *daemonOptions) (err error) {
} }
logrus.SetFormatter(&logrus.TextFormatter{ logrus.SetFormatter(&logrus.TextFormatter{
TimestampFormat: jsonlog.RFC3339NanoFixed, TimestampFormat: jsonmessage.RFC3339NanoFixed,
DisableColors: cli.Config.RawLogs, DisableColors: cli.Config.RawLogs,
FullTimestamp: true, FullTimestamp: true,
}) })

View file

@ -12,7 +12,6 @@ import (
"time" "time"
"github.com/docker/docker/api/types/backend" "github.com/docker/docker/api/types/backend"
"github.com/docker/docker/pkg/jsonlog"
) )
// ErrReadLogsNotSupported is returned when the underlying log driver does not support reading // ErrReadLogsNotSupported is returned when the underlying log driver does not support reading
@ -26,8 +25,6 @@ func (ErrReadLogsNotSupported) Error() string {
func (ErrReadLogsNotSupported) NotImplemented() {} func (ErrReadLogsNotSupported) NotImplemented() {}
const ( const (
// TimeFormat is the time format used for timestamps sent to log readers.
TimeFormat = jsonlog.RFC3339NanoFixed
logWatcherBufferSize = 4096 logWatcherBufferSize = 4096
) )

View file

@ -10,7 +10,7 @@ import (
"github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/integration-cli/checker"
"github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/integration-cli/cli"
"github.com/docker/docker/pkg/jsonlog" "github.com/docker/docker/pkg/jsonmessage"
"github.com/go-check/check" "github.com/go-check/check"
"github.com/gotestyourself/gotestyourself/icmd" "github.com/gotestyourself/gotestyourself/icmd"
) )
@ -55,7 +55,7 @@ func (s *DockerSuite) TestLogsTimestamps(c *check.C) {
for _, l := range lines { for _, l := range lines {
if l != "" { if l != "" {
_, err := time.Parse(jsonlog.RFC3339NanoFixed+" ", ts.FindString(l)) _, err := time.Parse(jsonmessage.RFC3339NanoFixed+" ", ts.FindString(l))
c.Assert(err, checker.IsNil, check.Commentf("Failed to parse timestamp from %v", l)) c.Assert(err, checker.IsNil, check.Commentf("Failed to parse timestamp from %v", l))
// ensure we have padded 0's // ensure we have padded 0's
c.Assert(l[29], checker.Equals, uint8('Z')) c.Assert(l[29], checker.Equals, uint8('Z'))

View file

@ -1,19 +1,12 @@
// Package jsonlog provides helper functions to parse and print time (time.Time) as JSON.
package jsonlog package jsonlog
import ( import (
"errors"
"time" "time"
"github.com/pkg/errors"
) )
const ( const jsonFormat = `"` + time.RFC3339Nano + `"`
// RFC3339NanoFixed is our own version of RFC339Nano because we want one
// that pads the nano seconds part with zeros to ensure
// the timestamps are aligned in the logs.
RFC3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
// JSONFormat is the format used by FastMarshalJSON
JSONFormat = `"` + time.RFC3339Nano + `"`
)
// FastTimeMarshalJSON avoids one of the extra allocations that // FastTimeMarshalJSON avoids one of the extra allocations that
// time.MarshalJSON is making. // time.MarshalJSON is making.
@ -23,5 +16,5 @@ func FastTimeMarshalJSON(t time.Time) (string, error) {
// See golang.org/issue/4556#c15 for more discussion. // See golang.org/issue/4556#c15 for more discussion.
return "", errors.New("time.MarshalJSON: year outside of range [0,9999]") return "", errors.New("time.MarshalJSON: year outside of range [0,9999]")
} }
return t.Format(JSONFormat), nil return t.Format(jsonFormat), nil
} }

View file

@ -9,11 +9,14 @@ import (
"time" "time"
gotty "github.com/Nvveen/Gotty" gotty "github.com/Nvveen/Gotty"
"github.com/docker/docker/pkg/jsonlog"
"github.com/docker/docker/pkg/term" "github.com/docker/docker/pkg/term"
units "github.com/docker/go-units" units "github.com/docker/go-units"
) )
// RFC3339NanoFixed is time.RFC3339Nano with nanoseconds padded using zeros to
// ensure the formatted time isalways the same number of characters.
const RFC3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
// JSONError wraps a concrete Code and Message, `Code` is // JSONError wraps a concrete Code and Message, `Code` is
// is an integer error code, `Message` is the error message. // is an integer error code, `Message` is the error message.
type JSONError struct { type JSONError struct {
@ -199,9 +202,9 @@ func (jm *JSONMessage) Display(out io.Writer, termInfo termInfo) error {
return nil return nil
} }
if jm.TimeNano != 0 { if jm.TimeNano != 0 {
fmt.Fprintf(out, "%s ", time.Unix(0, jm.TimeNano).Format(jsonlog.RFC3339NanoFixed)) fmt.Fprintf(out, "%s ", time.Unix(0, jm.TimeNano).Format(RFC3339NanoFixed))
} else if jm.Time != 0 { } else if jm.Time != 0 {
fmt.Fprintf(out, "%s ", time.Unix(jm.Time, 0).Format(jsonlog.RFC3339NanoFixed)) fmt.Fprintf(out, "%s ", time.Unix(jm.Time, 0).Format(RFC3339NanoFixed))
} }
if jm.ID != "" { if jm.ID != "" {
fmt.Fprintf(out, "%s: ", jm.ID) fmt.Fprintf(out, "%s: ", jm.ID)

View file

@ -8,7 +8,6 @@ import (
"testing" "testing"
"time" "time"
"github.com/docker/docker/pkg/jsonlog"
"github.com/docker/docker/pkg/term" "github.com/docker/docker/pkg/term"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -115,8 +114,8 @@ func TestJSONMessageDisplay(t *testing.T) {
From: "From", From: "From",
Status: "status", Status: "status",
}: { }: {
fmt.Sprintf("%v ID: (from From) status\n", time.Unix(now.Unix(), 0).Format(jsonlog.RFC3339NanoFixed)), fmt.Sprintf("%v ID: (from From) status\n", time.Unix(now.Unix(), 0).Format(RFC3339NanoFixed)),
fmt.Sprintf("%v ID: (from From) status\n", time.Unix(now.Unix(), 0).Format(jsonlog.RFC3339NanoFixed)), fmt.Sprintf("%v ID: (from From) status\n", time.Unix(now.Unix(), 0).Format(RFC3339NanoFixed)),
}, },
// General, with nano precision time // General, with nano precision time
{ {
@ -125,8 +124,8 @@ func TestJSONMessageDisplay(t *testing.T) {
From: "From", From: "From",
Status: "status", Status: "status",
}: { }: {
fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(jsonlog.RFC3339NanoFixed)), fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(RFC3339NanoFixed)),
fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(jsonlog.RFC3339NanoFixed)), fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(RFC3339NanoFixed)),
}, },
// General, with both times Nano is preferred // General, with both times Nano is preferred
{ {
@ -136,8 +135,8 @@ func TestJSONMessageDisplay(t *testing.T) {
From: "From", From: "From",
Status: "status", Status: "status",
}: { }: {
fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(jsonlog.RFC3339NanoFixed)), fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(RFC3339NanoFixed)),
fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(jsonlog.RFC3339NanoFixed)), fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(RFC3339NanoFixed)),
}, },
// Stream over status // Stream over status
{ {