logging into one file problem

Vinay Sajip vinay_sajip at yahoo.co.uk
Tue Sep 13 20:03:40 EDT 2005


Maksim Kasimov wrote:
[Example snipped]

Will the following do what you want?

Don't add handlers in each module. Just add a handler to the root
logger in the main script. Thus:

module1.py:
import logging
logger = logging.getLogger('module1')
#now use the logger in your code

module2.py:
import logging
logger = logging.getLogger('module2')
#now use the logger in your code

script.py:
import logging
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(levelname)s %(message)s',
                    filename='/tmp/script.log',
                    filemode='w') # this only works with 2.4+ - for
earlier versions, need to do as in your original post

Then, the output from loggers 'module1' and 'module2' will end up in
'/tmp/script.log' automatically.

Regards,

Vinay




More information about the Python-list mailing list