Skip to content

RFE: add NOT operator to journal matching #2720

Open

Description

@fsateler

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.

Activity

  1. added
    RFE 🎁
    on Feb 23, 2016
  2. daurnimator commented on Feb 25, 2016

    @daurnimator
    Contributor

    You can already do 'OR'.
    From man page:

    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):

    journalctl _SYSTEMD_UNIT=avahi-daemon.service _PID=28097 + _SYSTEMD_UNIT=dbus.service
    
  3. poettering commented on Apr 15, 2016

    @poettering
    Member

    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...

  4. changed the title [-]RFE: add other logical operators to journalctl besides AND[/-] [+]RFE: add NOT operator to journal matching[/+] on Apr 15, 2016
  5. glunardi commented on Jan 24, 2018

    @glunardi

    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.

  6. poettering commented on Jan 24, 2018

    @poettering
    Member

    no updates. nobody worked on this so far. situation has not changed. Somebody needs to sit down and do the work.

  7. ilf commented on May 2, 2018

    @ilf

    This would be really helpful!

  8. zantetsuken88 commented on May 31, 2018

    @zantetsuken88

    +1 for this. Trying to work around not having this functionality is causing us some real headaches

  9. jflorian commented on Mar 29, 2019

    @jflorian

    It's hard to optimize the indexes for though...

    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.

  10. gerow commented on Mar 31, 2019

    @gerow

    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.

  11. Lekensteyn commented on Apr 15, 2019

    @Lekensteyn
    Contributor

    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 -- -_SYSTEMD_UNIT=iamnoisy.service
    journalctl -f _SYSTEMD_UNIT-=iamnoisy.service
    journalctl -f '!_SYSTEMD_UNIT=iamnoisy.service'
    journalctl -f '_SYSTEMD_UNIT!=iamnoisy.service'
    

    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.

  12. garberw commented on Apr 21, 2020

    @garberw

    $ 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.

  13. lucaswerkmeister commented on Apr 21, 2020

    @lucaswerkmeister
    Contributor

    That already exists as the SYSTEMD_COLORS environment variable.

    SYSTEMD_COLORS=true journalctl -b -p4 | grep -vF /usr/libexec/gdm-x-session | less -R
  14. g2p commented on May 27, 2020

    @g2p
    Contributor

    I'm using jq to work around the lack of boolean expressions:

    journalctl -o json |jq '
      select(
        (has("_SYSTEMD_UNIT") and (._SYSTEMD_UNIT |in({"noisy.service": "", "chatty.service": ""}))
        or (._TRANSPORT == "kernel" and (.MESSAGE |startswith("[PREFIX]"))))
      |not)
      ' |less
    

    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.

  15. huww98 commented on Mar 5, 2021

    @huww98

    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.

    To this end, I propose the following grammar:

    Expr : Term ( ( '+' | '-' ) Term )*
    Term : [ '+' | '-' ] Match*
    Match : Match_discrete | '(' Expr ')'

    For example:

    1. - A=1 means NOT A=1
    2. A=1 - B=2 means A=1 AND (NOT B=2)
    3. - ( A=1 + B=2 ) means NOT (A=1 OR B=2)
    4. - A=1 B=2 means NOT (A=1 AND B=2)
    5. A=1 + - B=2 means A=1 OR (NOT B=2)

    What do you think? And I'm also willing to spend some days implementing this.

  16. jflorian commented on Mar 5, 2021

    @jflorian

    @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.

  17. fillods commented on May 4, 2021

    @fillods

    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.

    Disclaimer: I'm a kind of newcomer to journalctl.

  18. ilf commented on Oct 14, 2022

    @ilf

    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?

  19. daandemeyer commented on Oct 14, 2022

    @daandemeyer
    Collaborator

    @ilf Unfortunately not, log filtering on output is a completely different issue altogether.

  20. woodz- commented on Nov 30, 2022

    @woodz-

    besides journalctl, will this negation feature also be considered on the API sd_journal_add_match()?

  21. stolk commented on Apr 22, 2023

    @stolk

    journalctl -b -p4 --no-pager | grep --color=always -v "/usr/libexec/gdm-x-session" | less -R

    This does not preserve colours for me.

  22. Binly42 commented on Dec 2, 2023

    @Binly42

    journalctl -b -p4 --no-pager | grep --color=always -v "/usr/libexec/gdm-x-session" | less -R

    This does not preserve colours for me.

    @stolk for me,the environment variable works~

  23. MrPippin66 commented on Apr 5, 2024

    @MrPippin66

    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"

  24. Popyllol commented on Aug 22, 2024

    @Popyllol

    Hi, is this RFE still on the list? Is there anything done so far?

  25. Jwrede commented on Oct 27, 2026

    @Jwrede

    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 in logs-show.c; CI compares the output byte for byte with journalctl 255). For example:

    journalctl-not -f --exclude-unit=iamnoisy.service
    journalctl-not -b -p warning _TRANSPORT!=audit --exclude-grep='ACPI'
    

    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().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    RFE 🎁journal

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions