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"