How to log only one level to a FileHandler using python logging module.

F

fuzzylollipop

I want a FileHandler to only log a single level say for example
logging.INFO, and nothing else.
do I need to create a custom Handler for this or is this doable with
some magic that is not documeneted somewhere?
 
Z

zacherates

Try looking into logging.Filters:
http://docs.python.org/lib/node358.html

An example:

import logging

class InfoFilter(logging.Filter):
def filter(self, record):
return record.levelno == 20

if __name__ == "__main__":
logger = logging.getLogger()
hdlr = logging.FileHandler("blah.log")
hdlr.addFilter(InfoFilter())
logger.addHandler(hdlr)

logger.exception("ERROR WILL ROBINSON")

Cheers,
Aaron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top