Step 2: Filter by Severity
Log retention requirements differ by system. This example selects ERROR and WARN records and drops INFO and DEBUG records.
This step teaches you how to use a conditional mapping to filter a stream of messages.
The Goal
You will add a processor to your pipeline that inspects the level of each log message and deletes any message that is not an ERROR or WARN.
The deleted() Function
The key to filtering is the deleted() function. When a mapping processor returns deleted(), the entire message is discarded and does not proceed to the next stage of the pipeline.
Implementation
-
Start with the Previous Pipeline: Copy the
robust-parser.yamlfrom Step 1 to a new file namedseverity-filter.yaml.cp robust-parser.yaml severity-filter.yaml -
Add the Filtering Logic: Open
severity-filter.yamland add a newmappingprocessor to the end of thepipelinesection.Add this to the 'processors' array in severity-filter.yaml- mapping: |
# Normalize the level to uppercase for consistent matching
let level = this.level.string().uppercase()
# Check if the level is one we want to keep.
# If not, the message is deleted.
root = if level == "ERROR" || level == "WARN" {
this
} else {
deleted()
} -
Inspect the intended result: The authored output retains
ERRORandWARNmessages. The configured filter drops the other fixture levels.
Verification
This page does not record an executed result or measured volume effect. Verify the predicate against representative source levels before adapting it.