Ruby on Rails Single Line Logging
Reduce the size of the log messages for production environments to one line per request
Logging is one of the most important features that every software application must support. Application logs are an essential source of information that helps in understanding how the software application is behaving. For instance, proper application logs for a web application should be able to answer the questions regarding the incoming requests and responses, the application errors or warnings.
In the Rails world, the default logging system is very helpful and provides all the needed information regarding the web requests, errors and even the exception trace of errors. As it is shown in the below image, the Rails default logging format is very clear and easy to read. You can see when the request is started, the response code, the response time and many more.
Rails default logging system and log format will produce multiline log messages for the same request. Form the example above, rails generated six lines to log a single request. The number of lines is dependent on the request and rails configurations. For instance, rails will generate a logline for each of the views used to render the response of the request. In Addition, in the case of errors or exceptions, the exception stack trace will be produced in a multiline format too.
While the default rails log format is readable and easy to understand, it generates a lot of log lines for describing a single request and this behavior will increase the size/amount of the logs generated by the applications. Below are some of the drawbacks for this behavior:
- The size of the generated logs will be very huge and this will increase the costs for storing the logs especially in the case of shipping logs to a third party like logz.io.
- Parsing the logs will be a challenge and requires a very complex configuration since the parsing scripts need to consider the multiline logs.
In this post, we’ll walk through the actions needed to modify the logging system for Rails applications to be able to produce single line logs.
Rails Log level
Rails default log level is
debug, This means that all the log messages will be written to the log files including the debugging log messages and the SQL queries used for the web request. However, this should not be the case for production environments. Changing the log level to “info” in the production environment will help in reducing the amount of the generated logs. To active this goal, the below line need to add to the following configuration file
With the above change, we will be able to configure the Rails application log level using the environment variable
LOG_LEVEL the default log level is still
debug for the production environment 😆. however, it is not hardcoded anymore.
Integrate Lograge with rails
Lograge is ruby gem that provide an easy and intuitive way for rewriting rails multiline logs info a single log lines.
Integrating Lograge with Rails applications is a very easy and straightforward process. Below is the list of changes that need to done to integrate and customize rails log messages with Lograge
→ Add Lograge gem to application dependencies: This task can be done by adding the below line to the rails application
Gemfile then execute
→ Enable configuring Lograge support: Instead of hardcoded the switch to Lograge, we are going to make Lograge support configurable. For doing so, Add the below lines to the application configuration file under the following path
LOGRAGE: This environment variable will be used to switch on and off the Lograge support.
→ Add custom variables: By default Lograge messages do not include the host and the IP of the request. However, it is possible to integrate these variables and other variables in the log messages produced by the Lograge gem. Adding the below function to the Rails
app/controllers/application_aontroller.rb will add four variables (host, remote_ip, IP and x_forwarded_for) to the Lograge log event.
→ Create Lograge initializers: To finalize Lograge integration we need to create an initializer for Lograge and configure it with the needed configuration item. The initializer should be created under the path
config/initializers in the root directory of the Rails application. The below snippet can be used as the default configurations for Lograge.
The initializer file starts by enabling the Lograge library (this depends on the environment variable we introduced above). Then it defines the base controllers integrated with Lograge. And finally, it defines the attributes that need to be included in the log messages.
The default Lograge log format is the key-value format, However, Lograge supports other log formats such as JSON and Logstash. The complete list of supported log formats can be found on the Lograge repo.
After applying the above configurations and starting the rails application, rails logs will look like the below image
Switch rails log format from the default multiline logs to a single logline is a good idea, especially for production environments. This change will help us reduce the amount of the generated logs and to improve and simplify the log parsing scripts.
To join our community Slack team chat 🗣️ read our weekly Faun topics 🗞️, and connect with the community 📣 click here⬇