Futile.logger 1.3.3 RC available

[This article was first published on Cartesian Faith » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This is a preview release of futile.logger 1.3.3 so some people with feature requests can try out the code before I push to CRAN.

A futile try/catch

It is now possible to capture non-futile warnings and errors by wrapping a block in ftry, which is essentially a wrapper around tryCatch. The return value is the value of the expression or the message invoked by a warning or

> ftry(log(-1))
WARN [2013-05-29 15:49:45] simpleWarning in log(-1): NaNs produced

When using ftry, the warning and error handlers are defined to call flog.warn or flog.error, respectively. A finally statement can also be passed to ftry, just like in tryCatch.

> o <- ftry(log(-1), finally=flog.info("In finally block"))
INFO [2013-05-29 15:54:26] In finally block
WARN [2013-05-29 15:54:26] simpleWarning in log(-1): NaNs produced

Custom layouts

The default layout for the futile.logger is designed to be minimally function. For people who want control over their log output, it is now possible to call layout.format to define a format string for a logger. The available variables are:

~l The log level
~t The timestamp
~n The namespace of the calling function
~f The name of the calling function
~m The message string

None of these variables are required, although at a minimum ~m should be used so you get useful strings out. To set a logger’s layout amounts to passing the closure generated by layout.format to flog.layout.

> flog.layout(layout.format('[~l] [~t] [~n.~f] ~m'))
> dummy <- function() { flog.info("Check out my shiny log message") }
> dummy()
[INFO] [2013-05-29 16:26:19] [ROOT.dummy] Check out my shiny log message

For those that are truly particular, the timestamp format can also be set using the datetime.fmt option.

Carping log messages

With a nod to PERL, I’ve also introduced (by request) the ability to return every single log message directed to futile.logger. This is a per logger setting and is returned invisibly so as to not clutter the console.

> flog.carp(TRUE)
NULL
> flog.threshold(WARN)
NULL
> o <- flog.info("This won't print")
> o
[1] "[INFO] [2013-05-29 16:29:11] [futile.logger.flog.info] This won't print\n"

If you are testing this package, please let me know. The download link will be available for about a week, after which I will push to CRAN.


To leave a comment for the author, please follow the link and comment on their blog: Cartesian Faith » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)