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

Print nice journal messages #303

Closed
wants to merge 2 commits into from

Conversation

adombeck
Copy link
Contributor

@adombeck adombeck commented Jan 9, 2025

They used to look like this

Jan 07 14:27:26 ubuntu authd-msentraid.authd-msentraid[44768]: time=2025-01-07T14:27:26.272+01:00 level=INFO msg="Serving requests as com.ubuntu.authd.MSEntraID"

now they look like this

Jan 09 17:48:31 ubuntu authd-msentraid[8991]: Serving requests as com.ubuntu.authd.MSEntraID

and also use the corresponding journal priorities so that those can be filtered and are displayed as expected (i.e. with color and boldness).

UDENG-4523

@adombeck adombeck force-pushed the UDENG-4523-improve-logs-printed-to-the-journal branch from b60304d to 72a88fd Compare January 9, 2025 17:12
@adombeck adombeck marked this pull request as ready for review January 9, 2025 17:42
@adombeck adombeck requested a review from a team as a code owner January 9, 2025 17:42
Copy link
Member

@didrocks didrocks left a comment

Choose a reason for hiding this comment

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

I am a little bit uneasy with a special formatting if the output is not a TTY:

Maybe it’s not the journal, and you want just to redirect to a file, so you run the executable, you end up with the logs in one way, you rerun and redirect to a file, and the formatting is different! I think if we want different formatting, then only have this journal format when under systemd to the journal (even if in general, contrary to previous syslog, you shouldn’t really worry about the output formatting, but I agree the time stuttering is something to fix.

Also, we should have tests for all of those (formatting writing, ensuring there is no date for the journal) and detection.

@adombeck adombeck force-pushed the UDENG-4523-improve-logs-printed-to-the-journal branch from 72a88fd to 2b3f32f Compare January 10, 2025 10:11
@adombeck
Copy link
Contributor Author

adombeck commented Jan 10, 2025

Maybe it’s not the journal, and you want just to redirect to a file

Valid point. Taking a step back, maybe we don't need to support anything else than logging to the journal (for now)? At least I've never had a use case to run the broker (or authd) directly instead of via the systemd service - and even if I did, I don't think I would mind reading the logs via the journal. What do you think?

@adombeck
Copy link
Contributor Author

If at some point we have a use case to print the logs to stdout/stderr, we could add a --output <stdout|journal> flag.

@adombeck adombeck requested a review from didrocks January 13, 2025 09:43
They used to look like this

    Jan 07 14:27:26 ubuntu authd-msentraid.authd-msentraid[44768]: time=2025-01-07T14:27:26.272+01:00 level=INFO msg="Serving requests as com.ubuntu.authd.MSEntraID"

now they look like this

    Jan 09 17:48:31 ubuntu authd-msentraid[8991]: Serving requests as com.ubuntu.authd.MSEntraID

and also use the corresponding journal priorities so that those can be
filtered and are displayed as expected (i.e. with color and boldness).
@adombeck adombeck force-pushed the UDENG-4523-improve-logs-printed-to-the-journal branch from 2b3f32f to a92aee9 Compare January 14, 2025 11:55
@adombeck
Copy link
Contributor Author

I am a little bit uneasy with a special formatting if the output is not a TTY

As discussed, I'm now using journal.StderrIsJournalStream() to decide whether to log to the journal or to stderr.

Also, we should have tests for all of those (formatting writing, ensuring there is no date for the journal) and detection.

I tried but didn't manage to get journal.StderrIsJournalStream() to return true in tests. Checking the formatting of the journal seems overkill, because it requires actually fetching messages from the log via journalctl -f (there are no Go binding). I manually tested that the formatting is as expected.

@didrocks
Copy link
Member

I tried but didn't manage to get journal.StderrIsJournalStream() to return true in tests. Checking the formatting of the journal seems overkill, because it requires actually fetching messages from the log via journalctl -f (there are no Go binding). I manually tested that the formatting is as expected.

Ok, so the decision factor may be overkill to test with those constraints. However, I would still like to see test for the formatter itself, which could be easily tested and not relying on the journal for those.

@adombeck
Copy link
Contributor Author

However, I would still like to see test for the formatter itself, which could be easily tested and not relying on the journal for those.

True, we could test the formatter itself. However, if we want to use authd's log package in the broker, those tests would become obsolete, because as @3v1n0 pointed out, in the log package we can just use journal.Print(mapPriority(level), format, args...) instead of a custom handler (see ubuntu/authd#720). I think it would make sense to re-use authd's log package in the broker, firstly for consistency and secondly because it's more comfortable to use (especially the Debugf, Infof etc. methods are more comfortable to use than slog.Debug(fmt.Sprintf(...))). WDYT?

@adombeck
Copy link
Contributor Author

True, we could test the formatter itself. However, if we want to use authd's log package in the broker, those tests would become obsolete, because as @3v1n0 pointed out, in the log package we can just use journal.Print(mapPriority(level), format, args...) instead of a custom handler (see ubuntu/authd#720). I think it would make sense to re-use authd's log package in the broker, firstly for consistency and secondly because it's more comfortable to use (especially the Debugf, Infof etc. methods are more comfortable to use than slog.Debug(fmt.Sprintf(...))). WDYT?

We decided out-of-band to try using the log package from authd instead

@adombeck
Copy link
Contributor Author

Superseded by #315

@adombeck adombeck closed this Jan 14, 2025
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.

2 participants