Merge pull request #11 from tendermint/bugfix/multiple-module-keys
Squash module keyvals if multiple keyvals were provided
This commit is contained in:
commit
dd592a21b9
|
@ -11,6 +11,7 @@ import (
|
||||||
|
|
||||||
const (
|
const (
|
||||||
msgKey = "_msg" // "_" prefixed to avoid collisions
|
msgKey = "_msg" // "_" prefixed to avoid collisions
|
||||||
|
moduleKey = "module"
|
||||||
)
|
)
|
||||||
|
|
||||||
type tmLogger struct {
|
type tmLogger struct {
|
||||||
|
|
|
@ -51,13 +51,15 @@ func (l tmfmtLogger) Log(keyvals ...interface{}) error {
|
||||||
|
|
||||||
lvl := "none"
|
lvl := "none"
|
||||||
msg := "unknown"
|
msg := "unknown"
|
||||||
lvlIndex := -1
|
module := "unknown"
|
||||||
msgIndex := -1
|
|
||||||
|
// indexes of keys to skip while encoding later
|
||||||
|
excludeIndexes := make([]int, 0)
|
||||||
|
|
||||||
for i := 0; i < len(keyvals)-1; i += 2 {
|
for i := 0; i < len(keyvals)-1; i += 2 {
|
||||||
// Extract level
|
// Extract level
|
||||||
if keyvals[i] == kitlevel.Key() {
|
if keyvals[i] == kitlevel.Key() {
|
||||||
lvlIndex = i
|
excludeIndexes = append(excludeIndexes, i)
|
||||||
switch keyvals[i+1].(type) {
|
switch keyvals[i+1].(type) {
|
||||||
case string:
|
case string:
|
||||||
lvl = keyvals[i+1].(string)
|
lvl = keyvals[i+1].(string)
|
||||||
|
@ -66,18 +68,14 @@ func (l tmfmtLogger) Log(keyvals ...interface{}) error {
|
||||||
default:
|
default:
|
||||||
panic(fmt.Sprintf("level value of unknown type %T", keyvals[i+1]))
|
panic(fmt.Sprintf("level value of unknown type %T", keyvals[i+1]))
|
||||||
}
|
}
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// and message
|
// and message
|
||||||
if keyvals[i] == msgKey {
|
} else if keyvals[i] == msgKey {
|
||||||
msgIndex = i
|
excludeIndexes = append(excludeIndexes, i)
|
||||||
msg = keyvals[i+1].(string)
|
msg = keyvals[i+1].(string)
|
||||||
continue
|
// and module (could be multiple keyvals; if such case last keyvalue wins)
|
||||||
}
|
} else if keyvals[i] == moduleKey {
|
||||||
|
excludeIndexes = append(excludeIndexes, i)
|
||||||
if lvlIndex > 0 && msgIndex > 0 { // found all we're looking for
|
module = keyvals[i+1].(string)
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,10 +90,18 @@ func (l tmfmtLogger) Log(keyvals ...interface{}) error {
|
||||||
// Stopping ... - message
|
// Stopping ... - message
|
||||||
enc.buf.WriteString(fmt.Sprintf("%c[%s] %-44s ", lvl[0]-32, time.Now().UTC().Format("01-02|15:04:05.000"), msg))
|
enc.buf.WriteString(fmt.Sprintf("%c[%s] %-44s ", lvl[0]-32, time.Now().UTC().Format("01-02|15:04:05.000"), msg))
|
||||||
|
|
||||||
for i := 0; i < len(keyvals)-1; i += 2 {
|
if module != "unknown" {
|
||||||
if i == lvlIndex || i == msgIndex {
|
enc.buf.WriteString("module=" + module + " ")
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
KeyvalueLoop:
|
||||||
|
for i := 0; i < len(keyvals)-1; i += 2 {
|
||||||
|
for _, j := range excludeIndexes {
|
||||||
|
if i == j {
|
||||||
|
continue KeyvalueLoop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err := enc.EncodeKeyval(keyvals[i], keyvals[i+1]); err != nil {
|
if err := enc.EncodeKeyval(keyvals[i], keyvals[i+1]); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,6 +44,12 @@ func TestTMFmtLogger(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
assert.Regexp(t, regexp.MustCompile(`N\[.+\] Hello \s+\n$`), buf.String())
|
assert.Regexp(t, regexp.MustCompile(`N\[.+\] Hello \s+\n$`), buf.String())
|
||||||
|
|
||||||
|
buf.Reset()
|
||||||
|
if err := logger.Log("module", "main", "module", "crypto", "module", "wire"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
assert.Regexp(t, regexp.MustCompile(`N\[.+\] unknown \s+module=wire\s+\n$`), buf.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkTMFmtLoggerSimple(b *testing.B) {
|
func BenchmarkTMFmtLoggerSimple(b *testing.B) {
|
||||||
|
|
Loading…
Reference in New Issue