Feb 9, 2023 • 8 min read
The 5 Best Logging Libraries for Ruby
Logging allows you to record important activities on your application to persisting storages, such as files, emails, or a monitoring tool. This creates an audit trail that documents the state of your application before something goes wrong. The logs can help developers during debugging or even product teams to learn how users are using the application to create better experiences.
Ruby has a built-in logging library, as well as a plethora of third-party logging libraries to choose from.
In this article, we'll explore the five best logging libraries for Ruby:
Why Use Logging Libraries for Ruby?
If by any chance, you consider using the
puts() method for logging in to your application, they are a couple of issues you need aware of:
- The messages can be difficult to read for both humans and machines because of the lack of severity levels, timestamps, or a machine-readable format like JSON.
- Difficult to create functionality to collect all log messages to destinations such as files, emails, sockets, or monitoring tools.
- If the application is a module that can be used by other developers, it can clutter the standard output.
Take a look at the following example using
If you look at the output, you will see that nothing is differentiating these messages. There are no severity levels, timestamps, or process IDs to add more context to the message.
Now, let's compare the output with the one from a logging library:
There are a lot of interesting things with this message:
- It is structured in JSON format, which is machine-readable.
- It has a timestamp that tells the date the log entry was made.
- has a severity level, which indicates the message's significance. The most common levels are
Creating a structured logging message is easy with most logging tools as the functionality is built-in. You can also customize the log messages to include or remove other unwanted fields.
Most logging tools also make it easier to send log messages to multiple destinations, such as files, standard output, sockets, monitoring tools, etc.
Now that you know why a logging library is essential, we will go over the five best logging libraries in Ruby.
5 Best Ruby Logging Libraries
#1 Semantic logger
Semantic Logger is a popular feature-rich logging library for Ruby. It claims it can log thousands of lines without affecting the application performance by pushing the log events in an in-memory queue that resides in a separate thread. The thread is dedicated to sending logs to multiple persisting storages.
The following is a summary of the Semantic Logger features:
- Supports structured logging with JSON.
- Sending logs to multiple destinations, such as the standard output, file, MongoDB, HTTP, and other logging libraries.
- formatting log messages.
- faster performance.
- pretty printing with color.
How to Use Semantic logger
Install the package with the following command:
Next, create a
semantic_demo.rb file and add the following contents to create the logger instance:
In the first line, you import the
semantic_logger module. Next, you use the
add_appender() method to specify the destination to send logs, which is the standard output here(
Following that, you create a logger instance with a name of your choosing. Finally, you call all the methods that correspond to the levels the module supports.
The module supports the following levels ordered with increasing severity:
- TRACE: low-level information that details the program's logic flow.
- DEBUG: Information developers may need during debugging.
- INFO: Confirmation that the application is working well.
- WARN: An issue that may interrupt the application in the future.
- ERROR: information that needs your immediate attention but won't cause the program to exit.
- FATAL: Something serious that can terminate the program.
Once you are done making the changes, save and run the file:
You will receive output that looks close to this:
In the output, the message is logged along with the timestamp, level, process ID, filename, and the logger name you passed when you created an instance of the logger. Note that the level is denoted with a single letter(
If you examine the output, you will see
D) are missing. This is because Semantic Logger is set to
INFO as the minimum level. So all the levels less severe than
INFO are ignored. To override the default behavior, use the
default_level property as follows:
The output now shows all the levels. If you want to colorize the output to easily discern the messages, you can add a
formatter option to the
add_appender() method. Take a look at the following simplified example:
Semantic Logger is configurable, and if you want to save the logs to a file, you use the
file_name property. Consider the following simplified example:
add_appender() method of the
SemanticLogger now accepts a
file_name property, which is set to the file name you want your logs to be written.
After running the file, the
app.log file will be created. When you open the file, you will find content that looks similar to the following:
You can also customize the log messages using a
formatter property that you can pass the
add_appender() method as demonstrated in the following example:
You create a
formatter proc object that formats each log message with the attributes of the Log Event object:
level: the severity of the message.
time: the time that the log entry was created.
message: the message that needs to be logged.
Following this, you set the
formatter property in the
add_appender() method to the
formatter proc object.
Upon running the file, you will receive output similar to the following:
Until now, the log records have not been structured, which can be difficult to parse or filter. Let's make them structured using the JSON format. To do that, let's look at a new example that formats log records using the JSON format and sends them to the standard output:
add_appender() method, you set the
formatter property to
:json to use the JSON format. When you run the file, the output matches the following:
The log messages are now structured and machines can easily parse them.
Now that you have an idea of how to use the Semantic Logger, you can continue learning about it in the documentation.
Get the visibility you need
#2 Logger Class
The Ruby standard library ships with a sophisticated Logging library that contains a lot of useful features that can make logging easier. It has good documentation and a simple API in comparison to loggers in other programming languages.
The following are some of the features available in the Logger class:
- built-in support for rolling log files automatically.
- Can send logs to multiple destinations
- supports structured logging
- formatting log messages
How to Use the Logger Class
Logger class comes with the Ruby standard library, so there is no need to install anything.
To use it, create a
logger_demo.rb file with the following code:
In the first line, you import the
Logger class. After that, you invoke the
new() method that accepts a log device, which can be a file name or IO object like
Logger supports all the levels supported in the Semantic Logger module, and it adds one more level
UNKWOWN, for logging unknown messages.
When you run the file
ruby logger_demo.rb, it will yield the following output:
By default, the minimum log level is set to
DEBUG. You can modify this anytime. In the following code block, set the minimum log level to
Now, only levels with the severity of
WARN or higher are logged.
INFO levels are discarded.
To send the logs in a file, modify the
Logger.new() method to take the file name:
In the directory, you will find the
app.log file with the following log messages once you open it:
If the log message format doesn't work for you, you can customize it using a
formatter property set to a proc object. Take a look at the following example:
formatter is set to a proc object with the parameters
msg. You use these constants to format the log message string and also include a process ID.
Running the file yields the following output:
Let's make the logs structured to make them machine-parseable. Modify the example as follows:
You import the
json module first. Next, you modify the
formatter proc to a proc to return JSON.
With that, you can use the Logger class for structured logging. For more details about the Logger class, refer to the documentation.
Third on our list is the logging module, also capable of creating parseable logs. The design of the module was heavily inspired by Java's log4j library. As of this writing, it currently has over 500 stars on GitHub.
The following are some of the interesting features it offers:
- Structured logging
- Formatting log messages
- Sending logs to multiple destinations
- Creating custom logs
How to Use Logging
To use it, install the module as follows:
logging_demo.rb file with the following code:
First, you import the
logging module. Second, you create the logger instance. Following this, you invoke
add_appenders() with the destination you want to send the logs to, which is the standard output here. Finally, you invoke all the methods corresponding to the supported levels.
The module supports all the five logging levels that the Semantic Logger module supports:
When you run the file, the output will resemble the following:
By default, the minimum level is set to
DEBUG. You can specify a different level in the
add_appenders() method as follows:
appenders.stdout() function, you set the minimum level to
WARN. To verify this, run the file to see output that looks similar to this:
You can configure the
logging module to send logs to multiple destinations, such as the standard output, as well as a file. The previous example already sends logs to standard output. So let's add another appender to send logs to a file:
add_appenders() method now takes a
file() method call that accepts the file name as an argument.
Running the file will create an
app.log file in the project directory. When you open the file, you will see log messages like this:
When you look at the output, you will see that all the log calls have been logged. You can change the minimum level the same way you did in the
stdout() function. You also have the option of setting the minimum level globally like so:
To test the changes, delete the
app.log file, then run the file again to see the following:
If you want to use structured logging with the JSON format, take a look at the following example:
add_appenders() method, you pass the
file() method to send logs to a
logs.json file. You also pass
:layout, which is set to
layouts.json to format the logs using JSON.
Upon running the file, the
layouts.json will be created containing log messages in JSON format:
To continue exploring the
logging library features, see the documentation.
- Structuring logs out of the box.
- forwarding logs to multiple destinations.
- can produce human-readable-logs in the console with the Amazing Print module.
How to Use Ougai
Install the Ougai module:
ougai_demo.rb and add the code below:
In the preceding code, you create a logger instance with
$stdout to send the logs to the standard output. You then call all the log methods that corresponds to the levels. It supports the same levels as the Semantic Logger module, which were defined in the section covering Semantic Logger.
When you run the file, the output will match the following:
In the output, all the log messages are in JSON format. The
level property is set to a numeric value. If you are unfamiliarr with the numeric values, look at the following:
If you closely look at the beginning of the output, you will see that a log message with level that has a numeric value
TRACE) has been omitted. By default, the minimum level is set to
DEBUG. To see the message, look at the following example:
Now, the log message with the level set to the numeric value
10 appears in the output.
To redirect the output to a file, pass a filename to the
Running the code creates an
app.log file in the directory that the following log messages:
If you want to add a custom field to all logging methods, you can use the
with_fields property as follows:
Here, you set the
with_fields to add the
version field to all the log messages.
After you run the file, you will see that the
version field has been added at the end of each log message:
There is more to unpack about the Ougai, explore more features in the documentation.
Last on our list is Yell, which is a comprehensive logging library developed as a drop-in replacement of the built-in logging module. At the time of writing, it has 300 stars on GitHub.
The following are some of the features to consider:
- Writing logs to multiple destinations
- Formatting log messages
How to Use Yell
Next, create a
yell_demo.rb file and enter the following:
You create a logger instance with an adapter
STDOUT as an argument to forward log messages to the standard output. The module also supports all the levels that the standard
Logger class supports.
When you run the file, you will get output that looks like the following:
If you want to add a minimum level, consider the following example:
To send all the logs to a file, you need to pass a filename to the
new() method in place of the
Running the file creates the
app.log file, which contains the following:
You can also customize the log messages using
yell. To do that, you need to pass
:format as a second argument to the
new method as follows:
In this article, we analyzed the five best logging libraries available for Ruby. We hope this guide has helped you choose the logging library for your next project. If you are still unsure about the library you should use, we recommend Semantic Logger.