Skip to content

journalctl: add --exclude-identifier option - #23086

Merged
bluca merged 1 commit into
systemd:mainfrom
zhmylove:exclude_and_strip
Jan 4, 2024
Merged

bluca merged 1 commit into
systemd:mainfrom
zhmylove:exclude_and_strip

Conversation

@zhmylove

@zhmylove zhmylove commented Apr 15, 2022 •

Copy link
Copy Markdown
Contributor

Exclude syslog identifiers

There is -t (--identifier=STRING) journalctl option which allows to specify SYSLOG_IDENTIFIER in a handy manner. This option could be used several times, each of them appends new layer of OR condition. A lot of journalctl(1) users are using it to show log entries related to only particular syslog identifiers. There are a lot of situations when it's required not to select a particular syslog identifiers, but to exclude some of them leaving all the others visible. There is a real problem in implementing this using Match* functionality built into journals as it match any part of the object by hash, while this could be effectively achieved during the journal entries output. This PR introduces -T (--exclude=STRING) option which implements this functionality for output_short (output_verbose, output_json, and others are intentionally left intact so far).
Example:

# ./build/journalctl |head -5
Apr 15 02:05:40 rc1 systemd-logind[364]: Removed session c11.
Apr 15 02:06:01 rc1 CRON[23073]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr 15 02:06:01 rc1 CRON[23074]: (root) CMD (/root/redis/balancer.pl)
Apr 15 02:06:01 rc1 CRON[23073]: pam_unix(cron:session): session closed for user root
Apr 15 02:06:39 rc1 su[23076]: (to root) korg on pts/0
# ./build/journalctl -T CRON |head -5
Apr 15 02:05:40 rc1 systemd-logind[364]: Removed session c11.
Apr 15 02:06:39 rc1 su[23076]: (to root) korg on pts/0
Apr 15 02:06:39 rc1 su[23076]: pam_unix(su-l:session): session opened for user root by korg(uid=1000)
Apr 15 02:06:39 rc1 systemd-logind[364]: New session c12 of user root.
Apr 15 02:06:39 rc1 systemd[1]: Started Session c12 of user root.
# ./build/journalctl -T CRON -T su |head -5
Apr 15 02:05:40 rc1 systemd-logind[364]: Removed session c11.
Apr 15 02:06:39 rc1 systemd-logind[364]: New session c12 of user root.
Apr 15 02:06:39 rc1 systemd[1]: Started Session c12 of user root.
Apr 15 02:06:47 rc1 systemd[1]: session-c12.scope: Succeeded.
Apr 15 02:06:47 rc1 systemd-logind[364]: Session c12 logged out. Waiting for processes to exit.

Comment thread src/basic/string-util.c Resolved

@bluca bluca left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

manpage update is missing. Also would be good to have a test/unit test for this, there should be already tests for the journal. Finally, rebase and squash fixup commits, so that history is clean. Would be good to have the two new options added as separate commits, since they are not related.

Comment thread src/shared/logs-show.c Resolved
Comment thread src/journal/journalctl.c Resolved
Comment thread src/journal/journalctl.c Resolved
Comment thread src/basic/string-util.c Resolved
Comment thread .gitignore Resolved
Comment thread src/journal/journalctl.c Resolved
@bluca bluca added the reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks label Apr 15, 2022
@zhmylove

zhmylove commented Apr 15, 2022 •

Copy link
Copy Markdown
Contributor Author

@bluca many thanks for your review!

Would be good to have the two new options added as separate commits, since they are not related.

Should I send them as different PRs or just 2 separate commits under one PR?

I'll work on man/tests later on.

@bluca

bluca commented Apr 15, 2022

Copy link
Copy Markdown
Member

@bluca many thanks for your review!

Would be good to have the two new options added as separate commits, since they are not related.

Should I send them as different PRs or just 2 separate commits under one PR?

I'll work on man/tests later on.

same PR is fine by me

@zhmylove
zhmylove force-pushed the exclude_and_strip branch 2 times, most recently from 6e08d96 to aad68b2 Compare April 15, 2022 15:53
@zhmylove

Copy link
Copy Markdown
Contributor Author

@bluca I've re-ordered changes into 3 separate commits and added manpage update.
Still looking for a way to add unit tests. So far I can't find any of them for journalctl.

@bluca bluca removed the reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks label Apr 15, 2022
@bluca

bluca commented Apr 15, 2022

Copy link
Copy Markdown
Member

@bluca I've re-ordered changes into 3 separate commits and added manpage update. Still looking for a way to add unit tests. So far I can't find any of them for journalctl.

There are tests for the journal in test/units/testsuite-04.sh

@zhmylove
zhmylove force-pushed the exclude_and_strip branch from aad68b2 to f01fb3a Compare April 16, 2022 08:22
@zhmylove

zhmylove commented Apr 16, 2022 •

Copy link
Copy Markdown
Contributor Author

@bluca thank you for pointing me out on them.

I've added unit test for --exclude and I belive we're done with this part.

I'd like to discuss with you the test for --strip. Usually we use systemd-cat to add log messages into the log. Unfortunately it's not able of adding multiline messages as it splits stdin by newlines:

# systemd-cat -t "$ID" sh -c 'echo line1 ;echo line2'
# journalctl -q -b -t "$ID"
Apr 16 11:41:57 rc1 q[29341]: line1
Apr 16 11:41:57 rc1 q[29341]: line2

We can't rely on systemd-journal-remote as HAVE_MICROHTTPD could be false, like on my system.
Neither can we rely on logger(1) which could be missing on several systems. For example, on Debian-based it's part of bsdutils package, not the util-linux, and I believe it's not installed by default.
What will be the best way to create a multiline message in journal from the testsuite?

As python >= 3.5 is build-requirement for systemd we can try to utilize it like:

# python -c 'import syslog; syslog.openlog("qqq"); syslog.syslog("line1\nline2")'
# journalctl -q -b -t qqq
Apr 16 12:04:34 rc1 qqq[29513]: line1
                                line2

But I'm not sure it'll be available inside the testsuite. Should we add python to BASICTOOLS of test/test-functions?

Thanks!

@bluca

bluca commented Apr 16, 2022

Copy link
Copy Markdown
Member

@bluca thank you for pointing me out on them.

I've added unit test for --exclude and I belive we're done with this part.

I'd like to discuss with you the test for --strip. Usually we use systemd-cat to add log messages into the log. Unfortunately it's not able of adding multiline messages as it splits stdin by newlines:

# systemd-cat -t "$ID" sh -c 'echo line1 ;echo line2'
# journalctl -q -b -t "$ID"
Apr 16 11:41:57 rc1 q[29341]: line1
Apr 16 11:41:57 rc1 q[29341]: line2

We can't rely on systemd-journal-remote as HAVE_MICROHTTPD could be false, like on my system. Neither can we rely on logger(1) which could be missing on several systems. For example, on Debian-based it's part of bsdutils package, not the util-linux, and I believe it's not installed by default. What will be the best way to create a multiline message in journal from the testsuite?

As python >= 3.5 is build-requirement for systemd we can try to utilize it like:

# python -c 'import syslog; syslog.openlog("qqq"); syslog.syslog("line1\nline2")'
# journalctl -q -b -t qqq
Apr 16 12:04:34 rc1 qqq[29513]: line1
                                line2

But I'm not sure it'll be available inside the testsuite. Should we add python to BASICTOOLS of test/test-functions?

Thanks!

Use logger and simply check if it's available with command -v, and skip the test if not - I'll make sure it's installed in the CI

@zhmylove
zhmylove force-pushed the exclude_and_strip branch from f01fb3a to aef1caf Compare April 16, 2022 09:53
@zhmylove

Copy link
Copy Markdown
Contributor Author

@bluca added test for --strip. Thank you!

@bluca

bluca commented Apr 16, 2022

Copy link
Copy Markdown
Member

@bluca added test for --strip. Thank you!

You need to add a test_append_files in test/TEST-04-JOURNAL/test.sh that installs logger if it's available - see test/TEST-43-PRIVATEUSER-UNPRIV/test.sh as an example

@zhmylove
zhmylove force-pushed the exclude_and_strip branch 2 times, most recently from e4ebadd to eb6a37c Compare April 16, 2022 13:24
@zhmylove

Copy link
Copy Markdown
Contributor Author

@bluca this time included. Thanks!

@bluca

bluca commented Apr 16, 2022

Copy link
Copy Markdown
Member

@bluca bluca added the ci-fails/needs-rework 🔥 Please rework this, the CI noticed an issue with the PR label Apr 16, 2022
@bluca

bluca commented Apr 16, 2022

Copy link
Copy Markdown
Member

@zhmylove
zhmylove force-pushed the exclude_and_strip branch 2 times, most recently from e27669a to f117805 Compare April 17, 2022 12:59
Comment thread src/basic/string-util.h Resolved
Comment thread src/basic/string-util.c Resolved
Comment thread src/journal/journalctl.c Resolved
Comment thread src/journal/journalctl.c Resolved
Comment thread src/journal/journalctl.c Resolved
@yuwata yuwata added reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks and removed ci-fails/needs-rework 🔥 Please rework this, the CI noticed an issue with the PR labels Apr 17, 2022
@zhmylove
zhmylove force-pushed the exclude_and_strip branch 2 times, most recently from 77c6f18 to 34e4493 Compare April 17, 2022 14:58
@bluca bluca added good-to-merge/waiting-for-ci 👍 PR is good to merge, but CI hasn't passed at time of review. Please merge if you see CI has passed and removed documentation util-lib tests good-to-merge/with-minor-suggestions labels Jan 3, 2024
@bluca bluca changed the title journalctl: add --exclude option journalctl: add --exclude-identifier option Jan 3, 2024
Comment thread shell-completion/bash/journalctl Resolved
Comment thread shell-completion/bash/journalctl Resolved
Comment thread src/journal/journalctl.c Resolved
Comment thread src/journal/journalctl.c Resolved
Comment thread src/libsystemd/sd-journal/sd-journal.c Resolved
Comment thread src/shared/logs-show.c Resolved
@yuwata yuwata added reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks and removed good-to-merge/waiting-for-ci 👍 PR is good to merge, but CI hasn't passed at time of review. Please merge if you see CI has passed labels Jan 3, 2024
@zhmylove
zhmylove force-pushed the exclude_and_strip branch 2 times, most recently from bcf09c3 to b53b1aa Compare January 4, 2024 12:17
@github-actions github-actions Bot added please-review PR is ready for (re-)review by a maintainer and removed reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks labels Jan 4, 2024
@zhmylove

zhmylove commented Jan 4, 2024 •

Copy link
Copy Markdown
Contributor Author

@YHNdnzj

Hmm, this is not yet resolved?

Already resolved.

Also, how does this relate to #12592 ?

The idea looks similar, people do really want this functionality for a long time.
But that PR is probably abandoned by the author for last three years.
There were several of them, I think.

@bluca Thanks a lot for your approval!

@yuwata Thank you for the comments, everything is fixed now. Some CI failed due to GitHub API rate limit, nothing related to code :(

@yuwata yuwata left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Currently, the new option only works with output type which uses output_short().
I think such feature should be implemented in sd-journal side.
But, this is a good start point. Let's take the current way, and extend the feature later.

Comment thread src/journal/journalctl.c Resolved
@yuwata yuwata added reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks and removed please-review PR is ready for (re-)review by a maintainer labels Jan 4, 2024

@yuwata yuwata left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. Thank you, and sorry for late review process.

@yuwata yuwata added good-to-merge/waiting-for-ci 👍 PR is good to merge, but CI hasn't passed at time of review. Please merge if you see CI has passed and removed reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks labels Jan 4, 2024
@bluca
bluca merged commit 25aa35d into systemd:main Jan 4, 2024
jackpot51 pushed a commit to pop-os/systemd that referenced this pull request May 7, 2024
The logger utility is now used in TEST-04-JOURNAL.

See systemd/systemd#23086
@keszybz keszybz removed the good-to-merge/waiting-for-ci 👍 PR is good to merge, but CI hasn't passed at time of review. Please merge if you see CI has passed label Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.