Skip to content

Commit

Permalink
log filter: ignored by V, used during log call
Browse files Browse the repository at this point in the history
It's not entirely clear why the filter was copied into Verbose. The only
semantic difference is when Verbose get stored and then the filter gets
modified:

klogV := klog.V(5)
klog.SetLogFilter(...)
klogV.Info(...)

This seems like a very unlikely code pattern and goes against the intention of
first initializing logging, then using it.

Therefore we can undo the size increase in Verbose and only retrieve the filter
when it is needed.
  • Loading branch information
pohly committed Feb 8, 2022
1 parent 714cb7a commit 150f752
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions klog.go
Expand Up @@ -1395,15 +1395,14 @@ func (l *loggingT) setV(pc uintptr) Level {
type Verbose struct {
enabled bool
logr *logr.Logger
filter LogFilter
}

func newVerbose(level Level, b bool) Verbose {
if logging.logr == nil {
return Verbose{b, nil, logging.filter}
return Verbose{b, nil}
}
v := logging.logr.V(int(level))
return Verbose{b, &v, logging.filter}
return Verbose{b, &v}
}

// V reports whether verbosity at the call site is at least the requested level.
Expand Down Expand Up @@ -1466,55 +1465,55 @@ func (v Verbose) Enabled() bool {
// See the documentation of V for usage.
func (v Verbose) Info(args ...interface{}) {
if v.enabled {
logging.print(infoLog, v.logr, v.filter, args...)
logging.print(infoLog, v.logr, logging.filter, args...)
}
}

// InfoDepth is equivalent to the global InfoDepth function, guarded by the value of v.
// See the documentation of V for usage.
func (v Verbose) InfoDepth(depth int, args ...interface{}) {
if v.enabled {
logging.printDepth(infoLog, v.logr, v.filter, depth, args...)
logging.printDepth(infoLog, v.logr, logging.filter, depth, args...)
}
}

// Infoln is equivalent to the global Infoln function, guarded by the value of v.
// See the documentation of V for usage.
func (v Verbose) Infoln(args ...interface{}) {
if v.enabled {
logging.println(infoLog, v.logr, v.filter, args...)
logging.println(infoLog, v.logr, logging.filter, args...)
}
}

// InfolnDepth is equivalent to the global InfolnDepth function, guarded by the value of v.
// See the documentation of V for usage.
func (v Verbose) InfolnDepth(depth int, args ...interface{}) {
if v.enabled {
logging.printlnDepth(infoLog, v.logr, v.filter, depth, args...)
logging.printlnDepth(infoLog, v.logr, logging.filter, depth, args...)
}
}

// Infof is equivalent to the global Infof function, guarded by the value of v.
// See the documentation of V for usage.
func (v Verbose) Infof(format string, args ...interface{}) {
if v.enabled {
logging.printf(infoLog, v.logr, v.filter, format, args...)
logging.printf(infoLog, v.logr, logging.filter, format, args...)
}
}

// InfofDepth is equivalent to the global InfofDepth function, guarded by the value of v.
// See the documentation of V for usage.
func (v Verbose) InfofDepth(depth int, format string, args ...interface{}) {
if v.enabled {
logging.printfDepth(infoLog, v.logr, v.filter, depth, format, args...)
logging.printfDepth(infoLog, v.logr, logging.filter, depth, format, args...)
}
}

// InfoS is equivalent to the global InfoS function, guarded by the value of v.
// See the documentation of V for usage.
func (v Verbose) InfoS(msg string, keysAndValues ...interface{}) {
if v.enabled {
logging.infoS(v.logr, v.filter, 0, msg, keysAndValues...)
logging.infoS(v.logr, logging.filter, 0, msg, keysAndValues...)
}
}

Expand All @@ -1528,22 +1527,22 @@ func InfoSDepth(depth int, msg string, keysAndValues ...interface{}) {
// See the documentation of V for usage.
func (v Verbose) InfoSDepth(depth int, msg string, keysAndValues ...interface{}) {
if v.enabled {
logging.infoS(v.logr, v.filter, depth, msg, keysAndValues...)
logging.infoS(v.logr, logging.filter, depth, msg, keysAndValues...)
}
}

// Deprecated: Use ErrorS instead.
func (v Verbose) Error(err error, msg string, args ...interface{}) {
if v.enabled {
logging.errorS(err, v.logr, v.filter, 0, msg, args...)
logging.errorS(err, v.logr, logging.filter, 0, msg, args...)
}
}

// ErrorS is equivalent to the global Error function, guarded by the value of v.
// See the documentation of V for usage.
func (v Verbose) ErrorS(err error, msg string, keysAndValues ...interface{}) {
if v.enabled {
logging.errorS(err, v.logr, v.filter, 0, msg, keysAndValues...)
logging.errorS(err, v.logr, logging.filter, 0, msg, keysAndValues...)
}
}

Expand Down

0 comments on commit 150f752

Please sign in to comment.