Fluentd: One Source for Several Filters and Matches
In Fluentd, it's common to use a single source to collect logs and then process them through multiple filters and match patterns. This setup allows you to route and manipulate logs flexibly, applying different filters to the same source data and directing the results to various outputs.
Basic Fluentd Configuration: One Source, Multiple Filters, and Matches
Below is a step-by-step guide on how to set up a Fluentd configuration with one source and several filters and matches.
1. Define the Source
Start by defining a single source that collects logs. For example, you might use the tail input plugin to read logs from a file.
2. Apply Multiple Filters
Filters allow you to modify or enrich log data before it's sent to its destination. You can use multiple filters on the same source by matching on the source's tag.
Example Filters Configuration
3. Define Multiple Matches
Matches define where and how the filtered logs are sent. You can have multiple matches with different tags or patterns to direct logs to various destinations.
Example Matches Configuration
Explanation of the Configuration
- Source (
<source>block): Collects logs from/var/log/app.logand tags them asapp.logs. - Filters (
<filter>blocks):- The first filter parses JSON fields from the message.
- The second filter adds metadata like
hostnameandapp_name. - The third filter uses a grep filter to include only logs with a severity of
WARNorERROR.
- Matches (
<match>blocks):- The first match sends logs to Elasticsearch.
- The second match writes logs to a local file in JSON format.
- The third match sends critical logs to Slack for monitoring and alerts.
Using Tags for Routing
Tags (app.logs in the above example) are critical for routing logs through specific filters and matches. Ensure that your tag structure aligns with the filters and matches you want to apply.
Best Practices
- Organize Filters and Matches: Keep filters and matches organized logically and document them to understand the log flow.
- Use Buffering: Add buffering to matches, especially when sending data to external services like Elasticsearch or Slack, to handle errors and retries effectively.
- Testing and Monitoring: Test each filter and match configuration separately to ensure they work as expected before deploying to production. Monitor Fluentd’s logs to troubleshoot issues quickly.