Skip to content

Commit

Permalink
feat(mobile): Add filtered log core
Browse files Browse the repository at this point in the history
  • Loading branch information
gfanton committed Oct 18, 2018
1 parent c9ef00f commit cda3f0b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 28 deletions.
11 changes: 7 additions & 4 deletions client/react-native/gomobile/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core
import (
"fmt"

"berty.tech/core/pkg/filteredzap"
p2plog "github.com/ipfs/go-log"
"github.com/whyrusleeping/go-logging"
"go.uber.org/zap"
Expand Down Expand Up @@ -111,12 +112,14 @@ func setupLogger(logLevel string, mlogger Logger) error {
return err
}

consoleEncoder := zapcore.NewConsoleEncoder(config.EncoderConfig)
mobileCore := zap.WrapCore(func(core zapcore.Core) zapcore.Core {
return newMobileCore(core, consoleEncoder, mlogger)
core := zap.WrapCore(func(core zapcore.Core) zapcore.Core {
consoleEncoder := zapcore.NewConsoleEncoder(config.EncoderConfig)
mobileCore := newMobileCore(core, consoleEncoder, mlogger)
filteredCore := filteredzap.FilterByNamespace(mobileCore, "*")
return filteredCore
})

zap.ReplaceGlobals(l.WithOptions(mobileCore))
zap.ReplaceGlobals(l.WithOptions(core))
logger().Debug("logger initialized")

// configure p2p log
Expand Down
31 changes: 7 additions & 24 deletions core/cmd/berty/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"fmt"
"math/rand"
"path"
"strings"

p2plog "github.com/ipfs/go-log"
Expand Down Expand Up @@ -128,30 +127,14 @@ func setupLogger(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}

patternsString, err := cmd.Flags().GetString("log-namespaces")
if err != nil {
panic(err)
}

filtered := zap.WrapCore(func(core zapcore.Core) zapcore.Core {
matchMap := map[string]bool{}
patternsString, err := cmd.Flags().GetString("log-namespaces")
if err != nil {
panic(err)
}
patterns := strings.Split(patternsString, ",")
return filteredzap.NewFilteringCore(core, func(entry zapcore.Entry, fields []zapcore.Field) bool {
// always print error messages
if entry.Level >= zapcore.ErrorLevel {
return true
}
// only show debug,info,warn messages for enabled --log-namespaces
if _, found := matchMap[entry.LoggerName]; !found {
matchMap[entry.LoggerName] = false
for _, pattern := range patterns {
if matched, _ := path.Match(pattern, entry.LoggerName); matched {
matchMap[entry.LoggerName] = true
break
}
}
}
return matchMap[entry.LoggerName]
})
return filteredzap.FilterByNamespace(core, patternsString)
})
zap.ReplaceGlobals(l.WithOptions(filtered))
logger().Debug("logger initialized")
Expand Down
26 changes: 26 additions & 0 deletions core/pkg/filteredzap/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package filteredzap
// based on https://github.com/tchap/zapext/blob/a5d992351555de581c6d36e2829287a6d1c2f16a/middleware/core_filtering.go

import (
"path"
"strings"

"go.uber.org/zap/zapcore"
)

Expand Down Expand Up @@ -35,3 +38,26 @@ func (core *filteringCore) Write(entry zapcore.Entry, fields []zapcore.Field) er
}
return core.Core.Write(entry, fields)
}

func FilterByNamespace(core zapcore.Core, namespaces string) zapcore.Core {
matchMap := map[string]bool{}
patternsString := namespaces
patterns := strings.Split(patternsString, ",")
return NewFilteringCore(core, func(entry zapcore.Entry, fields []zapcore.Field) bool {
// always print error messages
if entry.Level >= zapcore.ErrorLevel {
return true
}
// only show debug,info,warn messages for enabled --log-namespaces
if _, found := matchMap[entry.LoggerName]; !found {
matchMap[entry.LoggerName] = false
for _, pattern := range patterns {
if matched, _ := path.Match(pattern, entry.LoggerName); matched {
matchMap[entry.LoggerName] = true
break
}
}
}
return matchMap[entry.LoggerName]
})
}

0 comments on commit cda3f0b

Please sign in to comment.