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
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 5 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,6 @@ required = [
source = "https://github.com/fsnotify/fsnotify.git"
version="v1.4.7"

[[constraint]]
name="sigs.k8s.io/controller-runtime"
version="v0.2.2"
23 changes: 23 additions & 0 deletions pkg/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ package logging

import (
"github.com/go-logr/logr"
"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 +36,29 @@ 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 Logger implementation of 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.

o := func(o *runtimezap.Options) {
o.Development = development
if disableStacktrace {
lvl := zap.NewAtomicLevelAt(zap.FatalLevel)
o.StacktraceLevel = &lvl
}
}

return runtimezap.New(o)
}

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