You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, it is possible to do an AND filter in journalctl. It would be great to have other operators as well. In particular, negating operators would be great, to eg, silence (in journalctl output) a particularly noisy unit when investigating an issue. In other words, being able to say journalctl --follow --system --not --unit iamnoisy.service (or some other equivalent invocation) would be very useful.
In more general terms, having AND, OR, NOT and grouping of the operations would be awesome.
Reacted by Brandon Philips, redbeard, Chance Zibolski, cgag, Colin Hom, Tobias Wolf, Ben Creasy, Lucas Werkmeister, Brian Morisette, bl33pbl0p and 121 more
the character "+" may appear as a separate word between other terms on the command line. This causes all matches before and after to be combined in a disjunction (i.e. logical OR).
...
If the separator "+" is used, two expressions may be combined in a logical OR. The following will show all messages from the Avahi service process with the PID 28097 plus all messages from the D-Bus service (from any of its processes):
Would negation support harm performance in all cases or just when negation is invoked? If it's the latter, I suspect most of us would be happy to have poor support over no support. IMHO, this is the most glaring omission in all of journalctl.
I looked into extending sd_journal_add_match today to accept a negative match, something like "_SYSTEMD_UNIT!=iamnoisy.service". I don't think there's a good way to make these kinds of queries work well with the existing indexes, which appear to just be a hash map to the first and last instance of a value, short of turning them into B+ trees, which certainly isn't worth the effort.
But I think that's fine, the average use case of a negation like this is to filter out some slightly annoying service or some specific message that makes it hard to read logs on a system. To that end, a solution that simply uses the existing Match setup and adds a list of negation matches to the existing AND Match should work, the idea being that it finds the next message matching its other members before making sure none of the negation members apply, and if they do it starts over again finding a new next next message.
Yeah, that ends up meaning the time could be linear with the number of messages matching the negation cases, but it would only affect when negations are added to the match.
I'm planning on trying to put together something that works for sd_add_match tomorrow to see if it's at all useful.
Reacted by John Florian, Laurent Rineau, Gabriel de Perthuis, Georg Sauthoff, Jeff, Bram Stolk, Cody Piersall and Peter Upton
Hi @gerow and others, have you been working on a patch yet? Being able to filter noise from journalctl -f would be very valuable. As for the interface to journalctl, the ! character has special behavior in Bash and maybe other shells. What about using minus (-) instead? Since it could be misinterpreted as an option -- might be necessary though to stop that. Examples of different options:
journalctl -f -u -iamnoisy or journalctl -f -u '!iamnoisy' would also be a great shorthand. Does not have to be very efficient, if it reads the full journal so be it.
Reacted by Stephane Fillod, hprem, Binar Web, Kanishkavikram Purohit and K3A
$ journalctl -b -p4 --no-pager | grep --color=always -v "/usr/libexec/gdm-x-session" | less -R
it is still possible to filter using pipe into grep but this sacrifices the color output because
output is not to a tty. That coloring is important to process the large amount of info in the log.
some commands include an option such as "--color=always" e.g. grep.
then you could feed the output of grep into "ls -R" for a pager. The -R option preserves color.
journalctl could benefit from an option analogous to grep's "--color=always"
Suggested feature enhancement please.
We can think of each match as a set of log entries. And the existing grammar of A + B means the union set of A and B. As an extension of this, we can also use A - B to represent the difference of A and B (entries in A but not in B), and use - A for all entries not in A.
@huww98 this would be great to have. Might I propose also allowing or, and, and not as acceptable alternates?
I could type that faster than checking the man page if I'm not confident in my memory of which is which. I already tend to forget that and is the implied operator and that + actually means or rather than and.
What about relying on an enhanced PATTERN processing of the --unit= option to do the filtering ?
Right now the PATTERN processing is performed by fnmatch(), a limited one FNM_NOESCAPE.
I suggest the processing to be done by PCRE2 instead (where available), pretty much like the --grep option (see #7881).
Example of usage :
journalctl -u '(?!iamnoisy.service)'
I agree with @Lekensteyn, it does not have to be very efficient, if it reads the full journal so be it.
With #24058, journald is getting log filtering on input, via LogIncludeRegex= and LogExcludeRegex=.
This issue wants log filtering on output.
Could we re-use some parts of the cother commit?
Reacted by Stephane Fillod, Lukáš Zapletal and Kanishkavikram Purohit
It doesn't appear any progress has been made on this, and I know I really desire to have exclusion in the API, not just some coding within "journalctl"
It passes all journalctl options through, reads -o json, drops the entries that match an exclusion and prints the rest in journalctl's format (a port of the formatting code in logs-show.c; CI compares the output byte for byte with journalctl 255). For example:
Exclusions are FIELD!=VALUE, --exclude-unit and --exclude-user-unit (the same fields as -u), -T/--exclude-identifier for systemd older than 256, and --exclude-grep. -n N counts the entries that are left after the exclusions, which | grep -v cannot do.
Limitations: it filters after journalctl has read the entries, so it is slower (about 3.5 times journalctl's runtime when printing a whole journal, not noticeable with -f or -n). It supports only the short*, cat and json output modes. It does not help anyone who needs NOT in sd_journal_add_match().
Activity
daurnimator commentedon Feb 25, 2016
You can already do 'OR'.
From man page:
...
poettering commentedon Apr 15, 2016
Yeah, you have one level of AND and one of OR exposed in journalctl. (internally there are more levels).
NOT is currently not supported, but there's already an item for it in the TODO list. It's hard to optimize the indexes for though...
[-]RFE: add other logical operators to journalctl besides AND[/-][+]RFE: add NOT operator to journal matching[/+]glunardi commentedon Jan 24, 2018
Could you please provide an update on this? Have changes made this more or less likely to be implemented since the last comment?
+1 for this.
poettering commentedon Jan 24, 2018
no updates. nobody worked on this so far. situation has not changed. Somebody needs to sit down and do the work.
ilf commentedon May 2, 2018
This would be really helpful!
zantetsuken88 commentedon May 31, 2018
+1 for this. Trying to work around not having this functionality is causing us some real headaches
jflorian commentedon Mar 29, 2019
Would negation support harm performance in all cases or just when negation is invoked? If it's the latter, I suspect most of us would be happy to have poor support over no support. IMHO, this is the most glaring omission in all of journalctl.
gerow commentedon Mar 31, 2019
I looked into extending sd_journal_add_match today to accept a negative match, something like "_SYSTEMD_UNIT!=iamnoisy.service". I don't think there's a good way to make these kinds of queries work well with the existing indexes, which appear to just be a hash map to the first and last instance of a value, short of turning them into B+ trees, which certainly isn't worth the effort.
But I think that's fine, the average use case of a negation like this is to filter out some slightly annoying service or some specific message that makes it hard to read logs on a system. To that end, a solution that simply uses the existing Match setup and adds a list of negation matches to the existing AND Match should work, the idea being that it finds the next message matching its other members before making sure none of the negation members apply, and if they do it starts over again finding a new next next message.
Yeah, that ends up meaning the time could be linear with the number of messages matching the negation cases, but it would only affect when negations are added to the match.
I'm planning on trying to put together something that works for sd_add_match tomorrow to see if it's at all useful.
Lekensteyn commentedon Apr 15, 2019
Hi @gerow and others, have you been working on a patch yet? Being able to filter noise from
journalctl -fwould be very valuable. As for the interface tojournalctl, the!character has special behavior in Bash and maybe other shells. What about using minus (-) instead? Since it could be misinterpreted as an option--might be necessary though to stop that. Examples of different options:journalctl -f -u -iamnoisyorjournalctl -f -u '!iamnoisy'would also be a great shorthand. Does not have to be very efficient, if it reads the full journal so be it.garberw commentedon Apr 21, 2020
$ journalctl -b -p4 --no-pager | grep --color=always -v "/usr/libexec/gdm-x-session" | less -R
it is still possible to filter using pipe into grep but this sacrifices the color output because
output is not to a tty. That coloring is important to process the large amount of info in the log.
some commands include an option such as "--color=always" e.g. grep.
then you could feed the output of grep into "ls -R" for a pager. The -R option preserves color.
journalctl could benefit from an option analogous to grep's "--color=always"
Suggested feature enhancement please.
lucaswerkmeister commentedon Apr 21, 2020
That already exists as the
SYSTEMD_COLORSenvironment variable.g2p commentedon May 27, 2020
I'm using jq to work around the lack of boolean expressions:
While jq is powerful, the queries are a lot more verbose and take trial and error to build.
The output isn't formatted, so I use &MESSAGE / &^M in less to toggle between a terse display and showing all fields.
huww98 commentedon Mar 5, 2021
We can think of each match as a set of log entries. And the existing grammar of
A + Bmeans the union set of A and B. As an extension of this, we can also useA - Bto represent the difference of A and B (entries in A but not in B), and use- Afor all entries not in A.To this end, I propose the following grammar:
For example:
- A=1meansNOT A=1A=1 - B=2meansA=1 AND (NOT B=2)- ( A=1 + B=2 )meansNOT (A=1 OR B=2)- A=1 B=2meansNOT (A=1 AND B=2)A=1 + - B=2meansA=1 OR (NOT B=2)What do you think? And I'm also willing to spend some days implementing this.
jflorian commentedon Mar 5, 2021
@huww98 this would be great to have. Might I propose also allowing
or,and, andnotas acceptable alternates?I could type that faster than checking the man page if I'm not confident in my memory of which is which. I already tend to forget that and is the implied operator and that
+actually means or rather than and.fillods commentedon May 4, 2021
What about relying on an enhanced
PATTERNprocessing of the--unit=option to do the filtering ?Right now the PATTERN processing is performed by
fnmatch(), a limited oneFNM_NOESCAPE.I suggest the processing to be done by
PCRE2instead (where available), pretty much like the--grepoption (see #7881).Example of usage :
journalctl -u '(?!iamnoisy.service)'I agree with @Lekensteyn, it does not have to be very efficient, if it reads the full journal so be it.
Disclaimer: I'm a kind of newcomer to
journalctl.ilf commentedon Oct 14, 2022
With #24058, journald is getting log filtering on input, via
LogIncludeRegex=andLogExcludeRegex=.This issue wants log filtering on output.
Could we re-use some parts of the cother commit?
daandemeyer commentedon Oct 14, 2022
@ilf Unfortunately not, log filtering on output is a completely different issue altogether.
woodz- commentedon Nov 30, 2022
besides
journalctl, will this negation feature also be considered on the APIsd_journal_add_match()?stolk commentedon Apr 22, 2023
This does not preserve colours for me.
Binly42 commentedon Dec 2, 2023
@stolk for me,the environment variable works~
MrPippin66 commentedon Apr 5, 2024
It doesn't appear any progress has been made on this, and I know I really desire to have exclusion in the API, not just some coding within "journalctl"
Popyllol commentedon Aug 22, 2024
Hi, is this RFE still on the list? Is there anything done so far?
Jwrede commentedon Oct 27, 2026
Until journalctl supports this, I wrote a small wrapper that does the filtering on the client side: https://github.com/Jwrede/journalctl-not
It passes all journalctl options through, reads
-o json, drops the entries that match an exclusion and prints the rest in journalctl's format (a port of the formatting code inlogs-show.c; CI compares the output byte for byte with journalctl 255). For example:Exclusions are
FIELD!=VALUE,--exclude-unitand--exclude-user-unit(the same fields as-u),-T/--exclude-identifierfor systemd older than 256, and--exclude-grep.-n Ncounts the entries that are left after the exclusions, which| grep -vcannot do.Limitations: it filters after journalctl has read the entries, so it is slower (about 3.5 times journalctl's runtime when printing a whole journal, not noticeable with
-for-n). It supports only the short*, cat and json output modes. It does not help anyone who needs NOT insd_journal_add_match().