@@ -122,17 +122,29 @@ func redirectLogs(path string, rc io.ReadCloser, w io.Writer, s StreamType, maxL
122122 buf [][]byte
123123 length int
124124 bufSize = defaultBufSize
125+
126+ timeBuffer = make ([]byte , len (timestampFormat ))
127+ lineBuffer = bytes.Buffer {}
125128 )
126129 // Make sure bufSize <= maxLen
127130 if maxLen > 0 && maxLen < bufSize {
128131 bufSize = maxLen
129132 }
130133 r := bufio .NewReaderSize (rc , bufSize )
131- writeLine := func (tag , line []byte ) {
132- timestamp := time .Now ().AppendFormat (nil , timestampFormat )
133- data := bytes .Join ([][]byte {timestamp , stream , tag , line }, delimiter )
134- data = append (data , eol )
135- if _ , err := w .Write (data ); err != nil {
134+ writeLineBuffer := func (tag []byte , lineBytes [][]byte ) {
135+ timeBuffer = time .Now ().AppendFormat (timeBuffer [:0 ], timestampFormat )
136+ headers := [][]byte {timeBuffer , stream , tag }
137+
138+ lineBuffer .Reset ()
139+ for _ , h := range headers {
140+ lineBuffer .Write (h )
141+ lineBuffer .Write (delimiter )
142+ }
143+ for _ , l := range lineBytes {
144+ lineBuffer .Write (l )
145+ }
146+ lineBuffer .WriteByte (eol )
147+ if _ , err := lineBuffer .WriteTo (w ); err != nil {
136148 logrus .WithError (err ).Errorf ("Fail to write %q log to log file %q" , s , path )
137149 // Continue on write error to drain the container output.
138150 }
@@ -171,7 +183,7 @@ func redirectLogs(path string, rc io.ReadCloser, w io.Writer, s StreamType, maxL
171183 panic ("exceed length should <= last buffer size" )
172184 }
173185 buf [len (buf )- 1 ] = last [:len (last )- exceedLen ]
174- writeLine (partial , bytes . Join ( buf , nil ) )
186+ writeLineBuffer (partial , buf )
175187 buf = [][]byte {last [len (last )- exceedLen :]}
176188 length = exceedLen
177189 }
@@ -182,9 +194,9 @@ func redirectLogs(path string, rc io.ReadCloser, w io.Writer, s StreamType, maxL
182194 // readLine only returns error when the message doesn't
183195 // end with a newline, in that case it should be treated
184196 // as a partial line.
185- writeLine (partial , bytes . Join ( buf , nil ) )
197+ writeLineBuffer (partial , buf )
186198 } else {
187- writeLine (full , bytes . Join ( buf , nil ) )
199+ writeLineBuffer (full , buf )
188200 }
189201 buf = nil
190202 length = 0
0 commit comments