aboutsummaryrefslogtreecommitdiffstats
path: root/log/format.go
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2017-02-28 21:36:51 +0800
committerPéter Szilágyi <peterke@gmail.com>2017-02-28 21:36:51 +0800
commite02883c0a2a9aa70de6b80413ca40dca833c4946 (patch)
treea722ade4f0a3ad46ad4e2eb3a87dfd5915c77e05 /log/format.go
parente588e0ca2b3b615af0ecfd5679c42df8f1cc4272 (diff)
downloaddexon-e02883c0a2a9aa70de6b80413ca40dca833c4946.tar.gz
dexon-e02883c0a2a9aa70de6b80413ca40dca833c4946.tar.zst
dexon-e02883c0a2a9aa70de6b80413ca40dca833c4946.zip
core, log: track field length and pad to align
Diffstat (limited to 'log/format.go')
-rw-r--r--log/format.go28
1 files changed, 25 insertions, 3 deletions
diff --git a/log/format.go b/log/format.go
index e8f4b4f24..c44ea8ed4 100644
--- a/log/format.go
+++ b/log/format.go
@@ -10,6 +10,7 @@ import (
"sync"
"sync/atomic"
"time"
+ "unicode/utf8"
)
const (
@@ -43,6 +44,13 @@ var locationEnabled uint32
// padded to to aid in alignment.
var locationLength uint32
+// fieldPadding is a global map with maximum field value lengths seen until now
+// to allow padding log contexts in a bit smarter way.
+var fieldPadding = make(map[string]int)
+
+// fieldPaddingLock is a global mutex protecting the field padding map.
+var fieldPaddingLock sync.RWMutex
+
type Format interface {
Format(r *Record) []byte
}
@@ -163,15 +171,29 @@ func logfmt(buf *bytes.Buffer, ctx []interface{}, color int, term bool) {
}
// XXX: we should probably check that all of your key bytes aren't invalid
+ fieldPaddingLock.RLock()
+ padding := fieldPadding[k]
+ fieldPaddingLock.RUnlock()
+
+ length := utf8.RuneCountInString(v)
+ if padding < length {
+ padding = length
+
+ fieldPaddingLock.Lock()
+ fieldPadding[k] = padding
+ fieldPaddingLock.Unlock()
+ }
if color > 0 {
- fmt.Fprintf(buf, "\x1b[%dm%s\x1b[0m=%s", color, k, v)
+ fmt.Fprintf(buf, "\x1b[%dm%s\x1b[0m=", color, k)
} else {
buf.WriteString(k)
buf.WriteByte('=')
- buf.WriteString(v)
+ }
+ buf.WriteString(v)
+ if i < len(ctx)-2 {
+ buf.Write(bytes.Repeat([]byte{' '}, padding-length))
}
}
-
buf.WriteByte('\n')
}