Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get rid of InfoLogger, make V() additive #16

Merged
merged 2 commits into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions examples/tab_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ limitations under the License.
package main

import (
"os"
"fmt"
"os"
"text/tabwriter"

"github.com/go-logr/logr"
)

// TabLogger is a sample logr.Logger that logs to stderr.
// It's terribly inefficient, and is *only* a basic example.
type TabLogger struct{
name string
type TabLogger struct {
name string
keyValues map[string]interface{}

writer *tabwriter.Writer
Expand Down Expand Up @@ -56,15 +56,15 @@ func (l *TabLogger) Error(err error, msg string, kvs ...interface{}) {
l.Info(msg, kvs...)
}

func (l *TabLogger) V(_ int) logr.InfoLogger {
func (l *TabLogger) V(_ int) logr.Logger {
return l
}

func (l *TabLogger) WithName(name string) logr.Logger {
return &TabLogger{
name: l.name+"."+name,
name: l.name + "." + name,
keyValues: l.keyValues,
writer: l.writer,
writer: l.writer,
}
}

Expand All @@ -77,9 +77,9 @@ func (l *TabLogger) WithValues(kvs ...interface{}) logr.Logger {
newMap[kvs[i].(string)] = kvs[i+1]
}
return &TabLogger{
name: l.name,
name: l.name,
keyValues: newMap,
writer: l.writer,
writer: l.writer,
}
}

Expand Down
32 changes: 12 additions & 20 deletions logr.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,19 @@ limitations under the License.
// above concepts, when neccessary (for example, in a pure-JSON output form, it
// would be necessary to represent at least message and timestamp as ordinary
// named values).
//
package logr

// TODO: consider adding back in format strings if they're really needed
// TODO: consider other bits of zap/zapcore functionality like ObjectMarshaller (for arbitrary objects)
// TODO: consider other bits of glog functionality like Flush, InfoDepth, OutputStats

// InfoLogger represents the ability to log non-error messages, at a particular verbosity.
type InfoLogger interface {
// Logger represents the ability to log messages, both errors and not.
type Logger interface {
// Enabled tests whether this Logger is enabled. For example, commandline
// flags might be used to set the logging verbosity and disable some info
// logs.
Enabled() bool

// Info logs a non-error message with the given key/value pairs as context.
//
// The msg argument should be used to add some constant description to
Expand All @@ -145,19 +149,6 @@ type InfoLogger interface {
// keys and arbitrary values.
Info(msg string, keysAndValues ...interface{})

// Enabled tests whether this InfoLogger is enabled. For example,
// commandline flags might be used to set the logging verbosity and disable
// some info logs.
Enabled() bool
}

// Logger represents the ability to log messages, both errors and not.
type Logger interface {
// All Loggers implement InfoLogger. Calling InfoLogger methods directly on
// a Logger value is equivalent to calling them on a V(0) InfoLogger. For
// example, logger.Info() produces the same result as logger.V(0).Info.
InfoLogger

// Error logs an error, with the given message and key/value pairs as context.
// It functions similarly to calling Info with the "error" named value, but may
// have unique behavior, and should be preferred for logging errors (see the
Expand All @@ -168,10 +159,11 @@ type Logger interface {
// triggered this log line, if present.
Error(err error, msg string, keysAndValues ...interface{})

// V returns an InfoLogger value for a specific verbosity level. A higher
// verbosity level means a log message is less important. It's illegal to
// pass a log level less than zero.
V(level int) InfoLogger
// V returns an Logger value for a specific verbosity level, relative to
// this Logger. In other words, V values are additive. V higher verbosity
// level means a log message is less important. It's illegal to pass a log
// level less than zero.
V(level int) Logger

// WithValues adds some key-value pairs of context to a logger.
// See Info for documentation on how key/value pairs work.
Expand Down
2 changes: 1 addition & 1 deletion testing/null.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (_ NullLogger) Error(_ error, _ string, _ ...interface{}) {
// Do nothing.
}

func (log NullLogger) V(_ int) logr.InfoLogger {
func (log NullLogger) V(_ int) logr.Logger {
return log
}

Expand Down
2 changes: 1 addition & 1 deletion testing/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (log TestLogger) Error(err error, msg string, args ...interface{}) {
log.T.Logf("%s: %v -- %v", msg, err, args)
}

func (log TestLogger) V(v int) logr.InfoLogger {
func (log TestLogger) V(v int) logr.Logger {
return log
}

Expand Down