forked from golang/glog
-
Notifications
You must be signed in to change notification settings - Fork 219
structured logging: support values with line breaks #273
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5ecf788
examples: use current klog, add structured logging example
pohly ae76ae0
structured output: reuse buffer
pohly aac832f
structured output: reduce usage of Sprintf
pohly 6749fe1
structured output: reorder type switch
pohly fafe98e
structured logging: support key/value pairs with line breaks
pohly 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 hidden or 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 hidden or 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 hidden or 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,74 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
|
||
"k8s.io/klog/v2" | ||
) | ||
|
||
// MyStruct will be logged via %+v | ||
type MyStruct struct { | ||
Name string | ||
Data string | ||
internal int | ||
} | ||
|
||
// MyStringer will be logged as string, with String providing that string. | ||
type MyString MyStruct | ||
|
||
func (m MyString) String() string { | ||
return m.Name + ": " + m.Data | ||
} | ||
|
||
func main() { | ||
klog.InitFlags(nil) | ||
flag.Parse() | ||
|
||
someData := MyStruct{ | ||
Name: "hello", | ||
Data: "world", | ||
} | ||
|
||
longData := MyStruct{ | ||
Name: "long", | ||
Data: `Multiple | ||
lines | ||
with quite a bit | ||
of text.`, | ||
} | ||
|
||
logData := MyStruct{ | ||
Name: "log output from some program", | ||
Data: `I0000 12:00:00.000000 123456 main.go:42] Starting | ||
E0000 12:00:01.000000 123456 main.go:43] Failed for some reason | ||
`, | ||
} | ||
|
||
stringData := MyString(longData) | ||
|
||
klog.Infof("someData printed using InfoF: %v", someData) | ||
klog.Infof("longData printed using InfoF: %v", longData) | ||
klog.Infof(`stringData printed using InfoF, | ||
with the message across multiple lines: | ||
%v`, stringData) | ||
klog.Infof("logData printed using InfoF:\n%v", logData) | ||
|
||
klog.Info("=============================================") | ||
|
||
klog.InfoS("using InfoS", "someData", someData) | ||
klog.InfoS("using InfoS", "longData", longData) | ||
klog.InfoS(`using InfoS with | ||
the message across multiple lines`, | ||
"int", 1, | ||
"stringData", stringData, | ||
"str", "another value") | ||
klog.InfoS("using InfoS", "logData", logData) | ||
klog.InfoS("using InfoS", "boolean", true, "int", 1, "float", 0.1) | ||
|
||
// The Kubernetes recommendation is to start the message with uppercase | ||
// and not end with punctuation. See | ||
// https://github.com/kubernetes/community/blob/HEAD/contributors/devel/sig-instrumentation/migration-to-structured-logging.md | ||
klog.InfoS("Did something", "item", "foobar") | ||
// Not recommended, but also works. | ||
klog.InfoS("This is a full sentence.", "item", "foobar") | ||
} |
This file contains hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.