Simple logging example doesn't work!

Vinay Sajip vinay_sajip at yahoo.co.uk
Mon Oct 18 19:45:33 EDT 2010


On Oct 18, 10:45 pm, "robinsieb... at gmail.com" <robinsieb... at gmail.com>
wrote:
> If I setlogging.basicConfig() call tologging.INFO, then I see info
> messages in the logfile. I only want to see error messages in the
> logfile.

What you need to do is,

1. Set the root logger's level to INFO. That's because you want at
least INFO messages to go somewhere.
2. Don't use basicConfig(), but instead create a console handler and a
file handler explicitly.
3. Set the file handler's level to ERROR, so only ERROR messages and
higher go to the file.
4. Set the console handler's level to INFO, so INFO messages and
higher go to console.

With this setup, the console will show INFO, WARNING, ERROR, CRITICAL
messages. The log file will show ERROR, CRITICAL messages.

If you need anything more involved (for example, you ONLY want INFO
messages to be shown on the console) you'll need to set up a suitable
Filter and add it to the console handler to filter out messages you
don't want. See the logging docs for more info on Filters.

Regards,

Vinay Sajip



More information about the Python-list mailing list