-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(log): wire logger filtering (#15601)
- Loading branch information
1 parent
f3110e9
commit d21f58c
Showing
18 changed files
with
170 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package log | ||
|
||
import "github.com/rs/zerolog" | ||
|
||
// defaultConfig has all the options disabled. | ||
var defaultConfig = Config{ | ||
Level: zerolog.NoLevel, | ||
Filter: nil, | ||
OutputJSON: false, | ||
} | ||
|
||
// LoggerConfig defines configuration for the logger. | ||
type Config struct { | ||
Level zerolog.Level | ||
Filter FilterFunc | ||
OutputJSON bool | ||
} | ||
|
||
type Option func(*Config) | ||
|
||
// FilterOption sets the filter for the Logger. | ||
func FilterOption(filter FilterFunc) Option { | ||
return func(cfg *Config) { | ||
cfg.Filter = filter | ||
} | ||
} | ||
|
||
// LevelOption sets the level for the Logger. | ||
// Messages with a lower level will be discarded. | ||
func LevelOption(level zerolog.Level) Option { | ||
return func(cfg *Config) { | ||
cfg.Level = level | ||
} | ||
} | ||
|
||
// OutputJSONOption sets the output of the logger to JSON. | ||
// By default, the logger outputs to a human-readable format. | ||
func OutputJSONOption() Option { | ||
return func(cfg *Config) { | ||
cfg.OutputJSON = true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package log | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
) | ||
|
||
// NewFilterWriter returns a writer that filters out all key/value pairs that do not match the filter. | ||
// If the filter is nil, the writer will pass all events through. | ||
// The filter function is called with the module and level of the event. | ||
func NewFilterWriter(parent io.Writer, filter FilterFunc) io.Writer { | ||
return &filterWriter{parent, filter} | ||
} | ||
|
||
type filterWriter struct { | ||
parent io.Writer | ||
filter FilterFunc | ||
} | ||
|
||
func (fw *filterWriter) Write(p []byte) (n int, err error) { | ||
if fw.filter == nil { | ||
return fw.parent.Write(p) | ||
} | ||
|
||
var event struct { | ||
Level string `json:"level"` | ||
Module string `json:"module"` | ||
} | ||
|
||
if err := json.Unmarshal(p, &event); err != nil { | ||
return 0, fmt.Errorf("failed to unmarshal event: %w", err) | ||
} | ||
|
||
// only filter module keys | ||
if fw.filter(event.Module, event.Level) { | ||
return len(p), nil | ||
} | ||
|
||
return fw.parent.Write(p) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package log_test | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
"testing" | ||
|
||
"cosmossdk.io/log" | ||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func TestFilteredWriter(t *testing.T) { | ||
buf := new(bytes.Buffer) | ||
|
||
level := "consensus:debug,mempool:debug,*:error" | ||
filter, err := log.ParseLogLevel(level) | ||
assert.NilError(t, err) | ||
|
||
logger := log.NewLogger(buf, log.FilterOption(filter)) | ||
logger.Debug("this log line should be displayed", log.ModuleKey, "consensus") | ||
assert.Check(t, strings.Contains(buf.String(), "this log line should be displayed")) | ||
buf.Reset() | ||
|
||
logger.Debug("this log line should be filtered", log.ModuleKey, "server") | ||
assert.Check(t, buf.Len() == 0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters