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 ZapLogger with option to disable stacktraces #47

Closed
wants to merge 3 commits into from
Closed
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
24 changes: 24 additions & 0 deletions pkg/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ limitations under the License.
package logging

import (
"os"

"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"go.uber.org/zap"
runtimezap "sigs.k8s.io/controller-runtime/pkg/log/zap"
runtimelog "sigs.k8s.io/controller-runtime/pkg/runtime/log"
)

Expand All @@ -34,8 +39,27 @@ var (
// Logger is the base logger used by Crossplane. It delegates to another
// logr.Logger. You *must* call SetLogger to get any actual logging.
Logger = logger.WithName("crossplane")

// ZapLogger is a Logger implementation.
ZapLogger = zapLogger
)

// ZapLogger is a Logger implementation from controller runtime.
// If development is true, a Zap development config will be used
// (stacktraces on warnings, no sampling), otherwise a Zap production
// config will be used (stacktraces on errors, sampling).
// If disableStacktrace is true, stacktraces enabled on fatals
// independent of config.
func zapLogger(development bool, disableStacktrace bool) logr.Logger {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as discussed in crossplane-contrib/provider-gcp#49, I like the idea of having a common reusable way to get a logger that only does stack traces for fatal level. Can we continue pursuing that on this PR? If we finish and merge this one, then all consumer repos (main crossplane, GCP/AWS/Azure stacks) can just call this one single implementation.

zl := runtimezap.RawLoggerTo(os.Stderr, development)

if disableStacktrace {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you think this should use the new controller-runtime v0.2.2 way of initializing the logger? It is probably not worth updating the entire controller-runtime to just get the new logger initialization, but it could also be nice to get a new controller-runtime update anyways.

What is your opinion @turkenh?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you noted, updating controller-runtime is not necessary for this specific issue but I agree that it make sense to bump controller-runtime to v0.2.2 which is latest v0.2, currently it is on v0.2.0-beta.5 which does not sound that stable :)
Still I'll go and ask on slack dev channel to make sure everyone is ok with this.

zl = zl.WithOptions(zap.AddStacktrace(zap.FatalLevel))
}

return zapr.NewLogger(zl)
}

// SetLogger sets a concrete logging implementation for all deferred Loggers.
func SetLogger(l logr.Logger) {
logger.Fulfill(l)
Expand Down