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

Fix bug with issues [17464] fix with zap logger redirection #17510

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

LLiuJJ
Copy link

@LLiuJJ LLiuJJ commented Feb 29, 2024

etcdctl: fix etcdctl zap logger redirection bug

This bug was report with issues #17464

Here is my test log

snaphot_cmd_test

Signed-off-by: Colin jay_jieliu@outlook.com

@k8s-ci-robot
Copy link

Hi @LLiuJJ. Thanks for your PR.

I'm waiting for a etcd-io 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.

Copy link
Member

@jmhbnz jmhbnz left a comment

Choose a reason for hiding this comment

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

Hey @LLiuJJ - Thanks for tackling this fix.

Please ensure your commit is signed so the developer certificate of origin (DCO) check passes, i.e:

git rebase HEAD~1 --signoff
git push --force

/ok-to-test

Signed-off-by: Colin <jay_jieliu@outlook.com>
Signed-off-by: Colin <jay_jieliu@outlook.com>
Signed-off-by: Colin <jay_jieliu@outlook.com>
Signed-off-by: Colin <jay_jieliu@outlook.com>
@LLiuJJ LLiuJJ requested a review from jmhbnz March 4, 2024 01:45
@LLiuJJ
Copy link
Author

LLiuJJ commented Mar 6, 2024

/ok-to-test

@siyuanfoundation
Copy link
Contributor

@ivanvc

@ivanvc
Copy link
Member

ivanvc commented Apr 25, 2024

/cc @ivanvc

Copy link
Member

@ivanvc ivanvc left a comment

Choose a reason for hiding this comment

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

Thanks for your PR @LLiuJJ. Please take a look at my comments.

Comment on lines 41 to 46
lcfg.Development = false
lcfg.Encoding = DefaultLogFormat
lcfg.Sampling = &zap.SamplingConfig{
Initial: 100,
Thereafter: 100,
}
Copy link
Member

Choose a reason for hiding this comment

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

These values are already defined in DefaultZapLoggerConfig; any reason why to redefine them here?

Copy link
Author

@LLiuJJ LLiuJJ Apr 28, 2024

Choose a reason for hiding this comment

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

( ⋂‿⋂’) Thank you for your review, Your suggestion is right, I will resubmit my fix code.

Comment on lines 48 to 51
return level == zapcore.InfoLevel
})
errorFatalLevel := zap.LevelEnablerFunc(func(level zapcore.Level) bool {
return level == zapcore.ErrorLevel || level == zapcore.FatalLevel
Copy link
Member

Choose a reason for hiding this comment

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

The problem with this is that it's not handling the WarningLevel, nor PanicLevel. It is an enumeration, so it would be useful to compare using greater than.

@@ -33,6 +34,39 @@ func CreateDefaultZapLogger(level zapcore.Level) (*zap.Logger, error) {
return c, nil
}

// CreateUtilZapLogger creates a logger with default zap configuration can redirect log to /dev/null
func CreateUtilZapLogger(level zapcore.Level) *zap.Logger {
Copy link
Member

Choose a reason for hiding this comment

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

I was able to keep returning the *zap.Logger and an error, by writing it like so:

func CreateUtilZapLogger(level zapcore.Level) (*zap.Logger, error) {
	lcfg := DefaultZapLoggerConfig
	lcfg.Level = zap.NewAtomicLevelAt(level)
	infoLevel := zap.LevelEnablerFunc(func(level zapcore.Level) bool {
		return level <= zapcore.InfoLevel
	})
	errorFatalLevel := zap.LevelEnablerFunc(func(level zapcore.Level) bool {
		return level > zapcore.InfoLevel
	})
	stdoutSyncer := zapcore.Lock(os.Stdout)
	stderrSyncer := zapcore.Lock(os.Stderr)
	opts := []zap.Option{
		zap.WrapCore(func(core zapcore.Core) zapcore.Core {
			return zapcore.NewTee(
				zapcore.NewCore(
					zapcore.NewJSONEncoder(lcfg.EncoderConfig),
					stdoutSyncer,
					infoLevel,
				),
				zapcore.NewCore(
					zapcore.NewJSONEncoder(lcfg.EncoderConfig),
					stderrSyncer,
					errorFatalLevel,
				),
			)
		}),
	}
	return lcfg.Build(opts...)
}

Which also makes me think if this should be an extension to CreateDefaultZapLogger(), and allow it to receive zap.Options.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not too fond of the name CreateUtilZapLogger. But I don't want to request more changes, being respectful of the contributor's time, as we don't have a conclusion on the parent issue 🙂.

if err != nil {
cobrautl.ExitWithError(cobrautl.ExitError, err)
}
lg := logutil.CreateUtilZapLogger(zap.InfoLevel)
Copy link
Member

Choose a reason for hiding this comment

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

The problem I spot here is that it will only fix the issue for snapshot save. I'm curious about what's the conclusion of issue #17464. So, maybe this fix should expand to other commands, not only this one.

Copy link
Member

Choose a reason for hiding this comment

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

There's still one instance in which it still calls CreateDefaultZapLogger. Refer to etcdctl/ctlv3/command/global.go:141.

Signed-off-by: Colin <jay_jieliu@outlook.com>
@LLiuJJ
Copy link
Author

LLiuJJ commented Apr 28, 2024

/retest

@LLiuJJ LLiuJJ requested a review from ivanvc April 28, 2024 16:13
@ivanvc
Copy link
Member

ivanvc commented May 6, 2024

Thanks for addressing my comments, @LLiuJJ. I'm waiting for the conclusion in #17464 to see how to proceed with this PR.

Signed-off-by: Colin <jay_jieliu@outlook.com>
@LLiuJJ
Copy link
Author

LLiuJJ commented May 7, 2024

/retest

1 similar comment
@LLiuJJ
Copy link
Author

LLiuJJ commented May 7, 2024

/retest

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

Successfully merging this pull request may close these issues.

None yet

5 participants