Skip to content
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

tk: Format logs with the console log formatter if outputting to a tty #776

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

iainlane
Copy link
Member

@iainlane iainlane commented Oct 7, 2022

Since the switch to structured logging in
cc21323 we now output evaluation errors
all on one line, with escaped newlines:

{"level":"fatal","time":"2022-10-07T12:50:42+01:00","message":"Error: evaluating jsonnet: RUNTIME ERROR: function expected 3 positional argument(s), but got 4\n\t/home/laney/temp/tk/environments/default/main.jsonnet:3:14-56\tobject <anonymous>\n\tField \"bar\"\t\n\tDuring manifestation\t\n"}

It's pretty unreadable. Instead, what we can do is turn on zerolog's
console writer when we're running interactively (precisely, when stderr
is a tty). Then the above message becomes:

12:52PM FTL Error: evaluating jsonnet: RUNTIME ERROR: function expected 3 positional argument(s), but got 4
	/home/laney/temp/tk/environments/default/main.jsonnet:3:14-56	object <anonymous>
	Field "bar"
	During manifestation

which is legible again.

Since the switch to structured logging in
cc21323 we now output evaluation errors
all on one line, with escaped newlines:

```
{"level":"fatal","time":"2022-10-07T12:50:42+01:00","message":"Error: evaluating jsonnet: RUNTIME ERROR: function expected 3 positional argument(s), but got 4\n\t/home/laney/temp/tk/environments/default/main.jsonnet:3:14-56\tobject <anonymous>\n\tField \"bar\"\t\n\tDuring manifestation\t\n"}
```

It's pretty unreadable. Instead, what we can do is turn on zerolog's
console writer when we're running interactively (precisely, when stderr
is a tty). Then the above message becomes:

```
12:52PM FTL Error: evaluating jsonnet: RUNTIME ERROR: function expected 3 positional argument(s), but got 4
	/home/laney/temp/tk/environments/default/main.jsonnet:3:14-56	object <anonymous>
	Field "bar"
	During manifestation
```

which is legible again.
@CLAassistant
Copy link

CLAassistant commented Oct 7, 2022

CLA assistant check
All committers have signed the CLA.

cmd/tk/main.go Outdated
@@ -12,7 +12,7 @@ import (
"github.com/grafana/tanka/pkg/tanka"
)

var interactive = term.IsTerminal(int(os.Stdout.Fd()))
var interactive = term.IsTerminal(int(os.Stderr.Fd()))
Copy link
Member Author

Choose a reason for hiding this comment

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

zerolog outputs to stderr, this is the relevant fd to check

Copy link
Member

Choose a reason for hiding this comment

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

This is used elsewhere though (to tell whether or not the pager should be activated), does checking stdout and stderr do the same thing, or should it be two different vars?

Copy link
Member Author

Choose a reason for hiding this comment

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

that's a good point, I guess the error one should check stderr and the other one can keep on looking at stdout

@iainlane iainlane requested review from a team and julienduchesne October 7, 2022 11:54
@julienduchesne
Copy link
Member

It's not really a TTY problem. We have the same problem in CI. The logs are ugly since #774, the fix should've been to add os.Exit(1), not change to log.Fatal

@iainlane
Copy link
Member Author

iainlane commented Oct 7, 2022

I don't really mind if you want to close this, but this has the effect of making all logs look nice. It's a decent practice if you ask me. But I'm not that wedded to it.

Copy link
Member

@julienduchesne julienduchesne left a comment

Choose a reason for hiding this comment

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

LGTM! Other than the one comment!

cmd/tk/main.go Outdated
@@ -12,7 +12,7 @@ import (
"github.com/grafana/tanka/pkg/tanka"
)

var interactive = term.IsTerminal(int(os.Stdout.Fd()))
var interactive = term.IsTerminal(int(os.Stderr.Fd()))
Copy link
Member

Choose a reason for hiding this comment

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

This is used elsewhere though (to tell whether or not the pager should be activated), does checking stdout and stderr do the same thing, or should it be two different vars?

inteactive -> stdoutIsTTY

add stderrIsTTY

Use the latter for checking if logs should be pretty printed, the former
for other checks, e.g. whether to use a pager.
@iainlane
Copy link
Member Author

@julienduchesne can you help me with the CI failure please? 🙈

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.

None yet

3 participants