Skip to content
bayerj edited this page Sep 13, 2010 · 2 revisions

drecks tries to make logging as simple as possible with as much flexibility as you want. Here is an example.

>>> import drecks
</pre>
Loggers with the same name are unique. Whenever you make another call like this one, it will always return the same object.
>>> logger = drecks.get_logger('mylogger')
</pre>
The next call will log a dictionary with the keys ‘message’ and ‘ip’ to the logger and file it under the tags ‘error’ and ‘network’. The labels and keyword arguments are not defind a priori, you can just make them up as you need them.
>>> logger.log(('error', 'network'), message='There was an error on the network.', ip='192.168.0.1')
</pre>
This call went into the void though, since we have not yet registered any reporters. Reporters are objects that receive log entries and process them. For instance, we can have a ListReporter that just stores the items in a list.
>>> rep = drecks.ListReporter()
>>> logger.register_reporter(rep)

Now we can do the call again and the reporter will receive the log.

>>> logger.log(('error', 'network'), message='There was an error on the network.', ip='192.168.0.1')</pre>
Let’s have a look at the list of stored items.
>>> rep.items
[(('error', 'network'), {'ip': '192.168.0.1', 'message': 'There was an error on the network.'}, datetime.datetime(2009, 6, 16, 19, 30, 33, 654932))]</pre>

As you can see, the current timestamp was added automatically.

Clone this wiki locally