Modern computing systems generate diverse log messages, encompassing vital information from system logs (including kernel and boot messages), applications, databases, and network services or daemons. These logs play a crucial role in troubleshooting and diagnosing issues when they arise, and are often effective when they have been centralized.
To centralize logs, you can use a log shipper, a tool designed to collect logs from various sources and forward them to diverse locations. Rsyslog is a prominent log shipper operating based on the syslog protocol.
Rsyslog ships with advanced features, such as filtering, and supports both TCP and UDP protocols for transporting messages. It can handle logs related to mail, authorizations, kernel messages, and more.
This comprehensive tutorial will guide you through using Rsyslog to collect, process, and forward log data to a central location. First, you will configure Rsyslog to read logs from a file. Next, you will explore how to process logs using Rsyslog. Finally, you will centralize the logs to Better Stack.
Before you begin, ensure you have access to a system with a non-root user account that has sudo privileges.
Once you've confirmed these prerequisites, create a directory to store your configuration files and applications:
Next, navigate into the newly created directory:
With the directory set up, you're ready to install Rsyslog.
Rsyslog is pre-installed on many systems and may sometimes need to be updated. It's considered best practice to install the latest version to ensure you have access to the most recent features and security enhancements.
Below are the installation instructions, tested on Ubuntu 22.04. For other systems, consult the Rsyslog documentation for installation guidelines.
First, install the latest version of Rsyslog:
If you see the message "rsyslog is already the newest version," it indicates that you have the latest version installed.
Confirm the installation and check the Rsyslog version with the following:
You should see an output similar to this:
Additionally, ensure that the Rsyslog service is active and running:
You should see the status as "active (running)," confirming that Rsyslog is operational:
With Rsyslog successfully installed and running, let's understand how it works.
Before delving into how Rsyslog collects application logs, it's essential to understand how it works with system logs.
In your system, various applications like SSHD, mail clients/servers, and cron tasks generate logs at frequent intervals. These applications write log messages to the /dev/log file as if it were a regular file (pseudo device).
The Rsyslog daemon monitors this file, collecting logs as they are written, and redirects them to individual plain text files in the /var/log directory, including the /var/log/syslog file. Rsyslog can route logs to their appropriate files by inspecting header information, such as priority and message origin, which it uses for filtering.
The routing of these messages is based on rules defined in the 50-default.conf file, located in the /etc/rsyslog.d/ directory, which we'll explore shortly. Rsyslog operates with default configurations, whether freshly installed or already existing.
However, data originates from diverse sources, and these sources might lack rules in the default configurations.
Building on this knowledge, Rsyslog can be extended to collect logs from additional inputs and redirect them to various destinations, including remote ones, as illustrated in the diagram below:
To understand this process, imagine Rsyslog as a pipeline. On one end, Rsyslog collects inputs, transforms them, and forwards them to the other end—the destination.
This can be achieved with a custom configuration file in the /etc/rsyslog.d/ directory, structured as follows:
The main components include:
input: collects logs from various sources.template: modifies the log message format.action: delivers logs to different destinations.Rsyslog uses modules extensively to accomplish its tasks.
Rsyslog features modules designed to collect logs from various sources, identifiable by names starting with the im prefix. Here are a few examples of these input modules:
imhttp: collects plaintext messages via HTTP.
imjournal: fetches system journal messages into Syslog.
imfile: reads text files and converts their contents into Syslog messages.
imdocker: collects logs from Docker containers using the Docker REST API.
For modifying log messages, Rsyslog provides message modification modules typically prefixed with mm:
mmjsonparse: parses structured log messages conforming to the CEE/lumberjack spec.
mmfields: extracts specific fields from log entries.
mmkubernetes: adds Kubernetes metadata to each log event.
mmanon: anonymizes IP addresses for privacy.
Rsyslog offers a wide array of output modules, recognizable by names starting with the om prefix. These modules allow forwarding log messages to various destinations:
omfile: writes log entries to a file on the local system.
ommysql: sends log entries to a MySQL database.
omrabbitmq: forwards log data to RabbitMQ, a popular message broker.
omelasticsearch: delivers log output to Elasticsearch, a robust search and analytics engine.
Now that you have an idea of the available Rsyslog modules and what they do, let's analyze the Rsyslog configuration file in greater detail.
When Rsyslog starts running on your system, it operates with a default configuration file. It collects logs from various processes and directs them to plain text files in the /var/log directories.
Rsyslog relies on rules predefined in the default configuration file. You can also define your own rules, global directives, or modules.
To comprehend how rules work, open the 50-default.conf configuration file in your preferred text editor. This tutorial uses nano, a command-line text editor:
In the initial part of the file, you'll find contents similar to this (edited for brevity):
The lines in the file are rules. A rule comprises a filter for selecting log messages and an action specifying the path to send the logs. Lines starting with # are comments and won't be executed.
Consider this line:
This line can be divided into a selector filtering syslog messages kern.* and an action specifying the path to forward the logs -/var/log/kern.log.
Let's examine the selector kern.* in detail. kern.* is a Facility/Priority-based filter, a commonly used method for filtering syslog messages.
kern.* can be interpreted as follows:
FACILITY: a subsystem generating log messages. kern is an example of a facility alongside other subsystems like authpriv, cron, user, daemon, mail, auth, syslog, lpr, news, uucp, etc. To define all facilities, you can use *.
PRIORITY: specifies the log message priority. Priorities include debug, info, notice, warning, warn (same as warning), err, error (same as err), crit, alert, emerg, panic. If you want to send logs with any priority level, you can use *. Optionally, you can use the priority keyword none for facilities without specified priorities.
Filter and action are separated by one or more spaces or tabs.
The last part, -/var/log/kern.log, is the action indicating the target file where content is sent.
In this configuration file, most rules direct output to various files, which you can find in /var/log.
Close the configuration file and use the following command to list all contents in the /var/log directory:
The output will include files like:
Most files Rsyslog creates belong to the syslog user and the adm group. Other applications besides Rsyslog also create logs in this directory, such as MySQL and Nginx.
This behavior of creating files with these attributes is defined in another default configuration file, /etc/rsyslog.conf.
When Rsyslog runs, it reads the /etc/rsyslog.conf file, another default configuration already defined. This file contains global directives, modules, and references to all the configuration files in the /etc/rsyslog.d/ directory, including the /etc/rsyslog.d/50-default.conf we examined in the previous section.
Open the /etc/rsyslog.conf configuration file using the following command:
Locate the following section near the bottom of the file:
In this file, there are properties such as $FileOwner and $FileGroup that specify the file owner and group, along with file permissions. If you need to change ownership, this is the section to look at. Any keyword prefixed with $ is a variable you can modify.
Further down the configuration file, you'll find lines like:
The $WorkDirectory specifies the location Rsyslog uses to store state files, and $IncludeConfig includes all the configuration files defined in the /etc/rsyslog.d directory. Rsyslog will read any configuration file you create in this directory. This is where you will define your custom configurations.
Now that you understand that Rsyslog has default configurations that route most system logs to various files in /var/log, you are ready to create a demo application that generates logs. Later, you'll configure Rsyslog to read these logs.
In this section, you'll create a logging application built with the Bash scripting language. The application will generate JSON logs at regular intervals, simulating a high-traffic real-world application.
To begin, ensure you are in the processing-stack/logify directory and create a subdirectory for the demo logging application:
Navigate into the directory:
Next, create a logify.sh file:
In your logify.sh file, add the following code to produce logs:
The create_log_entry() function generates structured logs in JSON format with details, such as severity level, message, and HTTP status code. It then enters an infinite loop that repeatedly calls the create_log_entry() function to write logs to a specified file in the /var/log/logify directory.
When you finish writing the code, save and exit the file. Then make the file executable:
Next, create the /var/log/logify directory to store the application logs:
Assign the currently logged-in user in the $USER variable as the owner of the /var/log/logify directory:
Run the logify.sh script in the background:
The & sign tells the OS to run the script in the background, allowing you to continue using the terminal for other tasks while the program runs.
When you press enter, the script will start running and you'll see something like:
Here, 652089 is the process ID, which can be used to terminate the script if needed.
Now, view the app.log contents with the tail command:
The output will show structured JSON logs similar to this:
With the application generating structured JSON logs, you are now ready to use Rsyslog to read these log entries.
Now that you have developed an application to produce logs at regular intervals, you will use Rsyslog to read the logs from a file and transform them into syslog messages stored under the /var/log/syslog file.
To begin, create a configuration file with a name of your choosing in the /etc/rsyslog.d directory:
In the 52-rsyslog-logify.conf file, add the following configuration:
In the first line, the global() directive configures the working directory to store state files. The files allow Rsyslog to track the parts of the logs it has processed.
Next, the module() method is used to load the imfile module, which is used to read logs from files.
Following that, you define an input using the imfile module to read logs from the specified path under the File parameter. You then add a tag, FileLogs,to each log entry processed, and the PersistStateInterval parameter specifies how often the state file should be written when reading the logs.
Finally, a conditional expression checks if the log tag equals the FileLogs tag. If true, an action using the omfile module is defined to forward the logs to the /var/log/syslog file.
After you are finished, save and exit the configuration file.
Before restarting Rsyslog, its a good idea to check the configuration file for syntax errors. Enter the following command to check if the configuration file has no syntax errors:
When the configuration file has no errors, you will see output similar to this:
Now restart Rsyslog:
When Rsyslog restarts, it will start sending the logs to /var/log/syslog. To check the logs in real-time as they get written, enter the following command:
The log entries will be displayed, showing the timestamp, hostname, log tag, and the log message:
Since the /var/log/syslog file contains logs from other processes, it's common to see logs from sources such as kernel.
Now that Rsyslog can read application logs, you can further process the log messages as needed.
When Rsyslog reads log entries, you can transform them before sending them to the output. You can enrich them with new fields or format them differently. One common transformation is formatting logs as JSON using Rsyslog templates.
Rsyslog allows you to format logs into various formats using templates. By default, Rsyslog automatically formats log messages, even if no templates are specified, using its built-in templates. However, you might want to format your logs in JSON, which is structured and machine parsable.
If you look at the logs Rsyslog is currently formatting, you will notice that the logs are not structured:
Many remote destinations prefer structured logs, so it's a good practice to structure the log messages.
In Rsyslog, you can use templates with the template() object to modify and structure logs. Open the configuration file:
Add the template to the configuration file:
In the configuration above, you define a json-template template using the template() object. This template formats the syslog message as JSON. The template includes various property statements to add fields to the syslog message. Each property statement specifies the name of the property to access and the outname, which defines the output field name in the JSON object. The format parameter is set to "jsonf" to format the property as JSON. Some properties include a timestamp, host, syslog-tag, and the syslog message itself.
Finally, you add the template parameter in the action section, referencing the newly defined json-template.
After saving your file, restart Rsyslog:
Now, check the logs being written:
The output shows that the syslog messages are now formatted as JSON. They also include additional fields that provide more context:
The logs in the output are now structured in JSON format and contain more detailed information. Next, you will add custom fields to the log event.
In Rsyslog, you can add custom fields to log entries using constant statements. These statements allow you to insert fixed values into log messages.
First, open the configuration file:
Add a new constant statement to include a custom field called environment with the value dev:
In the configuration above, a constant statement has been added with the outname set to environment and the value set to dev. This constant statement inserts a fixed field named environment with the value dev into each log entry.
Save and exit the configuration file. Then, restart Rsyslog to apply the changes:
To verify if the custom field has been added, tail the syslog file:
You will observe that Rsyslog has included an environment field in each log entry at the end of the log event:
Now that you can add custom fields to log events, you are ready to forward logs to Better Stack.
Better Stack provides an automated setup script that configures Rsyslog to forward logs. Run the following command, replacing $SOURCE_TOKEN with your actual source token from Better Stack:
This script will automatically:
- Detect your system configuration
- Create the necessary Rsyslog configuration for Better Stack as 70-logtail.conf
- Set up secure TLS connections to Better Stack's servers
First, install the required TLS package for secure log forwarding:
Next, create a free Better Stack account. Once registered, proceed to the Sources section in your dashboard and click the Connect source button:
Provide a name for your source, such as "Logify logs," and select "Rsyslog" as the platform:
After creating the source, copy the Source Token and Ingesting Host provided by Better Stack:
Run the following command, replacing $SOURCE_TOKEN with your actual source token from Better Stack:
This script will automatically: - Detect your system configuration - Create the necessary Rsyslog configuration for Better Stack - Set up secure TLS connections to Better Stack's servers
However, since you want to send only your logify application logs (not all system logs), you need to modify the generated configuration to specifically target your application logs.
After running the Better Stack setup script, you need to customize the 70-logtail.conf file to read logs from your logify application.
Open the Better Stack configuration file for editing:
The file will contain the Better Stack forwarding configuration. You need to add the file reading configuration at the beginning of this file. Add the following lines at the very top of the file, before any existing content:
Next, you need to modify the action section to only send your logify logs to Better Stack. Find the action section in the file (it will contain type="omfwd") and wrap it with a conditional statement.
The generated configuration will look like this:
Important: You must wrap this entire action block with a conditional statement to filter only your logify logs. Modify it to:
Without this conditional statement, Rsyslog will send ALL system logs to Better Stack, not just your logify application logs.
Since you're now reading logs directly in the Better Stack configuration, remove your previous local configuration to avoid conflicts:
Before restarting Rsyslog, validate your configuration file for syntax errors:
When the configuration file has no errors, you will see output similar to this:
Now restart Rsyslog to apply the new configuration:
To verify that your logs are being sent to Better Stack, first ensure your logify script is still running:
If the script is not running, restart it:
To monitor the log forwarding process, check the Rsyslog service status:
You can also monitor Rsyslog's activity in real-time:
After a few moments, navigate to your Better Stack dashboard and go to "Live tail." You should see your logify application logs appearing in real-time:
Click on any log entry to view its detailed information:
In this comprehensive guide, you explored the functionality and flexibility of Rsyslog for effective log management. You began by learning how Rsyslog works, then progressed to using it to read logs from various programs, transform log data into JSON format, and add custom fields.
Finally, you configured Rsyslog to forward logs to Better Stack.
With this foundation, you're now well-prepared to integrate Rsyslog into your own projects. To deepen your understanding, check out the official Rsyslog documentation.
While Rsyslog is a powerful log shipper, there are several other tools available. To compare alternatives and choose the right solution for your needs, explore our log shippers guide.
Thank you and happy logging!
We use cookies to authenticate users, improve the product user experience, and for personalized ads. Learn more.