forked from golang/glog
-
Notifications
You must be signed in to change notification settings - Fork 218
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
slog support + logr 1.3.0 update #384
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ module k8s.io/klog/v2 | |
|
||
go 1.13 | ||
|
||
require github.com/go-logr/logr v1.2.0 | ||
require github.com/go-logr/logr v1.3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
github.com/go-logr/logr v1.2.0 h1:QK40JKJyMdUDz+h+xvCsru/bJhvG0UxvePV0ufL/AcE= | ||
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= | ||
github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY= | ||
github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
//go:build !go1.21 | ||
// +build !go1.21 | ||
|
||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package serialize | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
|
||
"github.com/go-logr/logr" | ||
) | ||
|
||
// KVFormat serializes one key/value pair into the provided buffer. | ||
// A space gets inserted before the pair. | ||
func (f Formatter) KVFormat(b *bytes.Buffer, k, v interface{}) { | ||
// This is the version without slog support. Must be kept in sync with | ||
// the version in keyvalues_slog.go. | ||
|
||
b.WriteByte(' ') | ||
// Keys are assumed to be well-formed according to | ||
// https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/migration-to-structured-logging.md#name-arguments | ||
// for the sake of performance. Keys with spaces, | ||
// special characters, etc. will break parsing. | ||
if sK, ok := k.(string); ok { | ||
// Avoid one allocation when the key is a string, which | ||
// normally it should be. | ||
b.WriteString(sK) | ||
} else { | ||
b.WriteString(fmt.Sprintf("%s", k)) | ||
} | ||
|
||
// The type checks are sorted so that more frequently used ones | ||
// come first because that is then faster in the common | ||
// cases. In Kubernetes, ObjectRef (a Stringer) is more common | ||
// than plain strings | ||
// (https://github.com/kubernetes/kubernetes/pull/106594#issuecomment-975526235). | ||
switch v := v.(type) { | ||
case textWriter: | ||
writeTextWriterValue(b, v) | ||
case fmt.Stringer: | ||
writeStringValue(b, StringerToString(v)) | ||
case string: | ||
writeStringValue(b, v) | ||
case error: | ||
writeStringValue(b, ErrorToString(v)) | ||
case logr.Marshaler: | ||
value := MarshalerToValue(v) | ||
// A marshaler that returns a string is useful for | ||
// delayed formatting of complex values. We treat this | ||
// case like a normal string. This is useful for | ||
// multi-line support. | ||
// | ||
// We could do this by recursively formatting a value, | ||
// but that comes with the risk of infinite recursion | ||
// if a marshaler returns itself. Instead we call it | ||
// only once and rely on it returning the intended | ||
// value directly. | ||
switch value := value.(type) { | ||
case string: | ||
writeStringValue(b, value) | ||
default: | ||
f.formatAny(b, value) | ||
} | ||
case []byte: | ||
// In https://github.com/kubernetes/klog/pull/237 it was decided | ||
// to format byte slices with "%+q". The advantages of that are: | ||
// - readable output if the bytes happen to be printable | ||
// - non-printable bytes get represented as unicode escape | ||
// sequences (\uxxxx) | ||
// | ||
// The downsides are that we cannot use the faster | ||
// strconv.Quote here and that multi-line output is not | ||
// supported. If developers know that a byte array is | ||
// printable and they want multi-line output, they can | ||
// convert the value to string before logging it. | ||
b.WriteByte('=') | ||
b.WriteString(fmt.Sprintf("%+q", v)) | ||
default: | ||
f.formatAny(b, v) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there no way to avoid having to do this? From what I can think of, this is mostly test only use case than anything else. I know go test examples need a strict string match. Is there a way we can work around this somehow ? This is more of a question than something that needs to be fixed before we can get this change in.
This one will add a condition check for each of the
Format
orSprint
header calls unnecessarily.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is needed for
Example*
style unit tests where the output is matched verbatim. There is no workaround for this, upstream Go recommends to make all output of such tests deterministic.