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

Use ISO8601 timestamp format for zap log messages #843

Merged
merged 2 commits into from
Oct 12, 2021

Conversation

creydr
Copy link
Collaborator

@creydr creydr commented Oct 1, 2021

Is this a BUG FIX or a FEATURE ?:

/kind enhancement

What this PR does / why we need it:

Currently the timestamps in the logs are in epoch format. This makes it really hard to get the time of the event. This PR uses the ISO8601 format for encoding timestamps:

{"level":"info","ts":"2021-10-01T11:42:17.421Z","logger":"controller-runtime.manager.controller.nodenetworkstate","msg":"Starting Controller","reconciler group":"nmstate.io","reconciler kind":"NodeNetworkState"}

instead of

{"level":"info","ts":"1633079530.9509594","logger":"controller-runtime.manager.controller.nodenetworkstate","msg":"Starting Controller","reconciler group":"nmstate.io","reconciler kind":"NodeNetworkState"}

In addition it adds the standard zap flags:

  • zap-devel: Enable zap development mode (changes defaults to console encoder, debug log level, disables sampling and stacktrace from 'warning' level)")
  • zap-encoder: Zap log encoding (json or console)
  • zap-level: Zap log level (one of debug, info, error or any integer value > 0)
  • zap-stacktrace-level: Set the minimum log level that triggers stacktrace generation

The --v parameter is marked as deprecated in favor of zap-devel.

Special notes for your reviewer:
Unfortunately the standard zap flags don't provide a way to specify the timestamp format, so I had to set it "hard" to ISO8601.

Release note:

Deprecates the --v flag in favor of the --zap-devel flag
Uses ISO8601 format for timestamp encoding of log messages from zap logger

@kubevirt-bot kubevirt-bot added kind/enhancement release-note-action-required Denotes a PR that introduces potentially breaking changes that require user action. dco-signoff: yes Indicates the PR's author has DCO signed all their commits. labels Oct 1, 2021
@kubevirt-bot
Copy link
Collaborator

Hi @creydr. Thanks for your PR.

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

@kubevirt-bot kubevirt-bot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/XXL labels Oct 1, 2021
@phoracek
Copy link
Member

phoracek commented Oct 6, 2021

Thanks for the contribution @creydr

@rhrazdil would you please help review this PR?

@rhrazdil
Copy link
Collaborator

rhrazdil commented Oct 6, 2021

Hello @creydr , thanks for your PR.
I like the idea of making the timestamp more human-readable, however, I'm not sure if this PR in it's current state
is the best approach.
I don't think it's desirable to bring in zap package - we would then effectively became maintainers of the code.
And it's a lot of code.

Why not define a configuration with the said ISO8601 format (we can discuss potential alternatives later) and passing it as
an option to zap.New?

Something like this might work, of course ideally wrapped in a function and not directly in main.go, but you get the point:

	ecfg := zapcore.EncoderConfig{
		// Keys can be anything except the empty string.
		TimeKey:        "ts",
		LevelKey:       "level",
		NameKey:        "name",
		CallerKey:      "caller",
		FunctionKey:    zapcore.OmitKey,
		MessageKey:     "msg",
		StacktraceKey:  "error",
		LineEnding:     zapcore.DefaultLineEnding,
		EncodeLevel:    zapcore.LowercaseLevelEncoder,
		EncodeTime:     zapcore.ISO8601TimeEncoder,
		EncodeDuration: zapcore.StringDurationEncoder,
		EncodeCaller:   zapcore.ShortCallerEncoder,
	}
	encoder := zapcore.NewJSONEncoder(ecfg)
	ctrl.SetLogger(zap.New(zap.UseDevMode(logType != "production"), zap.Encoder(encoder)))

@creydr creydr force-pushed the support-zap-logging-options branch 2 times, most recently from 0e6ffe1 to e6e557d Compare October 7, 2021 11:56
@kubevirt-bot kubevirt-bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed release-note-action-required Denotes a PR that introduces potentially breaking changes that require user action. labels Oct 7, 2021
@creydr
Copy link
Collaborator Author

creydr commented Oct 7, 2021

Hello @rhrazdil,
thanks for the feedback. I thought the same at the beginning, but was struggling with the hard coding of so many options and having nothing adjustable via flags. Anyhow I found another way and updated this PR (only without the possibility to adjust the timestamp format via a flag).

@creydr creydr force-pushed the support-zap-logging-options branch from e6e557d to 16050fb Compare October 7, 2021 14:07
@rhrazdil
Copy link
Collaborator

/ok-to-test

@kubevirt-bot kubevirt-bot 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 11, 2021
main.go Outdated
@@ -252,6 +250,24 @@ func main() {
}
}

func setupLogger() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you please make the setupLogger function more focused?
It's currently doing two things: setting up the logger, and parsing command line flags.
Maybe you could pass the opts as a parameter and do the flag parsing in main directly?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for your feedback. Makes sense. I updated the PR.

Copy link
Collaborator

@rhrazdil rhrazdil left a comment

Choose a reason for hiding this comment

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

Thanks! Just one small thing, wouldn't you mind squashing the two commits into one commit enriched by a brief description of the change?

main.go Outdated
pflag.Parse()

if logType == "debug" {
// workarround until --v flag got removed
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: workaround

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

🤦 Thanks. Updated it

Adds the zap flagset to configure the logging. This includes the
following flags:
zap-devel: Enable zap development mode (changes defaults to console
encoder, debug log level, disables sampling and stacktrace from warning
level).
zap-encoder: Zap log encoding (json or console).
zap-level: Zap log level (one of debug, info, error or any uint value).
zap-stacktrace-level: Set the minimum log level that triggers stacktrace
generation.

Deprecates the --v flag in favor of --zap-debug.

Signed-off-by: Christoph Stäbler <cstabler@redhat.com>
Uses the ISO8601 format for encoding of log timestamps for a more
human-readable output.

Signed-off-by: Christoph Stäbler <cstabler@redhat.com>
@creydr creydr force-pushed the support-zap-logging-options branch from 479d001 to 7fb2914 Compare October 11, 2021 14:05
@creydr
Copy link
Collaborator Author

creydr commented Oct 11, 2021

Thanks! Just one small thing, wouldn't you mind squashing the two commits into one commit enriched by a brief description of the change?

If this is the usual way you do it here I can squash them. Anyhow I kept them separate as they are 2 different changes actually (whereas one was kind the "base" for the other). So let me know if this makes sense for you, or if I should squash them.
I enriched the commit messages with a brief description.

@creydr creydr requested a review from rhrazdil October 11, 2021 14:11
@rhrazdil
Copy link
Collaborator

Thanks! Just one small thing, wouldn't you mind squashing the two commits into one commit enriched by a brief description of the change?

If this is the usual way you do it here I can squash them. Anyhow I kept them separate as they are 2 different changes actually (whereas one was kind the "base" for the other). So let me know if this makes sense for you, or if I should squash them.
I enriched the commit messages with a brief description.

Thanks for the descriptions, this change makes sense as it is now. No need to squash.

/lgtm
/approve

@kubevirt-bot kubevirt-bot added the lgtm Indicates that a PR is ready to be merged. label Oct 12, 2021
@kubevirt-bot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: rhrazdil

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

@kubevirt-bot kubevirt-bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Oct 12, 2021
Copy link
Collaborator

@rhrazdil rhrazdil left a comment

Choose a reason for hiding this comment

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

(for the record)

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. dco-signoff: yes Indicates the PR's author has DCO signed all their commits. kind/enhancement lgtm 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. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/M
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants