Skip to content

Conversation

niamster
Copy link
Contributor

@niamster niamster commented Feb 26, 2023

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:

  • various color functions
  • TS format function formatTsRFC3339Nano
  • tryParseJSON function that does not return an error
  • --template-file/-T to load template from disk

Sample template that I use:

{{color .PodColor .PodName}} {{color .ContainerColor .ContainerName}} {{ with $msg := .Message | tryParseJSON }}[{{ colorGreen (formatTsRFC3339Nano $msg.ts) }}] {{ levelColor $msg.level }} ({{ colorCyan $msg.caller }}) {{ $msg.msg }}{{ else }} {{ .Message }} {{ end }}

It allows me to have an alias alias stern="stern -T ~/.stern.tpl" which support parsing both JSON and non-JSON logs.

Copy link
Member

@superbrothers superbrothers left a 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.")
Copy link
Member

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.

Copy link
Contributor Author

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.
Copy link
Member

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{} {
Copy link
Contributor

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

Copy link
Contributor Author

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 {
Copy link
Contributor

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) }}

Copy link
Contributor Author

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 👍

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for changing! Could you add the functions such as toRFC3339Nano to the table in the templates section?

image

Copy link
Contributor Author

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

@niamster
Copy link
Contributor Author

niamster commented Mar 3, 2023

@tksm @superbrothers Thank you so much for the review!
I tried to address your comments, please let me know if I missed something.

NOTE: I added toUTC function to simplify testing. It still might be useful in some case I think.

@tksm
Copy link
Contributor

tksm commented Mar 4, 2023

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?

@niamster
Copy link
Contributor Author

niamster commented Mar 4, 2023

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?

Thank you so for the suggestions! I merged master into my branch. Do you want me to rebase instead (TBH I'm not a huge fan of rebase since it messes up with GH quite often, so we loose essential traces)?

@tksm
Copy link
Contributor

tksm commented Mar 4, 2023

Thank you so for the suggestions! I merged master into my branch. D you want me to rebase instead (TBH I'm not a huge fan of rebase since it messes up with GH quite often, so we loose essential traces)?

Thank you. Either rebase or merge is fine with me.

Copy link
Contributor

@tksm tksm left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@tksm tksm merged commit 12a55fa into stern:master Mar 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants