how to change when the logging module creates the log file?

Chris Colbert sccolbert at gmail.com
Wed Jan 6 06:57:58 EST 2010


I have an application the writes to a log file when specific exceptions are
handled. However, if no exceptions are encountered, I don't want to create a
log at all.

The problem I am running into is that the stdlib logging module creates the
log file immediately upon logger instantiation.

Thus:
>>> logger = logging.basicConifg('testlog.txt')

already creates the file 'testlog.txt'.

So, at program close, I am reading the size of the log file, and if it is
empty I remove it with os.remove(). This works fine on Linux, but throws a
permission denied exception on Windows.

There has to be a better way to do this than using a hack like that. Is
there a way to make the logging module hold-off on file creation until the
first log is generated? I could do it by wrapping logger in a class, but
that would remove the beauty of having any module import logging from the
stdlib and being able to write to the log.

Thanks for any pointers!

Chris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100106/0bd95940/attachment.html>


More information about the Python-list mailing list