Skip to content

Commit f29e67a

Browse files
author
auxten
committed
add PkgDebugLogFilter, if package name exists and value is true, all debug log of this package will be dropped
1 parent f99c7cb commit f29e67a

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

utils/log/logwrapper.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package log
1919
import (
2020
"fmt"
2121
"io"
22+
"io/ioutil"
2223
"path/filepath"
2324
"runtime"
2425
"strings"
@@ -45,6 +46,12 @@ const (
4546
DebugLevel
4647
)
4748

49+
// PkgDebugLogFilter is the log filter
50+
// if package name exists and value is true, all debug log of this package will be dropped
51+
var PkgDebugLogFilter = map[string]bool{
52+
"metric": true,
53+
}
54+
4855
// Logger wraps logrus logger type.
4956
type Logger logrus.Logger
5057

@@ -56,7 +63,19 @@ type CallerHook struct{}
5663

5764
// Fire defines hook event handler.
5865
func (hook *CallerHook) Fire(entry *logrus.Entry) error {
59-
entry.Data["caller"] = hook.caller()
66+
funcDesc, caller := hook.caller()
67+
if entry.Level == DebugLevel {
68+
fields := strings.SplitN(funcDesc, ".", 2)
69+
if len(fields) > 0 {
70+
var isFiltered bool
71+
isFiltered, _ = PkgDebugLogFilter[fields[0]]
72+
if isFiltered {
73+
entry.Logger.Out = ioutil.Discard
74+
return nil
75+
}
76+
}
77+
}
78+
entry.Data["caller"] = caller
6079
return nil
6180
}
6281

@@ -72,7 +91,7 @@ func (hook *CallerHook) Levels() []logrus.Level {
7291
}
7392
}
7493

75-
func (hook *CallerHook) caller() string {
94+
func (hook *CallerHook) caller() (relFuncName, caller string) {
7695
var (
7796
file = "unknown"
7897
line = 0
@@ -86,9 +105,9 @@ func (hook *CallerHook) caller() string {
86105
funcName = details.Name()
87106
}
88107

89-
relFuncName := strings.TrimPrefix(funcName, "gitlab.com/thunderdb/ThunderDB/")
108+
relFuncName = strings.TrimPrefix(funcName, "gitlab.com/thunderdb/ThunderDB/")
90109
funcLocation := fmt.Sprintf("%s:%d %s", filepath.Base(file), line, relFuncName)
91-
return funcLocation
110+
return relFuncName, funcLocation
92111
}
93112

94113
func init() {

0 commit comments

Comments
 (0)