Skip to content

Commit

Permalink
Switching to logr tag v1.0.0
Browse files Browse the repository at this point in the history
Signed-off-by: Davanum Srinivas <davanum@gmail.com>
  • Loading branch information
dims committed Aug 3, 2021
1 parent 79f9f6b commit 4171f3c
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 140 deletions.
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -2,4 +2,4 @@ module k8s.io/klog/v2

go 1.13

require github.com/go-logr/logr v0.4.0
require github.com/go-logr/logr v1.0.0
6 changes: 4 additions & 2 deletions go.sum
@@ -1,2 +1,4 @@
github.com/go-logr/logr v0.4.0 h1:K7/B1jt6fIBQVd4Owv2MqGQClcgf0R266+7C/QjRcLc=
github.com/go-logr/logr v0.4.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU=
github.com/go-logr/logr v1.0.0-rc1 h1:+ul9F74rBkPajeP8m4o3o0tiglmzNFsPnuhYyBCQ0Sc=
github.com/go-logr/logr v1.0.0-rc1/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU=
github.com/go-logr/logr v1.0.0 h1:kH951GinvFVaQgy/ki/B3YYmQtRpExGigSJg6O8z5jo=
github.com/go-logr/logr v1.0.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
66 changes: 37 additions & 29 deletions klog.go
Expand Up @@ -509,7 +509,7 @@ type loggingT struct {
addDirHeader bool

// If set, all output will be redirected unconditionally to the provided logr.Logger
logr logr.Logger
logr *logr.Logger

// If true, messages will not be propagated to lower severity log levels
oneOutput bool
Expand Down Expand Up @@ -698,30 +698,30 @@ func (buf *buffer) someDigits(i, d int) int {
return copy(buf.tmp[i:], buf.tmp[j:])
}

func (l *loggingT) println(s severity, logr logr.Logger, filter LogFilter, args ...interface{}) {
func (l *loggingT) println(s severity, logger *logr.Logger, filter LogFilter, args ...interface{}) {
buf, file, line := l.header(s, 0)
// if logr is set, we clear the generated header as we rely on the backing
// logr implementation to print headers
if logr != nil {
// if logger is set, we clear the generated header as we rely on the backing
// logger implementation to print headers
if logger != nil {
l.putBuffer(buf)
buf = l.getBuffer()
}
if filter != nil {
args = filter.Filter(args)
}
fmt.Fprintln(buf, args...)
l.output(s, logr, buf, 0 /* depth */, file, line, false)
l.output(s, logger, buf, 0 /* depth */, file, line, false)
}

func (l *loggingT) print(s severity, logr logr.Logger, filter LogFilter, args ...interface{}) {
l.printDepth(s, logr, filter, 1, args...)
func (l *loggingT) print(s severity, logger *logr.Logger, filter LogFilter, args ...interface{}) {
l.printDepth(s, logger, filter, 1, args...)
}

func (l *loggingT) printDepth(s severity, logr logr.Logger, filter LogFilter, depth int, args ...interface{}) {
func (l *loggingT) printDepth(s severity, logger *logr.Logger, filter LogFilter, depth int, args ...interface{}) {
buf, file, line := l.header(s, depth)
// if logr is set, we clear the generated header as we rely on the backing
// logr implementation to print headers
if logr != nil {
if logger != nil {
l.putBuffer(buf)
buf = l.getBuffer()
}
Expand All @@ -732,14 +732,14 @@ func (l *loggingT) printDepth(s severity, logr logr.Logger, filter LogFilter, de
if buf.Bytes()[buf.Len()-1] != '\n' {
buf.WriteByte('\n')
}
l.output(s, logr, buf, depth, file, line, false)
l.output(s, logger, buf, depth, file, line, false)
}

func (l *loggingT) printf(s severity, logr logr.Logger, filter LogFilter, format string, args ...interface{}) {
func (l *loggingT) printf(s severity, logger *logr.Logger, filter LogFilter, format string, args ...interface{}) {
buf, file, line := l.header(s, 0)
// if logr is set, we clear the generated header as we rely on the backing
// logr implementation to print headers
if logr != nil {
if logger != nil {
l.putBuffer(buf)
buf = l.getBuffer()
}
Expand All @@ -750,17 +750,17 @@ func (l *loggingT) printf(s severity, logr logr.Logger, filter LogFilter, format
if buf.Bytes()[buf.Len()-1] != '\n' {
buf.WriteByte('\n')
}
l.output(s, logr, buf, 0 /* depth */, file, line, false)
l.output(s, logger, buf, 0 /* depth */, file, line, false)
}

// printWithFileLine behaves like print but uses the provided file and line number. If
// alsoLogToStderr is true, the log message always appears on standard error; it
// will also appear in the log file unless --logtostderr is set.
func (l *loggingT) printWithFileLine(s severity, logr logr.Logger, filter LogFilter, file string, line int, alsoToStderr bool, args ...interface{}) {
func (l *loggingT) printWithFileLine(s severity, logger *logr.Logger, filter LogFilter, file string, line int, alsoToStderr bool, args ...interface{}) {
buf := l.formatHeader(s, file, line)
// if logr is set, we clear the generated header as we rely on the backing
// logr implementation to print headers
if logr != nil {
if logger != nil {
l.putBuffer(buf)
buf = l.getBuffer()
}
Expand All @@ -771,28 +771,28 @@ func (l *loggingT) printWithFileLine(s severity, logr logr.Logger, filter LogFil
if buf.Bytes()[buf.Len()-1] != '\n' {
buf.WriteByte('\n')
}
l.output(s, logr, buf, 2 /* depth */, file, line, alsoToStderr)
l.output(s, logger, buf, 2 /* depth */, file, line, alsoToStderr)
}

// if loggr is specified, will call loggr.Error, otherwise output with logging module.
func (l *loggingT) errorS(err error, loggr logr.Logger, filter LogFilter, depth int, msg string, keysAndValues ...interface{}) {
func (l *loggingT) errorS(err error, logger *logr.Logger, filter LogFilter, depth int, msg string, keysAndValues ...interface{}) {
if filter != nil {
msg, keysAndValues = filter.FilterS(msg, keysAndValues)
}
if loggr != nil {
logr.WithCallDepth(loggr, depth+2).Error(err, msg, keysAndValues...)
if logger != nil {
logger.WithCallDepth(depth+2).Error(err, msg, keysAndValues...)
return
}
l.printS(err, errorLog, depth+1, msg, keysAndValues...)
}

// if loggr is specified, will call loggr.Info, otherwise output with logging module.
func (l *loggingT) infoS(loggr logr.Logger, filter LogFilter, depth int, msg string, keysAndValues ...interface{}) {
func (l *loggingT) infoS(logger *logr.Logger, filter LogFilter, depth int, msg string, keysAndValues ...interface{}) {
if filter != nil {
msg, keysAndValues = filter.FilterS(msg, keysAndValues)
}
if loggr != nil {
logr.WithCallDepth(loggr, depth+2).Info(msg, keysAndValues...)
if logger != nil {
logger.WithCallDepth(depth+2).Info(msg, keysAndValues...)
return
}
l.printS(nil, infoLog, depth+1, msg, keysAndValues...)
Expand Down Expand Up @@ -866,7 +866,14 @@ func SetLogger(logr logr.Logger) {
logging.mu.Lock()
defer logging.mu.Unlock()

logging.logr = logr
logging.logr = &logr
}

func clearLogger() {
logging.mu.Lock()
defer logging.mu.Unlock()

logging.logr = nil
}

// SetOutput sets the output destination for all severities
Expand Down Expand Up @@ -904,7 +911,7 @@ func LogToStderr(stderr bool) {
}

// output writes the data to the log files and releases the buffer.
func (l *loggingT) output(s severity, log logr.Logger, buf *buffer, depth int, file string, line int, alsoToStderr bool) {
func (l *loggingT) output(s severity, log *logr.Logger, buf *buffer, depth int, file string, line int, alsoToStderr bool) {
l.mu.Lock()
if l.traceLocation.isSet() {
if l.traceLocation.match(file, line) {
Expand All @@ -916,9 +923,9 @@ func (l *loggingT) output(s severity, log logr.Logger, buf *buffer, depth int, f
// TODO: set 'severity' and caller information as structured log info
// keysAndValues := []interface{}{"severity", severityName[s], "file", file, "line", line}
if s == errorLog {
logr.WithCallDepth(l.logr, depth+3).Error(nil, string(data))
l.logr.WithCallDepth(depth+3).Error(nil, string(data))
} else {
logr.WithCallDepth(log, depth+3).Info(string(data))
log.WithCallDepth(depth + 3).Info(string(data))
}
} else if l.toStderr {
os.Stderr.Write(data)
Expand Down Expand Up @@ -1269,15 +1276,16 @@ func (l *loggingT) setV(pc uintptr) Level {
// See the documentation of V for more information.
type Verbose struct {
enabled bool
logr logr.Logger
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, logging.logr.V(int(level)), logging.filter}
v := logging.logr.V(int(level))
return Verbose{b, &v, logging.filter}
}

// V reports whether verbosity at the call site is at least the requested level.
Expand Down
54 changes: 33 additions & 21 deletions klog_test.go
Expand Up @@ -446,7 +446,7 @@ func testVmoduleGlob(pat string, match bool, t *testing.T) {
defer logging.vmodule.Set("")
logging.vmodule.Set(pat)
if V(2).Enabled() != match {
t.Errorf("incorrect match for %q: got %t expected %t", pat, V(2), match)
t.Errorf("incorrect match for %q: got %#v expected %#v", pat, V(2), match)
}
}

Expand Down Expand Up @@ -1375,8 +1375,9 @@ func TestInfoSWithLogr(t *testing.T) {

for _, data := range testDataInfo {
t.Run(data.msg, func(t *testing.T) {
SetLogger(logger)
defer SetLogger(nil)
l := logr.New(logger)
SetLogger(l)
defer clearLogger()
defer logger.reset()

InfoS(data.msg, data.keysValues...)
Expand Down Expand Up @@ -1442,8 +1443,9 @@ func TestErrorSWithLogr(t *testing.T) {

for _, data := range testDataInfo {
t.Run(data.msg, func(t *testing.T) {
SetLogger(logger)
defer SetLogger(nil)
l := logr.New(logger)
SetLogger(l)
defer clearLogger()
defer logger.reset()

ErrorS(data.err, data.msg, data.keysValues...)
Expand Down Expand Up @@ -1499,8 +1501,9 @@ func TestCallDepthLogr(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
SetLogger(logger)
defer SetLogger(nil)
l := logr.New(logger)
SetLogger(l)
defer clearLogger()
defer logger.reset()
defer logger.resetCallDepth()

Expand All @@ -1520,7 +1523,8 @@ func TestCallDepthLogr(t *testing.T) {
func TestCallDepthLogrInfoS(t *testing.T) {
logger := &callDepthTestLogr{}
logger.resetCallDepth()
SetLogger(logger)
l := logr.New(logger)
SetLogger(l)

// Add wrapper to ensure callDepthTestLogr +2 offset is correct.
logFunc := func() {
Expand All @@ -1541,7 +1545,8 @@ func TestCallDepthLogrInfoS(t *testing.T) {
func TestCallDepthLogrErrorS(t *testing.T) {
logger := &callDepthTestLogr{}
logger.resetCallDepth()
SetLogger(logger)
l := logr.New(logger)
SetLogger(l)

// Add wrapper to ensure callDepthTestLogr +2 offset is correct.
logFunc := func() {
Expand All @@ -1562,7 +1567,8 @@ func TestCallDepthLogrErrorS(t *testing.T) {
func TestCallDepthLogrGoLog(t *testing.T) {
logger := &callDepthTestLogr{}
logger.resetCallDepth()
SetLogger(logger)
l := logr.New(logger)
SetLogger(l)
CopyStandardLogTo("INFO")

// Add wrapper to ensure callDepthTestLogr +2 offset is correct.
Expand All @@ -1588,7 +1594,7 @@ func TestCallDepthTestLogr(t *testing.T) {
logger.resetCallDepth()

logFunc := func() {
logger.Info("some info log")
logger.Info(0, "some info log")
}
// Keep these lines together.
_, wantFile, wantLine, _ := runtime.Caller(0)
Expand Down Expand Up @@ -1634,7 +1640,7 @@ func (l *testLogr) reset() {
l.entries = []testLogrEntry{}
}

func (l *testLogr) Info(msg string, keysAndValues ...interface{}) {
func (l *testLogr) Info(level int, msg string, keysAndValues ...interface{}) {
l.mutex.Lock()
defer l.mutex.Unlock()
l.entries = append(l.entries, testLogrEntry{
Expand All @@ -1655,12 +1661,15 @@ func (l *testLogr) Error(err error, msg string, keysAndValues ...interface{}) {
})
}

func (l *testLogr) Enabled() bool { panic("not implemented") }
func (l *testLogr) V(int) logr.Logger { panic("not implemented") }
func (l *testLogr) WithName(string) logr.Logger { panic("not implemented") }
func (l *testLogr) WithValues(...interface{}) logr.Logger {
panic("not implemented")
}
func (l *testLogr) Init(info logr.RuntimeInfo) {}
func (l *testLogr) Enabled(level int) bool { return true }
func (l *testLogr) V(int) logr.Logger { panic("not implemented") }
func (l *testLogr) WithName(string) logr.LogSink { panic("not implemented") }
func (l *testLogr) WithValues(...interface{}) logr.LogSink { panic("not implemented") }
func (l *testLogr) WithCallDepth(depth int) logr.LogSink { return l }

var _ logr.LogSink = &testLogr{}
var _ logr.CallDepthLogSink = &testLogr{}

type callDepthTestLogr struct {
testLogr
Expand All @@ -1673,17 +1682,17 @@ func (l *callDepthTestLogr) resetCallDepth() {
l.callDepth = 0
}

func (l *callDepthTestLogr) WithCallDepth(depth int) logr.Logger {
func (l *callDepthTestLogr) WithCallDepth(depth int) logr.LogSink {
l.mutex.Lock()
defer l.mutex.Unlock()
// Note: Usually WithCallDepth would be implemented by cloning l
// and setting the call depth on the clone. We modify l instead in
// this test helper for simplicity.
l.callDepth = depth
l.callDepth = depth + 1
return l
}

func (l *callDepthTestLogr) Info(msg string, keysAndValues ...interface{}) {
func (l *callDepthTestLogr) Info(level int, msg string, keysAndValues ...interface{}) {
l.mutex.Lock()
defer l.mutex.Unlock()
// Add 2 to depth for the wrapper function caller and for invocation in
Expand All @@ -1710,6 +1719,9 @@ func (l *callDepthTestLogr) Error(err error, msg string, keysAndValues ...interf
})
}

var _ logr.LogSink = &callDepthTestLogr{}
var _ logr.CallDepthLogSink = &callDepthTestLogr{}

func checkLogrEntryCorrectCaller(t *testing.T, wantFile string, wantLine int, entry testLogrEntry) {
t.Helper()

Expand Down
4 changes: 2 additions & 2 deletions klogr/calldepth-test/call_depth_helper_test.go
Expand Up @@ -8,9 +8,9 @@ import (
// their source code file is *not* logged because of WithCallDepth(1).

func myInfo(l logr.Logger, msg string) {
logr.WithCallDepth(l, 1).Info(msg)
l.WithCallDepth(2).Info(msg)
}

func myInfo2(l logr.Logger, msg string) {
myInfo(logr.WithCallDepth(l, 1), msg)
myInfo(l.WithCallDepth(2), msg)
}

0 comments on commit 4171f3c

Please sign in to comment.