From 07b996a0cefc739af27fe84cceabba486223fe37 Mon Sep 17 00:00:00 2001 From: shunsuke maeda Date: Fri, 31 Aug 2018 16:02:30 +0900 Subject: [PATCH] Fix to remove empty line in log --- infrastructure/docker/log.go | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/infrastructure/docker/log.go b/infrastructure/docker/log.go index 3734934c..dbf3c1fb 100644 --- a/infrastructure/docker/log.go +++ b/infrastructure/docker/log.go @@ -25,12 +25,22 @@ type buildLogger struct { } func (l *buildLogger) ReadLine() (*LogLine, error) { - line, _, err := l.reader.ReadLine() - s := &struct { - Stream string `json:"stream"` - }{} - json.NewDecoder(bytes.NewReader(line)).Decode(s) - return &LogLine{Timestamp: clock.Now(), Message: []byte(s.Stream)}, err + for { + line, _, readErr := l.reader.ReadLine() + msg := extractMessage(line) + if readErr == io.EOF { + return &LogLine{Timestamp: clock.Now(), Message: msg}, readErr + } + if readErr != nil { + return nil, errors.WithStack(readErr) + } + + if len(msg) == 0 { + continue + } + + return &LogLine{Timestamp: clock.Now(), Message: msg}, readErr + } } type runLogger struct { @@ -55,6 +65,14 @@ func (l *runLogger) ReadLine() (*LogLine, error) { } } +func extractMessage(line []byte) []byte { + s := &struct { + Stream string `json:"stream"` + }{} + json.NewDecoder(bytes.NewReader(line)).Decode(s) + return []byte(s.Stream) +} + func trimPrefix(line []byte) ([]byte, error) { if len(line) < 8 { return []byte{}, nil