[back to the tutorial]   [previous session]   [next session]  

logger objects

in the last example box in the previous section we have sent logging records to the 'libro.romanzo.campanile' logger. we have done this by invoking the global loginfo function, passing it the name of the logger. this is only practical if you are logging to the root logger or if you are using many different loggers, not if you are sending, as in our example, more records to the same logger. if you are talking the whole time to the same logger, you do not want to have to repeat the name of the logger each time you send it a record.

the solution to this is in the object oriented approach taken in this logging library. the getLogger() function returns a Logger object, which, since we are using Reference Classes, is itself an environment. in the previous examples we have only used the fact that Logger objects are environments, let's now have a look at what more they offer. > class(getLogger())
[1] "Logger"
[1] "logging"
> is.environment(getLogger())
[1] TRUE

let me keep it compact, I'm just giving you the code that will produce the same logging as in the previous example. do notice that you can mix invoking object methods with usage of the global functions. R> logReset()
R> getLogger('libro.romanzo')$addHandler(writeToConsole)
R> lrc <- getLogger('libro.romanzo.campanile')
R> lrc$info('Ma cos\'è questo amore?')
2010-04-08 11:18:59 INFO:libro.romanzo.campanile:Ma cos'è questo amore?
R> lrc$info('Se la luna mi porta fortuna')
2010-04-08 11:19:05 INFO:libro.romanzo.campanile:Se la luna mi porta fortuna
R> lrc$info('Giovanotti, non esageriamo!')
2010-04-08 11:19:12 INFO:libro.romanzo.campanile:Giovanotti, non esageriamo!
R> loginfo('memories of a survivor', logger='libro.romanzo.lessing')
2010-04-08 11:22:06 INFO:libro.romanzo.lessing:memories of a survivor
R> getLogger('libro')$warn('talking to a hierarchically upper logger')
R> logerror('talking to an unrelated logger', logger='rivista.cucina')