-
Notifications
You must be signed in to change notification settings - Fork 140
Allow levelColor
template function to parse numbers
#321
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
Hi, thanks for the PR! I think it would be best to avoid introducing a behavior change for existing users. How about creating a new function, such as |
Thanks for your reply.
If you tail two containers simultaneously, one which uses string levels, and one which uses numeric levels, how could one solve that if there are different color functions with the current behavior of If adding a Edit: And by "this behavior change" I don't mean what's currently in the PR. I like splitting the color functions but to enable versatility it would be good if |
How about calling for l in 10 20 30 40 50 60 70 \"debug\" \"info\" \"warn\" \"error\" \"fatal\";
do echo '{"level": '$l', "msg": "A message"}'
done | \
dist/stern --stdin \
--template='{{with $d := .Message | parseJSON}}[{{or (bunyanLevelColor $d.level) (levelColor $d.level)}}] {{$d.msg}}{{end}}{{"\n"}}' "bunyanLevelColor": func(level any) string {
var lv int64
var err error
switch value := level.(type) {
// tryParseJSON yields json.Number
case json.Number:
lv, err = value.Int64()
if err != nil {
return ""
}
// parseJSON yields float64
case float64:
lv = int64(value)
default:
return ""
}
switch {
case lv < 30:
return color.New(color.FgMagenta).SprintFunc()(lv)
case lv < 40:
return color.New(color.FgBlue).SprintFunc()(lv)
case lv < 50:
return color.New(color.FgYellow).SprintFunc()(lv)
case lv < 60:
return color.New(color.FgRed).SprintFunc()(lv)
case lv < 100:
return color.New(color.FgCyan).SprintFunc()(lv)
default:
return strconv.FormatInt(lv, 10)
}
}, |
Yeah, sure we can go with that. I just want to point out a couple of things.
I'll make the changes, but do let me know if you change your mind. 👍 |
cb5895e
to
66fb679
Compare
I have no objection to changing the |
Ok, cool. 👍 Even adding the error return value doesn't affect the user. The template engine swallows it and prints it. But I'll stick with the single string return. |
Some log facilities output a number as level instead of string.
66fb679
to
9b56bdf
Compare
levelColor discarded log lines if it could not coerce the given value to a string. By accepting anything the user has the option to handle such cases in the template instead.
9b56bdf
to
b69fed5
Compare
All done, let me know if you want to change anything. |
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 🚀
This fixes #320
This PR extends the functionality of the template function
levelColor
to also support numeric values. Try it out withIn case it cannot parse the value as a string or number it currently falls back to returning an empty string without an error. I think this is quite nice as it allows for things like this
{{or (colorLevel $d.level) "N/A"}}
if the level is missing completely. It is however a behavior change.Furthermore, it can be debated whether the levels I've chosen make the most sense. They are set according to what bunyan and pino uses whereas log4j use levels in the 100s. A flag to switch between the two styles perhaps?