
System Logging with Rsyslog: A Comprehensive Guide
As a system administrator, logging is an essential aspect of maintaining the health and security of your servers. In this article, we will delve into the world of rsyslog, a powerful and flexible system logging configuration tool. We will explore 13 system logging configurations that you can use to tailor your logging setup to meet the specific needs of your infrastructure.
What is Rsyslog?
Rsyslog is an open-source, widely-used system logging daemon for Unix-like operating systems. It allows you to collect and manage log messages from various sources, providing a centralized location for monitoring and troubleshooting. Rsyslog supports multiple output methods, including files, network sockets, and message queues.
13 System Logging Configurations with Rsyslog
1. Basic Configuration
The most fundamental configuration involves specifying the log level and the file where logs will be written.
“`markdown
/etc/rsyslog.conf
Set log level to info and write to a file called “log.log”
*.info /var/log/log.log
“`
2. Prioritizing Logs
You can prioritize logs by setting different levels for various facilities, such as auth and user messages.
“`markdown
/etc/rsyslog.conf
Log auth messages with level warn and user messages with level debug
auth.warning /var/log/auth.log
user.debug /var/log/user.log
“`
3. Filtering Logs
You can filter logs based on content, such as specific keywords or regular expressions.
“`markdown
/etc/rsyslog.conf
Log messages containing the keyword “error” with level warning
.;msg.regex,”error”.warning /var/log/error.log
“`
4. Rotation and Archiving Logs
You can rotate logs to prevent them from growing too large, and archive old logs for future reference.
“`markdown
/etc/rsyslog.conf
Rotate logs every day and keep 30 days of archives
.info;date=,. /var/log/rotated.log
$ModLoad imfile # Load the input module for log files
“`
5. Forwarding Logs to a Central Server
You can forward logs from remote servers to a central server using TCP or UDP.
“`markdown
/etc/rsyslog.conf (on central server)
Listen on port 514 for incoming log messages
$ModLoad imtcp # Load the input module for TCP
input(type=”imtcp” port=”514″)
Forward received logs to a file
. /var/log/central.log
“`
6. Using a Custom Log Format
You can customize the log format using templates and variables.
“`markdown
/etc/rsyslog.conf
Define a custom template for user messages
$template UserLog,”User %fromhost% %msg%\n”
Use the custom template for user messages
user.debug /var/log/user.log;UserLog
“`
7. Handling System Crashes and Reboots
You can configure rsyslog to handle system crashes and reboots by setting up a separate log file.
“`markdown
/etc/rsyslog.conf
Log system crash messages with level critical and reboot messages with level info
system.critical /var/log/crash.log
system.info /var/log/reboot.log
“`
8. Using Network Output
You can use network output to forward logs to a remote server or log collector.
“`markdown
/etc/rsyslog.conf (on local server)
Forward received logs to a remote server over TCP
. @@remote_server:514
“`
9. Setting Up Log Filtering and Prioritization for Multiple Facilities
You can set up separate log filtering and prioritization rules for multiple facilities, such as auth and user messages.
“`markdown
/etc/rsyslog.conf
Log auth messages with level warn and user messages with level debug
auth.warning /var/log/auth.log
user.debug /var/log/user.log
“`
10. Customizing the Log Format for Multiple Facilities
You can customize the log format for multiple facilities using templates and variables.
“`markdown
/etc/rsyslog.conf
Define a custom template for auth messages
$template AuthLog,”Auth %fromhost% %msg%\n”
Use the custom template for auth messages
auth.warning /var/log/auth.log;AuthLog
Define a custom template for user messages
$template UserLog,”User %fromhost% %msg%\n”
Use the custom template for user messages
user.debug /var/log/user.log;UserLog
“`
11. Handling Log File Overflow
You can configure rsyslog to handle log file overflow by setting up a separate log file and rotation rules.
“`markdown
/etc/rsyslog.conf
Rotate logs every day and keep 30 days of archives
.info;date=,. /var/log/rotated.log
Set up a separate log file for handling log file overflow
*log2 /var/log/overflow.log
“`
12. Using Message Queue Output
You can use message queue output to forward logs to a remote server or log collector.
“`markdown
/etc/rsyslog.conf (on local server)
Forward received logs to a message queue
. $QueueType=”imuxsock” $QueueName=”/var/run/queues”
“`
13. Setting Up Log Rotation and Archiving for Multiple Facilities
You can set up log rotation and archiving rules for multiple facilities, such as auth and user messages.
“`markdown
/etc/rsyslog.conf
Rotate logs every day and keep 30 days of archives
.info;date=,. /var/log/rotated.log
Set up a separate log file for auth messages
auth.warning /var/log/auth.log
Set up a separate log file for user messages
user.debug /var/log/user.log
“`
In conclusion, this article has explored 13 system logging configurations with rsyslog that can be used to tailor your logging setup to meet the specific needs of your infrastructure. By applying these configurations, you can ensure efficient and effective logging practices in your organization.