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

the basics

This logging library attempts to be as compatible as possible (that is: as it was conceivable to the author) to the standard Python logging library. If you are accustomed to that logging library, this one will look familiar to you, otherwise have a look at this excerpt, then decide if you want to come back here and read further or go to the python documentation and read deeper.

In this session we work with one single logger, the root logger, and we use only console handlers.

Start up R, load the library, use the basic configuration.

R> library(logging)
R> basicConfig()
R>

Let's check the effect of the above actions. Both our loggers and handlers are environments, so we can use for example ls and with to inspect them (don't worry if you get more elements than shown here, I'll come back to them later). After this basic configuration, our logger has handlers and a level and it contains one handler. This is enough for some simple logging to the console. The default logging level of the root logger is INFO (20). Anything at severity lower than INFO will not be logged.

R> ls(getLogger())
[1] "handlers" "level"
R> getLogger()[['level']]
INFO
  20
R> getLogger()[['handlers']]
$basic.stdout
<environment: 0x........>

R> loginfo('does it work?')
2010-04-08 11:28:35 INFO::does it work?
R> logwarn('my %s is %d', 'name', 5)
2010-04-08 11:28:48 WARN::my name is 5
R> logdebug('I am a silent child')
R>

We add an other handler to the console, without specifying its name. It gets one automatically from the name of the function. You can add and remove handlers using their names. You can also refer to them by function, if that is the way you defined it.

R> addHandler(writeToConsole)
R> names(getLogger()[['handlers']])
[1] "basic.stdout" "writeToConsole"
R> loginfo('test')
2010-04-07 11:31:06 INFO::test
2010-04-07 11:31:06 INFO::test
R> logwarn('test')
2010-04-07 11:31:15 WARN::test
2010-04-07 11:31:15 WARN::test
R> removeHandler('writeToConsole')
R> logwarn('test')
2010-04-07 11:32:37 WARN::test
R>

Handlers have a level associated to them. Any logging record passing through a handler and having a severity lower than the level of the handler is ignored. You can alter the level of a handler. this is what we do in the following lines: we alter the level of the default console handler 'basic.stdout' to 30 (WARN).

R> addHandler(writeToConsole)
R> setLevel(30, getHandler('basic.stdout'))
R> logwarn('test')
2011-08-03 15:59:13 WARNING::test
2011-08-03 15:59:13 WARNING::test
R> loginfo('test')
2011-08-03 15:59:18 INFO::test
R> getHandler('basic.stdout')[['level']]
WARN
30
R> getHandler('writeToConsole')[['level']]
INFO
20

code