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

Add NullLogger #26

Closed
wojas opened this issue Oct 20, 2020 · 3 comments · Fixed by #28
Closed

Add NullLogger #26

wojas opened this issue Oct 20, 2020 · 3 comments · Fixed by #28

Comments

@wojas
Copy link
Contributor

wojas commented Oct 20, 2020

Following discussion in #25, it is probably a good idea to add a null.Logger to logr that ignores all messages.

This can be used as a default logger by components that optionally allow you to pass in a logger. If none is passed in, it can simply use the null logger for all its logging.

The implementation would live under a "null" subpackage and could look like this (needs doc comments):

package null

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

type Logger struct{}

func (n Logger) Enabled() bool {
	return false
}

func (n Logger) Info(msg string, keysAndValues ...interface{}) {
}

func (n Logger) Error(err error, msg string, keysAndValues ...interface{}) {
}

func (n Logger) V(level int) logr.Logger {
	return n
}

func (n Logger) WithValues(keysAndValues ...interface{}) logr.Logger {
	return n
}

func (n Logger) WithName(name string) logr.Logger {
	return n
}

Usage:

import "github.com/go-logr/logr/null"

func foo() {
	l := null.Logger{}
}
@wojas wojas mentioned this issue Oct 20, 2020
@thockin
Copy link
Contributor

thockin commented Oct 20, 2020

Do you want to send a PR for this or do you want me to just copy it and attribute you? I am tempted to just leave it in the logr package.

@thockin
Copy link
Contributor

thockin commented Oct 20, 2020

Also - expose type or func NewNull() Logger { return nullLogger{} } ?

@wojas
Copy link
Contributor Author

wojas commented Oct 22, 2020

Do you want to send a PR for this or do you want me to just copy it and attribute you? I am tempted to just leave it in the logr package.

After some thought I also came to the conclusion that it's better to put it at the logr package level, because the null logger needs to import the Logger interface, and then the main package would no longer be able to import the null logger due to a circular import.

I will add a PR for this and then we can further discuss the details there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants