# Quick tip on controlling log output in tests

November 11, 2013
By

(This article was first published on Cartesian Faith » R, and kindly contributed to R-bloggers)

When running tests for a package, it’s important that the console output is unadulterated since test results are printed with formatting. My code is littered with log output using my futile.logger package [1]. Since arbitrary appenders are supported in futile.logger, a neat trick is to redirect output to a file when running the tests. This is as simple as adding the following lines to the top of a testthat or RUnit script:

library(futile.logger)
flog.appender(appender.file("unit_tests.log"))


Now when R CMD check runs, the console output will be clean and all the log output from your package will be directed to <package>.Rcheck/<package>/tests/unit_tests.log.

Similarly, if you want to modify the log threshold when running tests, throw in a change of threshold.

flog.threshold(DEBUG)


So you get all of this without needing to change any of the code in the package proper.

### Notes

[1] The futile.logger package is available on CRAN, while the latest version is on github. To install from github, run the following from your R shell.

library(devtools)
install.packages('futile.options')
install_github('lambda.r', 'zatonovo')
install_github('futile.logger', 'zatonovo')