Browse Source

return back initially allowed level if we encounter allowed key (#2889)

Fixes #2868 where module=main setting overrides all others
pull/2919/head
Anton Kaliaev 6 years ago
committed by Ethan Buchman
parent
commit
98e442a8de
3 changed files with 41 additions and 7 deletions
  1. +1
    -0
      CHANGELOG_PENDING.md
  2. +1
    -1
      libs/cli/flags/log_level_test.go
  3. +39
    -6
      libs/log/filter.go

+ 1
- 0
CHANGELOG_PENDING.md View File

@ -32,5 +32,6 @@ program](https://hackerone.com/tendermint).
### BUG FIXES:
- [log] \#2868 fix module=main setting overriding all others
- [rpc] \#2808 RPC validators calls IncrementAccum if necessary
- [rpc] \#2811 Allow integer IDs in JSON-RPC requests

+ 1
- 1
libs/cli/flags/log_level_test.go View File

@ -51,7 +51,7 @@ func TestParseLogLevel(t *testing.T) {
buf.Reset()
logger.With("module", "wire").Debug("Kingpin")
logger.With("module", "mempool").With("module", "wire").Debug("Kingpin")
if have := strings.TrimSpace(buf.String()); c.expectedLogLines[0] != have {
t.Errorf("\nwant '%s'\nhave '%s'\nlevel '%s'", c.expectedLogLines[0], have, c.lvl)
}


+ 39
- 6
libs/log/filter.go View File

@ -11,9 +11,10 @@ const (
)
type filter struct {
next Logger
allowed level // XOR'd levels for default case
allowedKeyvals map[keyval]level // When key-value match, use this level
next Logger
allowed level // XOR'd levels for default case
initiallyAllowed level // XOR'd levels for initial case
allowedKeyvals map[keyval]level // When key-value match, use this level
}
type keyval struct {
@ -33,6 +34,7 @@ func NewFilter(next Logger, options ...Option) Logger {
for _, option := range options {
option(l)
}
l.initiallyAllowed = l.allowed
return l
}
@ -76,14 +78,45 @@ func (l *filter) Error(msg string, keyvals ...interface{}) {
// logger = log.NewFilter(logger, log.AllowError(), log.AllowInfoWith("module", "crypto"), log.AllowNoneWith("user", "Sam"))
// logger.With("user", "Sam").With("module", "crypto").Info("Hello") # produces "I... Hello module=crypto user=Sam"
func (l *filter) With(keyvals ...interface{}) Logger {
keyInAllowedKeyvals := false
for i := len(keyvals) - 2; i >= 0; i -= 2 {
for kv, allowed := range l.allowedKeyvals {
if keyvals[i] == kv.key && keyvals[i+1] == kv.value {
return &filter{next: l.next.With(keyvals...), allowed: allowed, allowedKeyvals: l.allowedKeyvals}
if keyvals[i] == kv.key {
keyInAllowedKeyvals = true
// Example:
// logger = log.NewFilter(logger, log.AllowError(), log.AllowInfoWith("module", "crypto"))
// logger.With("module", "crypto")
if keyvals[i+1] == kv.value {
return &filter{
next: l.next.With(keyvals...),
allowed: allowed, // set the desired level
allowedKeyvals: l.allowedKeyvals,
initiallyAllowed: l.initiallyAllowed,
}
}
}
}
}
return &filter{next: l.next.With(keyvals...), allowed: l.allowed, allowedKeyvals: l.allowedKeyvals}
// Example:
// logger = log.NewFilter(logger, log.AllowError(), log.AllowInfoWith("module", "crypto"))
// logger.With("module", "main")
if keyInAllowedKeyvals {
return &filter{
next: l.next.With(keyvals...),
allowed: l.initiallyAllowed, // return back to initially allowed
allowedKeyvals: l.allowedKeyvals,
initiallyAllowed: l.initiallyAllowed,
}
}
return &filter{
next: l.next.With(keyvals...),
allowed: l.allowed, // simply continue with the current level
allowedKeyvals: l.allowedKeyvals,
initiallyAllowed: l.initiallyAllowed,
}
}
//--------------------------------------------------------------------------------


Loading…
Cancel
Save