Skip to content

Missing log level keys and fallback #263

@hogklint

Description

@hogklint

What happened:
When using --template with the function levelColor it prints this error, without the log entry, when encountering level keys it's not familiar with.

expanding template failed: template: log:1:148: executing "log" at <levelColor $d.levelname>: error calling levelColor: runtime error: invalid memory address or nil pointer dereference

What you expected to happen:
I expect the most common level keys to be handled and a fallback to exist instead of generating an error.

How to reproduce it (as minimally and precisely as possible):
Produce "warning" and "critical" logs from a Python application. These two levelnames are not handled.

If stern could read from stdin (I couldn't find that it does at least) it could have looked something like this.

echo '{"levelname": "warning"}' | stern --template='{{with $d := .Message | tryParseJSON}}{{levelColor $d.levelname}}{{end}}{{"\n"}}'

Anything else we need to know?:
I'm submitting a PR for this in a moment.

Environment:

  • stern version (use stern --version):
version: 1.24.0
commit: 68305a0ab1383053166b7b606ad167b43fc6b329
built at: 2023-03-12T06:50:44Z

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions