Introduction to Logging in R using log4r

Reading Time: 3 minutes

One of the most important aspect of an application is Logging. Since logs provide visibility into the behavior of a running app. Hence logs play a vital role in maintenance and enhancement of an application.

However, most of us are already aware with the importance of logging. That’s why we add them in our applications. But one thing that we are not aware of is that, the application should never be concerned with routing or storage of logs, i.e., it should not attempt to write to or manage logs or log files. Instead, each running process, within the application, writes logs to a stdout. In local environment, we can view the logs in the console whereas in staging/production environment, logs can be collated together in .log file(s).

Hence, in this blog post we will learn – how to collect, customize, and standardize R logs using log4r? But first let’s know what log4r is.

What is log4r?

It is a fast, lightweight, object-oriented approach to logging in R based on the widely-emulated Apache Log4j project. It differs from other R logging packages in its focus on performance and simplicity. As it has fewer features (although it is still quite extensible) but it is much faster.

How to use it?

In order to use it, we first have to install it’s package. Like this:

Logging to Console

Then in order to use it, first we have to create a logger object. By default it will add logs to console and suppress the messages of DEBUG level.

# Package for Log4R

# Default Logger
default_logger <- logger()

info(default_logger, "Some other message at the end")

This will provide output like this:

INFO  [2020-04-02 16:01:07] Some other message at the end

In the above log statement we can see that log level, time, and the message are logged.

Enabling DEBUG logs

Moreover, if we want to enable the DEBUG level then we just have to provide the log level in the logger() function. Like this:

# Package for Log4R

# Debug level enabled Logger (by default DEBUG level is suppressed)
debug_logger <- logger("DEBUG")

debug(debug_logger, "This is the 'try' part")

This will provide output like this:

DEBUG [2020-04-02 16:00:59] This is the 'try' part

We can see that DEBUG log statements are now available on the console.

Logging to File

However, in staging/production environments, we generally do not have access to the console, to view and analyze the logs. Hence in that case we need to archive those logs to a file. For that, log4r provides file_appender.

# Package for Log4R

# File Logger
log_file <- "sample.log"
file_logger <- logger("WARN", appenders = file_appender(log_file))

warn(file_logger, paste("Here's the original warning message."))

This will stream all the logs to the file named – sample.log

Hence, using log4r we can collect, customize, and standardize the logs in R applications. A sample snippet can be found here – log4r-sample.R.


Written by 

Himanshu Gupta is a software architect having more than 9 years of experience. He is always keen to learn new technologies. He not only likes programming languages but Data Analytics too. He has sound knowledge of "Machine Learning" and "Pattern Recognition". He believes that best result comes when everyone works as a team. He likes listening to Coding ,music, watch movies, and read science fiction books in his free time.

1 thought on “Introduction to Logging in R using log4r3 min read

Comments are closed.