[Tutor] python logging module: two handlers writing to the same file - okay?

Hans Fangohr h.fangohr at soton.ac.uk
Sun Dec 16 21:33:02 CET 2007


Dear all,

thanks to everybody who replied to my question.

On 15 Dec 2007, at 16:34, Kent Johnson wrote:

> Ricardo Aráoz wrote:
>> Kent Johnson wrote:
>>> I don't know the answer, but it has nothing to do with the logging
>>> module. The question is, can the same file reliably be opened  
>>> twice for
>>> writing in the same module.
>>
>> Well, the question would actually be if the logging module is smart
>> enough to find out that both your filehandlers are referring to  
>> the same
>> file and open it only once.
>
> A quick glance at the logging module shows that it is not that  
> smart. It
> just opens the file twice; hence my original question.
>

To summarise the situation: the logging module is not clever enough  
to detect that I am opening the same file twice. If I use 'w' as the  
opening mode, then I know (and have an example program for this) that  
the logging 'fails': I suppose this can be tracked to the first part  
of the log file being overriden when the second filehandler attempts  
to log its first message, and thus opens the file (again) in 'w' mode.

Using 'a' (append) or 'a+' (?)  *seems* to solve this problem but we  
are still short of knowing as to whether this is guaranteed to work  
(on all platforms...) or whether it just happened to be okay on Mac  
OS X and (Debian) Linux (on which I have tested this).

I think my example raises another question (in particular in context  
with openening the files in 'w' mode): would it be worth contacting  
the developers of the logging module to suggest that it
- tracks which files it writes to and
- warns if the same file is opened twice (or more ofter) using 'w'?

Alternatively, one could enhance the internal logic of the logging  
module so that it is aware of the same file being used twice (or more  
times) so that it only opens the file once, but I suppose this raises  
some other questions (if the two filehandlers have been given  
different opening modes, say 'a' and 'w', which one should it take  
when opening the file only once?). Therefore, just issueing a warning  
may be the better solution: should be easy to implement, doesn't  
change the interface, and prevents (naive) users (like myself) from  
wasting time trying to track down the problem of the beginning of the  
log file missing.

What is the recommended method to make such a suggestion to the  
python team, or the people who look after the logging module?

Many thanks,

Hans





> Kent
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>

--
Hans Fangohr
School of Engineering Sciences
University of Southampton
Phone: +44 (0) 238059 8345

Email: fangohr at soton.ac.uk
http://www.soton.ac.uk/~fangohr








More information about the Tutor mailing list