<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/improved-python-style-logging-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://mazamascience.com/WorkingWithData/?p=1759"> R – Working With Data</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>This entry is part 21 of 21 in the series <a title="Using R" target="_blank" rel="nofollow" href="http://mazamascience.com/WorkingWithData/?series=using-r">Using R</a></div>
<p>Last August, in <a target="_blank" rel="nofollow" href="http://mazamascience.com/WorkingWithData/?p=1727">Python-style Logging in R</a>, we described using an R script as a wrapper around the futile.logger package to generate log files for an operational R data processing script. Today, we highlight an improved, documented version that can be sourced by your R scripts or dropped into your package’s R/ directory to provide easy file and console logging.</p>
<p><span></span></p>
<p>The improved <a target="_blank" rel="nofollow" href="https://gist.github.com/jonathancallahan/3ed51265d3c6d56818458de95567d3ae">pylogging.R</a> script enables the following use case:</p>
<ol>
<li>set up log files for different log levels</li>
<li>set console log level</li>
<li>six available log levels: TRACE, DEBUG, INFO, WARN, ERROR, FATAL</li>
</ol>
<p>All of these capabilities depend upon the excellent futile.logger package (<a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/futile.logger">CRAN</a> or <a target="_blank" rel="nofollow" href="https://github.com/zatonovo/futile.logger">github</a>). This script just wraps this package to get python-style file logging. Please see futile.logger’s documentation for details on output formatting, <em>etc</em>.</p>
<p>The pylogging.R script is fully documented with roxygen2 comments and can be incorporated into packages as long as their DESCRIPTION file adds a dependency on futile.logger.  For those developing operational processing pipelines using R, python style logging can be very useful.</p>
<p>To demonstrate log files and console output you can download <a target="_blank" rel="nofollow" href="https://gist.github.com/jonathancallahan/3ed51265d3c6d56818458de95567d3ae">pylogging.R</a> and the following sleepy.R script:</p>
<pre># sleepy.R

logger.info(&quot;Getting sleepy ...&quot;)
Sys.sleep(1)
logger.warn(&quot;Getting really tired ...&quot;)
Sys.sleep(2)
logger.error(&quot;Having trouble staying awake ...&quot;)
Sys.sleep(3)
logger.fatal(&quot;Not gonna marzlmurrrzzzzz ...&quot;)
stop(&quot;Snap out of it!&quot;)</pre>
<p>The following R session demonstrates the general functionality:</p>
<pre>> list.files()
[1] &quot;pylogger.R&quot; &quot;sleepy.R&quot;
> # Nothing up my sleeve 
>
> source(&quot;pylogger.R&quot;)
> source(&quot;sleepy.R&quot;)
Error: You must initialize with 'logger.setup()' before issuing logger statements.
> # Setup is required
> 
> logger.setup()
> source(&quot;sleepy.R&quot;)
FATAL [2017-03-15 16:34:15] Not gonna marzlmurrrzzzzz ...
Error in eval(expr, envir, enclos) : Snap out of it!!
> # The console log level is set to FATAL by default
>
> list.files()
[1] &quot;pylogger.R&quot; &quot;sleepy.R&quot; 
> # No log files created
> 
> # Now modify console log level
> logger.setLevel(ERROR)
> source(&quot;sleepy.R&quot;)
ERROR [2017-03-15 16:35:29] Having trouble staying awake ...
FATAL [2017-03-15 16:35:32] Not gonna marzlmurrrzzzzz ...
Error in eval(expr, envir, enclos) : Snap out of it!!
> # Got ERROR and higher
>
> logger.setLevel(DEBUG)
> source(&quot;sleepy.R&quot;)
INFO [2017-03-15 16:35:42] Getting sleepy ...
WARN [2017-03-15 16:35:43] Getting really tired ...
ERROR [2017-03-15 16:35:45] Having trouble staying awake ...
FATAL [2017-03-15 16:35:48] Not gonna marzlmurrrzzzzz ...
Error in eval(expr, envir, enclos) : Snap out of it!!
> # Got DEBUG and higher
>
> list.files()
[1] &quot;pylogger.R&quot; &quot;sleepy.R&quot;  
> # Still no log files
>
> # Set up log files for two levels
> logger.setup(debugLog=&quot;debug.log&quot;,errorLog=&quot;error.log&quot;)
> logger.setLevel(FATAL)
> source(&quot;sleepy.R&quot;)
FATAL [2017-03-15 16:36:43] Not gonna marzlmurrrzzzzz ...
Error in eval(expr, envir, enclos) : Snap out of it!!
> # Expected console output
>
> list.files()
[1] &quot;debug.log&quot;  &quot;error.log&quot;  &quot;pylogger.R&quot; &quot;sleepy.R&quot;  
> readr::read_lines(&quot;debug.log&quot;)
[1] &quot;INFO [2017-03-15 16:36:37] Getting sleepy ...&quot;               
[2] &quot;WARN [2017-03-15 16:36:38] Getting really tired ...&quot;         
[3] &quot;ERROR [2017-03-15 16:36:40] Having trouble staying awake ...&quot;
[4] &quot;FATAL [2017-03-15 16:36:43] Not gonna marzlmurrrzzzzz ...&quot;   
> readr::read_lines(&quot;error.log&quot;)
[1] &quot;ERROR [2017-03-15 16:36:40] Having trouble staying awake ...&quot;
[2] &quot;FATAL [2017-03-15 16:36:43] Not gonna marzlmurrrzzzzz ...&quot; 
>
> # Got two log files containing DEBUG-and-higher and ERROR-and-higher</pre>
<p>Best Wishes for Better Logging!</p>
<p> </p>
<p> </p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://mazamascience.com/WorkingWithData/?p=1759"> R – Working With Data</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/WoHkYhRmCz0"></a></div>