Taming the Noise in journalctl with Custom Filters and Priorities

Introduction to journalctl Filtering

I’ve been using journalctl for years, and I’ve learned that it can be overwhelming if you don’t know how to filter the noise. By default, journalctl displays a vast amount of information that may not be relevant to your current needs. In this article, I’ll share how I tame the noise in journalctl using custom filters and priorities.

Understanding journalctl Basics

Before we dive into filtering, let’s cover some journalctl basics. The command journalctl is used to query the systemd journal, which stores log messages from various system components, including systemd services, kernel messages, and other system logs. I usually start with a simple command to view the most recent journal logs:

journalctl -n 100

This displays the 100 most recent log entries, which is a good starting point for troubleshooting.

Filtering by Priority

One way to reduce the noise in journalctl is to filter by priority. The systemd journal uses a priority system, ranging from emerg (emergency) to debug, to categorize log messages. I’ve seen this go wrong when people don’t filter by priority, and they end up with a huge amount of irrelevant logs. You can use the -p option to filter logs by priority. For example:

journalctl -p err

This displays only log messages with a priority of err (error) or higher.

Filtering by Unit

Another way to filter logs is by unit, which refers to a specific systemd service or component. Don’t bother with trying to filter by unit if you’re not sure what you’re looking for - it can be overwhelming. Instead, use the -u option to filter logs by unit. For example:

journalctl -u sshd

This displays only log messages related to the sshd service.

Filtering by Keyword

You can also filter logs by keyword using the --grep option. This is where people usually get burned - they use a keyword that’s too broad, and they end up with a huge amount of irrelevant logs. For example:

journalctl --grep "connection refused"

This displays only log messages containing the phrase “connection refused”.

Combining Filters

To further reduce the noise in journalctl, you can combine multiple filters using the -- separator. The real trick is to use the right combination of filters to get the information you need. For example:

journalctl -p err -u sshd --grep "connection refused"

This displays only log messages with a priority of err or higher, related to the sshd service, and containing the phrase “connection refused”.

Saving Filters to a File

If you find yourself using the same filters repeatedly, you can save them to a file using the --output option. In practice, this is a huge time-saver. For example:

journalctl -p err -u sshd --grep "connection refused" --output=json > sshd_errors.json

This saves the filtered logs to a file named sshd_errors.json in JSON format.

Troubleshooting Tips

When working with journalctl filters, you may encounter issues with log rotation or journal size limits. I’ve seen this happen to people who don’t specify a time range for their filters. To address these issues, you can use the --since and --until options to specify a time range for your filters. For example:

journalctl -p err -u sshd --since=yesterday --until=1hourago

This displays only log messages with a priority of err or higher, related to the sshd service, and occurring between yesterday and one hour ago.

Security Considerations

While filtering logs with journalctl, it’s essential to consider security implications. For example, if you’re filtering logs to troubleshoot a specific issue, you may inadvertently expose sensitive information. To mitigate this risk, you can use the --no-pager option to prevent log output from being piped to a pager, which can help reduce the attack surface. Additionally, you can use tools like systemd-journald to configure journal settings and ensure that sensitive information is not logged.

For more information on journalctl and systemd journal configuration, you can visit the systemd documentation or the Arch Linux wiki.


See also