log: Do not pad too long values

This commit is contained in:
Nikifor Seryakov 2019-05-17 14:20:43 +03:00 committed by GitHub
parent 509facd631
commit a114d4d1fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -18,6 +18,7 @@ const (
termTimeFormat = "01-02|15:04:05.000" termTimeFormat = "01-02|15:04:05.000"
floatFormat = 'f' floatFormat = 'f'
termMsgJust = 40 termMsgJust = 40
termCtxMaxPadding = 40
) )
// locationTrims are trimmed for display to avoid unwieldy log lines. // locationTrims are trimmed for display to avoid unwieldy log lines.
@ -175,7 +176,7 @@ func logfmt(buf *bytes.Buffer, ctx []interface{}, color int, term bool) {
fieldPaddingLock.RUnlock() fieldPaddingLock.RUnlock()
length := utf8.RuneCountInString(v) length := utf8.RuneCountInString(v)
if padding < length { if padding < length && length <= termCtxMaxPadding {
padding = length padding = length
fieldPaddingLock.Lock() fieldPaddingLock.Lock()
@ -189,7 +190,7 @@ func logfmt(buf *bytes.Buffer, ctx []interface{}, color int, term bool) {
buf.WriteByte('=') buf.WriteByte('=')
} }
buf.WriteString(v) buf.WriteString(v)
if i < len(ctx)-2 { if i < len(ctx)-2 && padding > length {
buf.Write(bytes.Repeat([]byte{' '}, padding-length)) buf.Write(bytes.Repeat([]byte{' '}, padding-length))
} }
} }