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

Discussion in 'Python' started by rikardhulten@gmail.com, Aug 29, 2012.

  1. Guest

    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
    , Aug 29, 2012
    #1
    1. Advertising

  2. 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.
    Mark Lawrence, Aug 29, 2012
    #2
    1. Advertising

  3. Guest

    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
    , Aug 29, 2012
    #3
  4. Guest

    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
    , Aug 29, 2012
    #4
  5. 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").
    Dieter Maurer, Aug 30, 2012
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rich
    Replies:
    0
    Views:
    700
  2. Jim

    logging / FileHandler

    Jim, Aug 4, 2004, in forum: Python
    Replies:
    1
    Views:
    315
    Vinay Sajip
    Aug 5, 2004
  3. fuzzylollipop
    Replies:
    1
    Views:
    375
  4. Kenneth Love
    Replies:
    1
    Views:
    315
    Vinay Sajip
    Sep 26, 2007
  5. =?UTF-8?B?UmFmYcWC?= Zawadzki

    Write by logging.FileHandler to one file by many processess

    =?UTF-8?B?UmFmYcWC?= Zawadzki, Oct 17, 2007, in forum: Python
    Replies:
    5
    Views:
    845
    Gabriel Genellina
    Oct 23, 2007
Loading...

Share This Page