Skip to content

Commit

Permalink
log: not pad too long values (ethereum#19592)
Browse files Browse the repository at this point in the history
  • Loading branch information
gzliudan committed Nov 12, 2024
1 parent cb2d604 commit 8ac7741
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions log/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ import (
)

const (
timeFormat = "2006-01-02T15:04:05-0700"
termTimeFormat = "01-02|15:04:05"
floatFormat = 'f'
termMsgJust = 40
timeFormat = "2006-01-02T15:04:05-0700"
termTimeFormat = "01-02|15:04:05"
floatFormat = 'f'
termMsgJust = 40
termCtxMaxPadding = 40
)

// locationTrims are trimmed for display to avoid unwieldy log lines.
Expand Down Expand Up @@ -77,12 +78,11 @@ type TerminalStringer interface {
// a terminal with color-coded level output and terser human friendly timestamp.
// This format should only be used for interactive programs or while developing.
//
// [TIME] [LEVEL] MESAGE key=value key=value ...
// [TIME] [LEVEL] MESAGE key=value key=value ...
//
// Example:
//
// [May 16 20:58:45] [DBUG] remove route ns=haproxy addr=127.0.0.1:50002
//
// [May 16 20:58:45] [DBUG] remove route ns=haproxy addr=127.0.0.1:50002
func TerminalFormat(usecolor bool) Format {
return FormatFunc(func(r *Record) []byte {
var color = 0
Expand Down Expand Up @@ -147,7 +147,6 @@ func TerminalFormat(usecolor bool) Format {
// format for key/value pairs.
//
// For more details see: http://godoc.org/github.com/kr/logfmt
//
func LogfmtFormat() Format {
return FormatFunc(func(r *Record) []byte {
common := []interface{}{r.KeyNames.Time, r.Time, r.KeyNames.Lvl, r.Lvl, r.KeyNames.Msg, r.Msg}
Expand Down Expand Up @@ -175,7 +174,7 @@ func logfmt(buf *bytes.Buffer, ctx []interface{}, color int, term bool) {
fieldPaddingLock.RUnlock()

length := utf8.RuneCountInString(v)
if padding < length {
if padding < length && length <= termCtxMaxPadding {
padding = length

fieldPaddingLock.Lock()
Expand All @@ -189,7 +188,7 @@ func logfmt(buf *bytes.Buffer, ctx []interface{}, color int, term bool) {
buf.WriteByte('=')
}
buf.WriteString(v)
if i < len(ctx)-2 {
if i < len(ctx)-2 && padding > length {
buf.Write(bytes.Repeat([]byte{' '}, padding-length))
}
}
Expand Down

0 comments on commit 8ac7741

Please sign in to comment.