Skip to content

Commit

Permalink
feat use go-logr v0.2.0
Browse files Browse the repository at this point in the history
Signed-off-by: Yoan Blanc <yoan@dosimple.ch>
  • Loading branch information
greut committed Jun 12, 2020
1 parent 825bd2f commit 966c986
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 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.1.0
require github.com/go-logr/logr v0.2.0
4 changes: 2 additions & 2 deletions go.sum
@@ -1,2 +1,2 @@
github.com/go-logr/logr v0.1.0 h1:M1Tv3VzNlEHg6uyACnRdtrploV2P7wZqH8BoQMtz0cg=
github.com/go-logr/logr v0.1.0/go.mod h1:ixOQHD9gLJUVQQ2ZOR7zLEifBX6tGkNJF4QyIY7sIas=
github.com/go-logr/logr v0.2.0 h1:QvGt2nLcHH0WK9orKa+ppBPAxREcH364nPUedEpK0TY=
github.com/go-logr/logr v0.2.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU=
24 changes: 16 additions & 8 deletions klog.go
Expand Up @@ -687,7 +687,7 @@ func (buf *buffer) someDigits(i, d int) int {
return copy(buf.tmp[i:], buf.tmp[j:])
}

func (l *loggingT) println(s severity, logr logr.InfoLogger, args ...interface{}) {
func (l *loggingT) println(s severity, logr logr.Logger, 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
Expand All @@ -699,11 +699,11 @@ func (l *loggingT) println(s severity, logr logr.InfoLogger, args ...interface{}
l.output(s, logr, buf, file, line, false)
}

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

func (l *loggingT) printDepth(s severity, logr logr.InfoLogger, depth int, args ...interface{}) {
func (l *loggingT) printDepth(s severity, logr logr.Logger, 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
Expand All @@ -718,7 +718,7 @@ func (l *loggingT) printDepth(s severity, logr logr.InfoLogger, depth int, args
l.output(s, logr, buf, file, line, false)
}

func (l *loggingT) printf(s severity, logr logr.InfoLogger, format string, args ...interface{}) {
func (l *loggingT) printf(s severity, logr logr.Logger, 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
Expand All @@ -736,7 +736,7 @@ func (l *loggingT) printf(s severity, logr logr.InfoLogger, format string, args
// 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.InfoLogger, file string, line int, alsoToStderr bool, args ...interface{}) {
func (l *loggingT) printWithFileLine(s severity, logr logr.Logger, 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
Expand All @@ -761,7 +761,7 @@ func (l *loggingT) errorS(err error, loggr logr.Logger, msg string, keysAndValue
}

// if loggr is specified, will call loggr.Info, otherwise output with logging module.
func (l *loggingT) infoS(loggr logr.InfoLogger, msg string, keysAndValues ...interface{}) {
func (l *loggingT) infoS(loggr logr.Logger, msg string, keysAndValues ...interface{}) {
if loggr != nil {
loggr.Info(msg, keysAndValues)
return
Expand Down Expand Up @@ -878,7 +878,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.InfoLogger, buf *buffer, file string, line int, alsoToStderr bool) {
func (l *loggingT) output(s severity, log logr.Logger, buf *buffer, file string, line int, alsoToStderr bool) {
l.mu.Lock()
if l.traceLocation.isSet() {
if l.traceLocation.match(file, line) {
Expand Down Expand Up @@ -1231,7 +1231,7 @@ func (l *loggingT) setV(pc uintptr) Level {
// See the documentation of V for more information.
type Verbose struct {
enabled bool
logr logr.InfoLogger
logr logr.Logger
}

func newVerbose(level Level, b bool) Verbose {
Expand Down Expand Up @@ -1324,6 +1324,14 @@ func (v Verbose) InfoS(msg string, keysAndValues ...interface{}) {
}
}

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

// Info logs to the INFO log.
// Arguments are handled in the manner of fmt.Print; a newline is appended if missing.
func Info(args ...interface{}) {
Expand Down
3 changes: 1 addition & 2 deletions klogr/klogr.go
Expand Up @@ -174,7 +174,7 @@ func (l klogger) Error(err error, msg string, kvList ...interface{}) {
klog.ErrorDepth(framesToCaller(), l.prefix, " ", msgStr, " ", errStr, " ", fixedStr, " ", userStr)
}

func (l klogger) V(level int) logr.InfoLogger {
func (l klogger) V(level int) logr.Logger {
new := l.clone()
new.level = level
return new
Expand All @@ -199,4 +199,3 @@ func (l klogger) WithValues(kvList ...interface{}) logr.Logger {
}

var _ logr.Logger = klogger{}
var _ logr.InfoLogger = klogger{}
2 changes: 1 addition & 1 deletion klogr/klogr_test.go
Expand Up @@ -22,7 +22,7 @@ func TestInfo(t *testing.T) {
flag.Parse()

tests := map[string]struct {
klogr logr.InfoLogger
klogr logr.Logger
text string
keysAndValues []interface{}
expectedOutput string
Expand Down

0 comments on commit 966c986

Please sign in to comment.