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 helpers for remaining Zap options #639

Merged

Conversation

hasbro17
Copy link
Contributor

This PR adds predefined functional options for more easily setting the logger options in pkg/log/zap.

Follow up to #560 per #560 (comment)

/cc @DirectXMan12 @alvaroaleman

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Oct 14, 2019
@k8s-ci-robot
Copy link
Contributor

Hi @hasbro17. Thanks for your PR.

I'm waiting for a kubernetes-sigs or kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@hasbro17
Copy link
Contributor Author

This covers the all the provided options but I was wondering if we should expose more predefined options for the EncoderConfig?
I guess you can still set that via the Encoder option.

import (
	"os"

	uzap "go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"sigs.k8s.io/controller-runtime/pkg/log/zap"
	logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
)

var log = logf.Log.WithName("cmd")

func main() {
	ecfg := uzap.NewDevelopmentEncoderConfig()
	ecfg.EncodeTime = zapcore.EpochTimeEncoder
	ecfg.EncodeLevel = zapcore.LowercaseLevelEncoder
	encoder := zapcore.NewConsoleEncoder(ecfg)

	zapLogr := zap.New(zap.Development(true), zap.DestWritter(os.Stdout), zap.Encoder(encoder))
	logf.SetLogger(zapLogr)

	log.Info("Test message", "value", 1)

}

And having options for both Encoder and EncoderConfig might be confusing if you could set two different configs via both options.

@hasbro17
Copy link
Contributor Author

@alvaroaleman I also just realized that the destination writer option is named DestWritter

DestWritter io.Writer

Is that supposed to be with a single T DestWriter? I could change that, but that might be a breaking change.

@alvaroaleman
Copy link
Member

/ok-to-test

Is that supposed to be with a single T DestWriter? I could change that, but that might be a breaking change.

Yes, seems there is a typo. Please change it, I guess Solly will defer changing that to a new release anyways.

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Oct 14, 2019
pkg/log/zap/zap.go Outdated Show resolved Hide resolved
}
}

// Encoder sets the Encoder option for the logger
Copy link
Member

Choose a reason for hiding this comment

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

Should we probably mention the defaults in the godocs for the new funcs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We have them defined already for the Option fields so I wasn't sure if I should repeat those here again.

// The encoder to use, defaults to console when Development is true
// and JSON otherwise

}

// ZapOpts appends zapOpts to the raw zap.Options option of the logger
func ZapOpts(zapOpts []zap.Option) func(o *Options) {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe RawZapOpts to avoid the linter error?

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Oct 19, 2019
@hasbro17 hasbro17 changed the title ✨ Add predefined functional options for log/zap WIP ✨ Add predefined functional options for log/zap Oct 19, 2019
@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Oct 19, 2019
@hasbro17
Copy link
Contributor Author

Didn't see #646 get merged which has the first two options. I'll rebase this to only add the remaining option helpers.

@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Oct 19, 2019
@hasbro17 hasbro17 changed the title WIP ✨ Add predefined functional options for log/zap WIP ✨ Add helpers for remaining Zap options Oct 19, 2019
@@ -82,18 +77,51 @@ type Opts func(*Options)
// See Options.Development
func UseDevMode(enabled bool) Opts {
return func(o *Options) {
o.Development = true
o.Development = enabled
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@DirectXMan12 Did you mean to fix this?
#646 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

@hasbro17 I noticed this as well, and fixed it and added tests in #653.

@hasbro17 hasbro17 changed the title WIP ✨ Add helpers for remaining Zap options ✨ Add helpers for remaining Zap options Oct 20, 2019
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Oct 20, 2019
@alvaroaleman
Copy link
Member

/lgtm
/assign @DirectXMan12

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 23, 2019
@hasbro17
Copy link
Contributor Author

hasbro17 commented Nov 4, 2019

PTAL @mengqiy or @droot if you get a chance.

There's a small overlap with #653 so we can rebase either PR depending on what gets merged first.

Copy link
Contributor

@DirectXMan12 DirectXMan12 left a comment

Choose a reason for hiding this comment

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

minor nit, otherwise fine


// RawZapOpts allows appending arbitrary zap.Options to configure the underlying zap logger.
// See Options.RawZapOpts
func RawZapOpts(zapOpts []zap.Option) func(o *Options) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: make this variadic

@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 6, 2019
@k8s-ci-robot
Copy link
Contributor

New changes are detected. LGTM label has been removed.

@mengqiy mengqiy added approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm "Looks good to me", indicates that a PR is ready to be merged. labels Nov 8, 2019
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

Approval requirements bypassed by manually added approval.

This pull-request has been approved by: hasbro17

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@mengqiy
Copy link
Member

mengqiy commented Nov 8, 2019

/lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants