[back to the tutorial] [previous session] [next session]
differently than in the logging library in Python and in Java, handlers in this logging library aren't objects: they are environments stored in one of the loggers. the principal characteristic property of a handler is its action. a action is a function that specifies what the handler should do with a logrecord that, based on all that we have seen above, must be handled. the two commodity functions we have seen in the first two sessions, writeToConsole and writeToFile are action functions.
a look at writeToFile will help understand the idea implemented in this library.
writeToFile <- function (msg, handler)
{
if (!exists('file' , envir=handler))
stop ("handler with writeToFile 'action' must have a 'file' element.\n" )
cat(paste(msg, '\n' , sep='' ), file=with(handler, file), append=TRUE )
}
an action is invoked if a record must be handled. its result code is ignored and all its output goes to the console. it receives exactly two arguments, the formatted message that must be output (the string returned by the formatter of the handler) and the handler owning the action. recall that a handler is an environment: in the action you can inspect the handler environment to perform the desired behaviour.
imagine you want a handler to send its messages to a xmlrpc server or to a password protected ftp server, you would add these properties in the call to addHandler. addHandler would store them in the new handler environment. your action function would retrieve the values from the handler and use them to connect to your hypothetical external server.
the structure of your solution might be something like this:
sendToFtpServer <- function (msg, handler)
{
proxy <- connectToServer(with(handler, server), with(handler, user), with(handler, passwd))
do_the_rest()
}
addHandler(sendToFptServer, user='' , server='' , passwd='' , logger="deep.deeper.deepest" )