[Tutor] Setting log directory from a command line argument

Michael O'Leary michael at seomoz.org
Fri Feb 22 02:22:19 CET 2013


I have added logging to a Python program I have been working on by putting
this in the module's __init__.py file:

##########
import logging
logger = logging.getLogger('ranking_factors')
formatter = logging.Formatter('[%(asctime)s] %(levelname)s in
%(module)s:%(funcName)s@%(lineno)s => %(message)s')
handler = logging.FileHandler('ranking_factors.log')
handler.setLevel(logging.DEBUG)
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.INFO)
##########

I would like to provide a way for different users to specify the directory
where the log files are written, and perhaps also the log file names,
through command line arguments. Is there a way that I can do that? I can
access the logger variable in the files of the application with

from . import logger

logger.info("Some message")

Could I import logger in the file where the command line arguments are
processed and change its handler to use a different filename? If I can do
that, will that change logger globally, so that logging statements in other
files of the application also write logging to the new file location? If
this wouldn't work, is there a different way that I could accomplish this
task?
Thanks,
Mike
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130221/3bd863b0/attachment.html>


More information about the Tutor mailing list