-
Notifications
You must be signed in to change notification settings - Fork 148
add support to parse JSON logs #228
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
Conversation
…, and add zap-json input format support
cmd/cmd.go
Outdated
if color.NoColor { | ||
messageTpl = `{{ with $msg := .Message | parseJSON }}[{{ $msg.ts }}] {{ $msg.level }} ({{ $msg.caller }}) {{ $msg.msg }}{{ end }}` | ||
} else { | ||
messageTpl = `{{ with $msg := .Message | parseJSON }}[{{ color $.TimestampColor $msg.ts }}] {{ levelColor $msg.level }} ({{ color $.CallerColor $msg.caller }}) {{ $msg.msg }}{{ end }}` |
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.
I would like to create a generic color function and use it as follows. This will eliminate the need to change tail.go
.
diff --git a/cmd/cmd.go b/cmd/cmd.go
index 5dc6784..4395441 100644
--- a/cmd/cmd.go
+++ b/cmd/cmd.go
@@ -242,7 +242,7 @@ func (o *options) sternConfig() (*stern.Config, error) {
if color.NoColor {
messageTpl = `{{ with $msg := .Message | parseJSON }}[{{ $msg.ts }}] {{ $msg.level }} ({{ $msg.caller }}) {{ $msg.msg }}{{ end }}`
} else {
- messageTpl = `{{ with $msg := .Message | parseJSON }}[{{ color $.TimestampColor $msg.ts }}] {{ levelColor $msg.level }} ({{ color $.CallerColor $msg.caller }}) {{ $msg.msg }}{{ end }}`
+ messageTpl = `{{ with $msg := .Message | parseJSON }}[{{ colorGreen $msg.ts }}] {{ levelColor $msg.level }} ({{ colorCyan $msg.caller }}) {{ $msg.msg }}{{ end }}`
}
case "default":
default:
@@ -312,6 +312,14 @@ func (o *options) sternConfig() (*stern.Config, error) {
"color": func(color color.Color, text string) string {
return color.SprintFunc()(text)
},
+ "colorBlack": color.BlackString,
+ "colorRed": color.RedString,
+ "colorGreen": color.GreenString,
+ "colorYellow": color.YellowString,
+ "colorBlue": color.BlueString,
+ "colorMagenta": color.MagentaString,
+ "colorCyan": color.CyanString,
+ "colorWhite": color.WhiteString,
"levelColor": func(level string) string {
var levelColor *color.Color
switch strings.ToLower(level) {
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.
Thank you for the suggestion! I was not happy about adding specific color fields into the tail.go
file, and I was thinking more about function like colorFromString
, or even change default color
function to test agains specific constants, but I was not sure whether it worth the trouble.
I like your idea, will apply your suggestion 👍
README.md
Outdated
@@ -84,6 +84,7 @@ Supported Kubernetes resources are `pod`, `replicationcontroller`, `service`, `d | |||
`--field-selector` | | Selector (field query) to filter on. If present, default to ".*" for the pod-query. | |||
`--include`, `-i` | `[]` | Log lines to include. (regular expression) | |||
`--init-containers` | `true` | Include or exclude init containers. | |||
`--input-format`, `-I` | `default` | Specify input format. Currently support: [default, zap-json] |
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.
Since the name of the option that specifies the output format is output
, it seems that the name of the option that specifies the input format should be input
, but I am torn.
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.
I'm a little bit hesitant about --input
since it's too broad. If one day someone would like to add --input
option for whatever reason, that person would have hard time finding an appropriate name.
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.
I feel that the flag name --input-format
is a bit confusing because its purpose is to change the output format. We can consider it a formatter for json-zap
, so how about --formatter
?
stern --formatter zap-json <QUERY>
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.
Yeah, I was afraid that --input-format
might sound misleading, one of the option I had in mind is --reformat
😆 .
--formatter
sounds good for me, @superbrothers what do you think?
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.
I've changed --input-format
to --formatter
.
…late constants, update template accordingly
Thank you for the PR! I understand the use case, but I would prefer not to support application or library-specific formats to keep Stern simple. There are countless log libraries and log formats, so it will soon be unmanageable in this approach. I believe doing the log formatting and the syntax highlighting in another program is better. It is more flexible and works better with other programs.
|
Yes, you may be right. We need to continue to manage once we support. However, since there is no change to the default, it seems to me that if it breaks, the person who needs it can just fix it. |
That makes sense. In that case, I have no objection to adding this feature. |
Thank you for taking time to review 🙇 I perfectly understand your reasoning, and TBH I was considering using something like https://github.com/garabik/grc. At the same time it looks an overkill, and I was thinking that at some point these templates could be moved to configuration files, and even have a dedicated "community" repo if their number starts to grow, but it's too early to make such decision. |
I agree it's too early to move to configuration files, so I favor implementing it as a flag. 👍 |
@tksm @superbrothers Thank you for taking your time for the review! Is there anything else I could do to get this PR approved and merged? |
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.
LGTM 🎉
@superbrothers Please check the change of the flag name to |
Thank you folks 🙇 |
@niamster I tried this feature and found the following problems. Could you check them? I assume the format of {"level":"info","ts":1257894000,"caller":"sandbox2210433069/prog.go:10","msg":"hello","key1":"val1"}
{"level":"info","ts":1257894000,"caller":"sandbox2210433069/prog.go:13","msg":"world","key1":"val1"} When we specify
When we specify
I tested with the following manifest. apiVersion: v1
kind: Pod
metadata:
name: zap-log
spec:
restartPolicy: Never
containers:
- command:
- sh
- -c
- |
cat <<EOS
{"level":"info","ts":1257894000,"caller":"sandbox2210433069/prog.go:10","msg":"hello","key1":"val1"}
{"level":"info","ts":1257894000,"caller":"sandbox2210433069/prog.go:13","msg":"world","key1":"val1"}
EOS
image: busybox:1.36.0
name: c |
Hi Takashi, I have a different
You can see that the Thank you for your understanding! |
Thank you for confirming!
If you plan to fix it soon, either is fine for me. I prefer to revert if it takes some time because it might be included in the next release. |
This reverts commit 72a5854.
Let's revert then to avoid any problems #232 I will propose a new PR later when I have more time to investigate possible solutions. |
Addressed #227
This PR adds:
zap-json
input format support