Skip to content

Commit

Permalink
feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Mar 29, 2023
1 parent 2dbd0a0 commit 2c3139a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 19 deletions.
5 changes: 3 additions & 2 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ type zeroLogWrapper struct {
// NewLogger returns a new logger that writes to the given destination.
//
// Typical usage from a main function is:
// logger := log.NewLogger(os.Stderr)
//
// logger := log.NewLogger(os.Stderr)
//
// Stderr is the typical destination for logs,
// so that any output from your application can still be piped to other processes.
Expand Down Expand Up @@ -123,4 +124,4 @@ func (nopLogger) Info(string, ...any) {}
func (nopLogger) Error(string, ...any) {}
func (nopLogger) Debug(string, ...any) {}
func (nopLogger) With(...any) Logger { return nopLogger{} }
func (nopLogger) Impl() any { return zerolog.Nop() }
func (nopLogger) Impl() any { return nopLogger{} }
2 changes: 2 additions & 0 deletions log/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ func FilterOption(filter FilterFunc) Option {
}

// 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
Expand Down
18 changes: 7 additions & 11 deletions log/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,18 @@ func (fw *filterWriter) Write(p []byte) (n int, err error) {
return fw.parent.Write(p)
}

event := make(map[string]interface{})
if err := json.Unmarshal(p, &event); err != nil {
return 0, fmt.Errorf("failed to unmarshal event: %w", err)
var event struct {
Level string `json:"level"`
Module string `json:"module"`
}

level, ok := event["level"].(string)
if !ok {
return 0, fmt.Errorf("failed to get level from event")
if err := json.Unmarshal(p, &event); err != nil {
return 0, fmt.Errorf("failed to unmarshal event: %w", err)
}

// only filter module keys
module, ok := event[ModuleKey].(string)
if ok {
if fw.filter(module, level) {
return len(p), nil
}
if fw.filter(event.Module, event.Level) {
return len(p), nil
}

return fw.parent.Write(p)
Expand Down
11 changes: 5 additions & 6 deletions log/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@ import (
)

func TestFilteredWriter(t *testing.T) {
checkBuf := new(bytes.Buffer)
buf := new(bytes.Buffer)

level := "consensus:debug,mempool:debug,*:error"
filter, err := log.ParseLogLevel(level)
assert.NilError(t, err)

logger := log.NewLogger(checkBuf, log.FilterOption(filter))
logger := log.NewLogger(buf, log.FilterOption(filter))
logger.Debug("this log line should be displayed", log.ModuleKey, "consensus")
assert.Check(t, strings.Contains(checkBuf.String(), "this log line should be displayed"))
checkBuf.Reset()
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, !strings.Contains(checkBuf.String(), "this log line should be filtered"))
checkBuf.Reset()
assert.Check(t, buf.Len() == 0)
}

0 comments on commit 2c3139a

Please sign in to comment.