Discussion:
[Pyogp] Logging
Christian Scholz
2008-08-18 05:53:37 UTC
Permalink
Hi!

As I noted in the mail before we should implement some logging mechanism
soon as it's better than prints ;-)

So what the Python standard lib offers is the logging module:

http://docs.python.org/lib/module-logging.html

Unfortunately the docs don't make it quite clear how to use it.
But here is an example which maybe makes it clearer:

http://antonym.org/node/76

What it actually simply comes down to in the library is that we use a
logger for each module. E.g. in caps.py we could use:

import logging
log = logging.getLogger("pyogp.lib.base.caps")

which instantiates a logger with the name pyogp.lib.base.caps. We don't
specify where we want to log to, just under which "path" we want our
logmessages to be stored.

We then can use it like this:

log.error("connection failed")

which will log this message with the ERROR severity. Other possibilites
are critical, warning, info, debug.

The name of the logger also is a path which means that if you log
something for pyogp.lib.base.caps it will also be recorded in a logger
which only listens to pyogp.lib.base. That way you can control how
specific you want to be.

How things are output depends on the application then. You don't want to
define in the library if things go to syslog, stdout or some file.

The logging module defines various formatters and output handlers to
configure this. You will probably want to use a configuration file like
the one in the example. You probably want to read this from bottom to top.

The last entry looks like this:

[logger_engine]
level: INFO
qualname: pi.basil
handlers: console

This means that you want everything logged with level INFO (or higher,
means more critical) for the logname "pi.basil" (in our case maybe
"pyogp.lib.base" to be logged with handler "console".

console is defined further above and defines a formatter to be used and
the StreamHandler. These are all describes in the logging module definition.

As an example I will add some logging code to the caps module and will
also add some output example to the example.py login script. I hope this
will make things clear.


cheers,

Christian
--
Christian Scholz Homepage: http://comlounge.net
COM.lounge blog: http://mrtopf.de/blog
Luetticher Strasse 10 Skype: HerrTopf
52064 Aachen Video Blog: http://comlounge.tv
Tel: +49 241 400 730 0 E-Mail ***@comlounge.net
Fax: +49 241 979 00 850 IRC: MrTopf, Tao_T

neue Show: TOPFt?glich (http://mrtopf.de/blog/category/topf-taglich/)
Loading...