Skip to content

Commit

Permalink
chore(trace): ignore format logging if mismatch log level
Browse files Browse the repository at this point in the history
close: cubefs#2611

Signed-off-by: slasher <[email protected]>
  • Loading branch information
sejust committed Sep 18, 2023
1 parent 273e2ea commit 1da2058
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
4 changes: 2 additions & 2 deletions blobstore/common/trace/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,11 @@ const (
)

func (s *spanImpl) output(lvl log.Level, v []interface{}) {
log.DefaultLogger.Output(s.String(), lvl, defaultCalldepth, fmt.Sprintln(v...))
log.DefaultLogger.Output(s.String(), lvl, defaultCalldepth, v...)
}

func (s *spanImpl) outputf(lvl log.Level, format string, v []interface{}) {
log.DefaultLogger.Output(s.String(), lvl, defaultCalldepth, fmt.Sprintf(format, v...))
log.DefaultLogger.Outputf(s.String(), lvl, defaultCalldepth, format, v...)
}

func (s *spanImpl) Println(v ...interface{}) { s.output(log.Linfo, v) }
Expand Down
3 changes: 2 additions & 1 deletion blobstore/util/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ type Logger interface {
GetOutputLevel() Level
SetOutputLevel(logLevel Level)
SetOutput(w io.Writer)
Output(id string, lvl Level, calldepth int, s string) error
Output(id string, lvl Level, calldepth int, a ...interface{}) error
Outputf(id string, lvl Level, calldepth int, format string, a ...interface{}) error

// implement raft Logger with these two function
Warningf(format string, v ...interface{})
Expand Down
29 changes: 21 additions & 8 deletions blobstore/util/log/logext.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,34 @@ func New(out io.Writer, calldepth int) Logger {
return l
}

func (l *logger) Output(id string, lvl Level, calldepth int, s string) error {
func (l *logger) Output(id string, lvl Level, calldepth int, a ...interface{}) error {
if int32(lvl) < atomic.LoadInt32(&l.level) || lvl >= maxLevel {
return nil
}
_, file, line, ok := runtime.Caller(calldepth)
if !ok {
file = "???"
line = 0
}
return l.write(id, lvl, file, line, fmt.Sprintln(a...))
}

now := time.Now()
buf := l.pool.Get().(*bytes.Buffer)

func (l *logger) Outputf(id string, lvl Level, calldepth int, format string, a ...interface{}) error {
if int32(lvl) < atomic.LoadInt32(&l.level) || lvl >= maxLevel {
return nil
}
_, file, line, ok := runtime.Caller(calldepth)
if !ok {
file = "???"
line = 0
}
return l.write(id, lvl, file, line, fmt.Sprintf(format, a...))
}

func (l *logger) write(id string, lvl Level, file string, line int, s string) error {
now := time.Now()
buf := l.pool.Get().(*bytes.Buffer)

buf.Reset()
l.formatOutput(buf, now, file, line, lvl)
if id != "" {
Expand All @@ -86,11 +101,11 @@ func (l *logger) Output(id string, lvl Level, calldepth int, s string) error {
// -----------------------------------------

func (l *logger) outputf(lvl Level, format string, v []interface{}) {
l.Output("", lvl, l.calldepth, fmt.Sprintf(format, v...))
l.Outputf("", lvl, l.calldepth, format, v...)
}

func (l *logger) output(lvl Level, v []interface{}) {
l.Output("", lvl, l.calldepth, fmt.Sprintln(v...))
l.Output("", lvl, l.calldepth, v...)
}

func (l *logger) Printf(format string, v ...interface{}) { l.outputf(Linfo, format, v) }
Expand Down Expand Up @@ -119,14 +134,12 @@ func (l *logger) Fatal(v ...interface{}) {
func (l *logger) Panicf(format string, v ...interface{}) {
s := fmt.Sprintf(format, v...)
l.outputf(Lpanic, format, v)
// l.Output("", Lpanic, l.calldepth, s)
panic(s)
}

func (l *logger) Panic(v ...interface{}) {
s := fmt.Sprintln(v...)
l.output(Lpanic, v)
// l.Output("", Lpanic, l.calldepth, s)
panic(s)
}

Expand Down

0 comments on commit 1da2058

Please sign in to comment.