Splitting Docker Stdout and Stderr With Fluentd Fluent-plugin-rewrite-tag-filter Plugin
Splitting Docker stdout and stderr logs using Fluentd and the fluent-plugin-rewrite-tag-filter plugin involves routing logs based on their stream type (stdout or stderr). This approach allows you to handle standard output and error output differently, such as sending them to separate destinations for better log management.
Overview of the Setup
- Fluentd Input: Collect logs from Docker using the Fluentd logging driver or directly using the Fluentd forward input.
- Filter with Rewrite Tag: Use the
rewrite-tag-filterplugin to create new tags based on the stream type (stdoutorstderr). - Match: Direct logs to different destinations based on the new tags.
Step-by-Step Configuration
1. Fluentd Configuration (fluent.conf)
Below is a Fluentd configuration that demonstrates how to achieve this setup.
Input Section: Collect Docker Logs
Set up Fluentd to receive logs from Docker containers. You can use the forward input if you're using the Fluentd logging driver in Docker.
<source>
@type forward
port 24224
tag docker.*
</source>
2. Filter Section: Split Logs Using rewrite-tag-filter
Use the rewrite-tag-filter plugin to rewrite the tags based on whether the log entry is from stdout or stderr. This plugin allows you to add conditions that modify the tags dynamically.
<filter docker.**>
@type rewrite_tag_filter
# Match based on the log stream type (stdout or stderr)
<rule>
key stream
pattern stdout
tag stdout.logs
</rule>
<rule>
key stream
pattern stderr
tag stderr.logs
</rule>
</filter>
3. Match Section: Route Logs Based on the Rewritten Tags
Once the tags have been rewritten, you can match these new tags (stdout.logs and stderr.logs) to route the logs to different outputs.
# Match for stdout logs
<match stdout.logs>
@type file
path /var/log/fluentd/stdout_logs.log
<buffer>
@type file
path /var/log/fluentd/buffer/stdout
</buffer>
<format>
@type json
</format>
</match>
# Match for stderr logs
<match stderr.logs>
@type file
path /var/log/fluentd/stderr_logs.log
<buffer>
@type file
path /var/log/fluentd/buffer/stderr
</buffer>
<format>
@type json
</format>
</match>
Explanation of the Configuration
- Source:
- The
<source>section uses theforwardplugin to collect logs from Docker containers tagged withdocker.*.
- The
- Filter:
- The
<filter>section uses therewrite_tag_filterplugin to inspect thestreamfield, which indicates whether the log is fromstdoutorstderr. - It then rewrites the tag based on the stream type:
stdoutlogs are tagged asstdout.logs.stderrlogs are tagged asstderr.logs.
- The
- Match:
- The
<match>sections specify where to send the rewritten logs. - In this example,
stdoutlogs are sent to/var/log/fluentd/stdout_logs.logandstderrlogs to/var/log/fluentd/stderr_logs.log.
- The
Installing fluent-plugin-rewrite-tag-filter
Ensure that the rewrite-tag-filter plugin is installed in your Fluentd environment. You can install it using the following command:
td-agent-gem install fluent-plugin-rewrite-tag-filter
Testing the Configuration
Restart Fluentd to apply the new configuration:
sudo systemctl restart td-agentCheck Logs: Verify that the logs are being split correctly into their respective files by checking
/var/log/fluentd/stdout_logs.logand/var/log/fluentd/stderr_logs.log.
Additional Considerations
- Buffering: Ensure proper buffering is configured to handle high log volumes and prevent data loss in case of service interruptions.
- Performance: Test the setup under load conditions similar to production to ensure Fluentd handles log processing efficiently.
- Security: Set appropriate file permissions on log files and buffers to prevent unauthorized access.