Why Use Logging Libraries for Ruby?
If by any chance, you consider using theputs() 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.
puts():
- 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
DEBUG,INFO,ERROR,WARN, andFATAL.
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:semantic_demo.rb file and add the following contents to create the logger instance:
semantic_logger module. Next, you use the add_appender() method to specify the destination to send logs, which is the standard output here(STDOUT).
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.
I for INFO, W for WARN, etc)
If you examine the output, you will see TRACE(T) and DEBUG(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:
formatter option to the add_appender() method. Take a look at the following simplified example:

Semantic Logger output with colorized log levels in the terminal.
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:
formatter property that you can pass the add_appender() method as demonstrated in the following example:
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.
formatter property in the add_appender() method to the formatter proc object.
Upon running the file, you will receive output similar to the following:
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:
#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
TheLogger 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:
Logger class. After that, you invoke the Logger’s class new() method that accepts a log device, which can be a file name or IO object like STDOUT, or STDERROR.
The Logger supports all the levels supported in the Semantic Logger module, and it adds one more level UNKNOWN, for logging unknown messages.
When you run the file ruby logger_demo.rb, it will yield the following output:
DEBUG. You can modify this anytime. In the following code block, set the minimum log level to WARN:
WARN or higher are logged. DEBUG, and INFO levels are discarded.
To send the logs in a file, modify the Logger.new() method to take the file name:
app.log file with the following log messages once you open it:
formatter property set to a proc object. Take a look at the following example:
formatter is set to a proc object with the parameters severity, datetime, prog_name, and msg. You use these constants to format the log message string and also include a process ID.
Running the file yields the following output:
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.
#3 Logging
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:
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: DEBUG, INFO, WARN, ERROR, and FATAL.
When you run the file, the output will resemble the following:
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:
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:
stdout() function. You also have the option of setting the minimum level globally like so:
app.log file, then run the file again to see the following:
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:
logging library features, see the documentation.
#4 Ougai
Ougai is a structured logging library that builds upon the standard Logger class module in Ruby. At the time of writing, it has 240 Github stars and boost of having the following features:- 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:
$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:
level property is set to a numeric value. If you are unfamiliar with the numeric values, look at the following:
TRACE: 10DEBUG: 20INFO: 30WARN: 40ERROR: 50FATAL: 60
10(TRACE) has been omitted. By default, the minimum level is set to DEBUG. To see the message, look at the following example:
10 appears in the output.
To redirect the output to a file, pass a filename to the Logger.new() method:
app.log file in the directory that the following log messages:
with_fields property as follows:
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:
#5 Yell
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
Install theyell module:
yell_demo.rb file and enter the following:
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:
new() method in place of the STDOUT:
app.log file, which contains the following:
yell. To do that, you need to pass :format as a second argument to the new method as follows: