Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #28050 > unrolled thread

Can I get logging.FileHandler to close the file on each emit?

Started byrikardhulten@gmail.com
First post2012-08-29 03:18 -0700
Last post2012-08-30 09:24 +0200
Articles 4 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Can I get logging.FileHandler to close the file on each emit? rikardhulten@gmail.com - 2012-08-29 03:18 -0700
    Re: Can I get logging.FileHandler to close the file on each emit? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-08-29 13:49 +0100
      Re: Can I get logging.FileHandler to close the file on each emit? rikardhulten@gmail.com - 2012-08-29 05:57 -0700
    Re: Can I get logging.FileHandler to close the file on each emit? Dieter Maurer <dieter@handshake.de> - 2012-08-30 09:24 +0200

#28050 — Can I get logging.FileHandler to close the file on each emit?

Fromrikardhulten@gmail.com
Date2012-08-29 03:18 -0700
SubjectCan I get logging.FileHandler to close the file on each emit?
Message-ID<3570ffff-749a-44c4-ab9e-10c37d9526e8@googlegroups.com>
I use logging.FileHandler (on windows) and I would like to be able to delete the file while the process is running and have it create the file again on next log event.

On windows (not tried linux) this is not possible because the file is locked by the process, can I get it to close the file after each log event?

If not, would the correct thing to do be to write my own LogHandler with this behavior?

/ Rikard

[toc] | [next] | [standalone]


#28061

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2012-08-29 13:49 +0100
Message-ID<mailman.3931.1346244515.4697.python-list@python.org>
In reply to#28050
On 29/08/2012 11:18, rikardhulten@gmail.com wrote:
> I use logging.FileHandler (on windows) and I would like to be able to delete the file while the process is running and have it create the file again on next log event.
>
> On windows (not tried linux) this is not possible because the file is locked by the process, can I get it to close the file after each log event?
>
> If not, would the correct thing to do be to write my own LogHandler with this behavior?
>
> / Rikard
>

I know little about the logging module but given that the FileHandler[1] 
has a close method, can you simply call that, delete the file and reopen 
where needed?  Failing that I'd look at subclassing existing code and 
not writing your own (Your wording implies to me that you're thinking of 
writing something from scratch, my apologies should I be wrong on that).

[1] http://docs.python.org/dev/library/logging.handlers.html

-- 
Cheers.

Mark Lawrence.

[toc] | [prev] | [next] | [standalone]


#28063

Fromrikardhulten@gmail.com
Date2012-08-29 05:57 -0700
Message-ID<mailman.3933.1346245034.4697.python-list@python.org>
In reply to#28061
On Wednesday, August 29, 2012 2:48:57 PM UTC+2, Mark Lawrence wrote:
> On 29/08/2012 11:18,  wrote:
> 
> > I use logging.FileHandler (on windows) and I would like to be able to delete the file while the process is running and have it create the file again on next log event.
> 
> >
> 
> > On windows (not tried linux) this is not possible because the file is locked by the process, can I get it to close the file after each log event?
> 
> >
> 
> > If not, would the correct thing to do be to write my own LogHandler with this behavior?
> 
> >
> 
> > / Rikard
> 
> >
> 
> 
> 
> I know little about the logging module but given that the FileHandler[1] 
> 
> has a close method, can you simply call that, delete the file and reopen 
> 
> where needed?  Failing that I'd look at subclassing existing code and 
> 
> not writing your own (Your wording implies to me that you're thinking of 
> 
> writing something from scratch, my apologies should I be wrong on that).
> 
> 
> 
> [1] http://docs.python.org/dev/library/logging.handlers.html
> 
> 
> 
> -- 
> 
> Cheers.
> 
> 
> 
> Mark Lawrence.

I want to delete the file from outside the process while it is running, so in the code I have no idea when that is...

I meant subclassing logging.Handler, which I went ahead and tried and it seems to work as I like.

Basically in emit I do
1. open log file
2. write msg to it
3. close file

/ Rikard

[toc] | [prev] | [next] | [standalone]


#28093

FromDieter Maurer <dieter@handshake.de>
Date2012-08-30 09:24 +0200
Message-ID<mailman.3958.1346311469.4697.python-list@python.org>
In reply to#28050
rikardhulten@gmail.com writes:

> I use logging.FileHandler (on windows) and I would like to be able to delete the file while the process is running and have it create the file again on next log event.
>
> On windows (not tried linux) this is not possible because the file is locked by the process, can I get it to close the file after each log event?
>
> If not, would the correct thing to do be to write my own LogHandler with this behavior?

Zope is using Python's "logging" module and wants to play well
with log rotating (start a new logfile, do something with the old log file
(compress, rename, remove)).
It does this by registering a signal handler which closes its logfiles
when the corresponding signal is received.

Maybe, you can do something like this. Signal handling under
Windows is limited, but maybe you find a usable signal under Windows
(Zope is using "SIGUSR1").

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web