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

Chris Colbert sccolbert at gmail.com
Wed Jan 6 11:47:20 EST 2010


i was able to fix the exception by calling logging.shutdown() before the
call to os.remove().

However, I still think there is probably a more elegant solution.

On Wed, Jan 6, 2010 at 12:57 PM, Chris Colbert <sccolbert at gmail.com> wrote:

> 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/ce5c8f86/attachment.html>


More information about the Python-list mailing list