-
Notifications
You must be signed in to change notification settings - Fork 147
allow flexible log parsing and formatting #239
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
… as {{ with $msg := .Message | tryParseJSON }} {{ else }} {{ .Message }} {{ end }}
This reverts commit 1bef6be.
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.
Please add enough tests as needed for changes to generateTemplat
function.
cmd/cmd.go
Outdated
@@ -302,6 +306,7 @@ func (o *options) AddFlags(fs *pflag.FlagSet) { | |||
fs.DurationVarP(&o.since, "since", "s", o.since, "Return logs newer than a relative duration like 5s, 2m, or 3h.") | |||
fs.Int64Var(&o.tail, "tail", o.tail, "The number of lines from the end of the logs to show. Defaults to -1, showing all logs.") | |||
fs.StringVar(&o.template, "template", o.template, "Template to use for log lines, leave empty to use --output flag.") | |||
fs.StringVarP(&o.templateFile, "template-file", "T", o.templateFile, "Path to template to use for log lines, leave empty to use --output flag.") |
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.
Please modify so that --template
and --template-file
options cannot be used at the same time.
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.
Good point, I wanted to add that one overrides the other but that slipped from my mind.
README.md
Outdated
@@ -95,6 +95,7 @@ Supported Kubernetes resources are `pod`, `replicationcontroller`, `service`, `d | |||
`--since`, `-s` | `48h0m0s` | Return logs newer than a relative duration like 5s, 2m, or 3h. | |||
`--tail` | `-1` | The number of lines from the end of the logs to show. Defaults to -1, showing all logs. | |||
`--template` | | Template to use for log lines, leave empty to use --output flag. | |||
`--template-file`, `-T` | | Path to template to use for log lines, leave empty to use --output flag. |
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.
Please add examples using this change to https://github.com/stern/stern#examples.
@@ -348,6 +360,13 @@ func (o *options) generateTemplate() (*template.Template, error) { | |||
} | |||
return string(b), nil | |||
}, | |||
"tryParseJSON": func(text string) map[string]interface{} { |
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.
Could you add the new functions to the "templates" section in README.md?
https://github.com/stern/stern#templates
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.
Definitely, thank you for pointing it out 🙇
cmd/cmd.go
Outdated
@@ -365,9 +384,41 @@ func (o *options) generateTemplate() (*template.Template, error) { | |||
} | |||
return strings.TrimSuffix(string(b), "\n"), nil | |||
}, | |||
"formatTsRFC3339Nano": func(ts any) string { |
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 think the Ts
part does not give additional information for users, so what do you think about simply naming it rfc3339Nano
or toRFC3339Nano
?
{{ colorGreen (rfc3339Nano $msg.ts) }}
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.
Sounds good to me, I don't have a strong opinion here 👍
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.
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.
👍 adding all of them them
Co-authored-by: Takashi Kusumi <tkusumi@zlab.co.jp>
…le of advanced parsing JSON logs
@tksm @superbrothers Thank you so much for the review! NOTE: I added |
Thank you for the changes! I added minor comments below, but it looks good to me other than these. Could you rebase it to allow CI to check? |
Co-authored-by: Takashi Kusumi <tkusumi@zlab.co.jp>
Thank you so for the suggestions! I merged |
Thank you. Either rebase or merge is fine with me. |
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 👍
Addresses #227
This is an alternative solution to #228
Instead of hardcoding template in the code, I suggest adding a bunch of helper functions and a way to load template from disk.
This PR adds:
formatTsRFC3339Nano
tryParseJSON
function that does not return an error--template-file/-T
to load template from diskSample template that I use:
It allows me to have an alias
alias stern="stern -T ~/.stern.tpl"
which support parsing both JSON and non-JSON logs.