Log filters
Log filters are expression-based conditions that allow log messages to be filtered before they are forwarded to external services. With filters, only the messages that match the defined conditions are forwarded to the destination server, reducing noise downstream and limiting bandwidth used by log forwarding.
Log filters are available only in the context of the rsyslog client (profiles::rsyslog_client). For the broader configuration model, see Configuring log forwarding.
How filters are defined and applied
Filters are defined in the Rsyslog Client hash under the Filters property. Defining a filter on its own does not change which messages are forwarded; filters must be applied on a per-server basis. To use a filter, specify it in the server definition (Rsyslog server, Elasticsearch, Datadog, or New Relic).
When multiple filters are specified on a single server, those filters are linked with an OR operator: a log message that passes through at least one of the specified filters is forwarded to the server.
Inside a single filter, the Or and And blocks combine with the following logic:
- Inside the
Orblock, the listed conditions are linked with theoroperator. - Inside the
Andblock, the listed conditions are linked with theandoperator. - Between the
Orblock and theAndblock, anandoperator is used: the message must satisfy theAndexpression AND any of theOrexpressions.
Between multiple filters applied to the same server, the expressions of each filter are linked with the or operator.
Filter definition options
The definition of a filter has the following options:
---
Rsyslog Client:
Filters:
<Filter Name>:
Or:
Contains:
- <keyword>
Not Contains:
- <keyword>
Regex:
- <regex>
Program Name:
- <programname>
Not Program Name:
- <programname>
And:
Contains:
- <keyword>
Not Contains:
- <keyword>
Regex:
- <regex>
Program Name:
- <programname>
Not Program Name:
- <programname>
Each condition inside Or or And accepts a list of values and has the following meaning:
- Contains. The log message must contain the given keyword.
- Not Contains. The log message must not contain the given keyword.
- Regex. The log message must match the given regular expression.
- Program Name. The
programnamefield must exactly match the given value. - Not Program Name. The
programnamefield must not match the given value.
Considerations
- Filters are case sensitive. When defining filters, the words
Hello,hElLo,HELLO, andheLLoare all considered different. - Whitespace is significant. Leading and trailing whitespace characters are considered part of the expression and are not stripped from input data.
- Performance. Using too many filters can impact performance on the node running the rsyslog client. Define filters narrowly and reuse them across servers where possible.
Example: combined And/Or filter
Consider the following filter:
---
Rsyslog Client:
Filters:
'filter_name':
Or:
Contains:
- a
- b
Not Contains:
- c
Regex:
- '*a*regex'
And:
Contains:
- d
Not Contains:
- g
Regex:
- 'another*'
- '*regex*'
When applied to a server, this filter generates the following expression:
if (( $msg contains('d') and not($msg contains('g')) and re_match($msg, 'another*') and re_match($msg, '*regex*')) and ($msg contains('a') or $msg contains('b') or not($msg contains('c')) or re_match($msg, '*a*regex')))
The generated expression has an and condition between the And block and the Or block. The log message must satisfy the And expression and any of the Or expressions.
The log message another d regex passes this filter because:
- It contains
d. - It does not contain
g. - It matches both the
another*and*regex*regular expressions. - The
Orblock also accepts it because it containsa.
Filter scenarios
The following table shows common filter configurations and the rsyslog expressions they generate.
| Scenario | Filter configuration | Additional notes |
|---|---|---|
Forward logs only when the log message contains one of the following words: Hello, World. |
|
Generated expression:if (($msg contains('Hello') or $msg contains('World')))
Accepted: Hello, Hello World, The World is round Not accepted: Hi, Hey, Word count. |
Forward logs only when the log message contains the word Hello or does not contain the word World. |
|
Generated expression:if (($msg contains('Hello') or not($msg contains('World'))))
Accepted: Hello, Hello World, Hello Goodbye, Yellow, Hi Not accepted: Hi World, Hey World, This is a blue World. |
Forward logs only when the log message contains the word Hello, or does not contain the word World, or matches the regex st*s*w*s. |
|
Generated expression:if (($msg contains('Hello') or not($msg contains('World')) or re_match($msg, 'st*s*w*s')))
Accepted: Hello, Hello World, Hello Goodbye, Yellow, Hi, stars wars Not accepted: Hi World, Hey World, This is a blue World. |
Forward logs only when the log message contains the words Hello and World. |
|
Generated expression:if (($msg contains('Hello') and $msg contains('World')))
Accepted: Hello World, Hello, this is World Not accepted: Hi World, Hey World, Hello Word. |
Forward logs only when the log message contains the word Hello and does not contain the word World. |
|
Generated expression:if (($msg contains('Hello') and not($msg contains('World'))))
Accepted: Hello, Hello Goodbye, Hello, man, Hello word Not accepted: Hi World, Hey World, Hello World. |
Forward logs only when the log message contains the word Hello, and does not contain the word World, and matches the regex st*t*k. |
|
Generated expression:if (($msg contains('Hello') and not($msg contains('World')) and re_match($msg, 'st*t*k')))
Accepted: Hello, Let's watch star trek Not accepted: Hi World, Hey World, Hello World, Hello, Star trek. |
Forward logs only when the log message contains the word Hello and does not contain the word World, or contains the words hey and there. |
|
Generated expression:if (($msg contains('Hello') and not($msg contains('World'))) or ($msg contains('hey') and $msg contains('there')))
Two filters are needed here because the or operator is used between filters. Accepted: Hello, hey there, I said hello and they said hey there Not accepted: This is a blue world, Hello World. |
Forward logs only of specific applications or Docker containers. For example, where Program Name is docker.rabbitmq or docker.redis. |
|
Accepted: Any log generated by the rabbitmq or redis container.When matching multiple Program Name values, use the Or condition. |
Forward logs except from specific applications or Docker containers. For example, where Not Program Name is docker.cosign-test or docker.nginx. |
|
Accepted: All logs except those generated by the cosign-test and nginx containers.When matching multiple Not Program Name values, use the And condition. |