Need help in configuration for TimedRotatingFileHandler

Discussion in 'Python' started by Lokesh, Aug 9, 2009.

  1. Lokesh

    Lokesh Guest

    Hi,

    Need help in configure the TimedRotatingFileHandler from configuration
    file

    I have tried with the below code and ended up with the error, code is
    pasted below
    Error - IOError: [Errno 2] No such file or directory: 'G:\\lok_sib\
    \logs\rotate_test'
    [loggers]
    keys=root,simpleExample

    [handlers]
    keys=consoleHandler,timedRotatingFileHandler

    [formatters]
    keys=simpleFormatter

    [logger_root]
    level=DEBUG
    handlers=consoleHandler

    [logger_simpleExample]
    level=DEBUG
    handlers=timedRotatingFileHandler
    qualname=simpleExample
    propagate=0

    [handler_consoleHandler]
    class=StreamHandler
    level=DEBUG
    formatter=simpleFormatter
    args=(sys.stdout,)

    [handler_timedRotatingFileHandler]
    class=handlers.TimedRotatingFileHandler
    level=DEBUG
    formatter=simpleFormatter
    args=("G:\lok_sib\logs\rotate_test", 'midnight', 1)

    [formatter_simpleFormatter]
    format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
    datefmt='%Y-%m-%d %H:%M:%S'


    Thanks or your time
    Regards,
    Lokesh
     
    Lokesh, Aug 9, 2009
    #1
    1. Advertising

  2. On Sun, Aug 9, 2009 at 5:13 PM, Lokesh<> wrote:
    > Hi,
    >
    > Need help in configure the TimedRotatingFileHandler from configuration
    > file
    >
    > I have tried with the below code and ended up with the error, code is
    > pasted below
    > Error - IOError: [Errno 2] No such file or directory: 'G:\\lok_sib\
    > \logs\rotate_test'


    Does the directory G:\\lok_sib\\logs exist?

    > <snipped config file>


    --
    kushal
     
    Kushal Kumaran, Aug 9, 2009
    #2
    1. Advertising

  3. 09-08-2009 Lokesh <> wrote:

    > I have tried with the below code and ended up with the error, code is
    > pasted below
    > Error - IOError: [Errno 2] No such file or directory: 'G:\\lok_sib\
    > \logs\rotate_test'


    Note that: '\r' is listed (interpreted by Python as 'carriage return'
    special character) and not '\\r' (which would mean '\' char + 'r' char).

    Regards,
    *j

    --
    Jan Kaliszewski (zuo) <>
     
    Jan Kaliszewski, Aug 9, 2009
    #3
  4. Lokesh

    Dave Angel Guest

    Lokesh wrote:
    > Hi,
    >
    > Need help in configure the TimedRotatingFileHandler from configuration
    > file
    >
    > I have tried with the below code and ended up with the error, code is
    > pasted below
    > Error - IOError: [Errno 2] No such file or directory: 'G:\\lok_sib\
    > \logs\rotate_test'
    > [loggers]
    > keys=root,simpleExample
    >
    > [handlers]
    > keys=consoleHandler,timedRotatingFileHandler
    >
    > [formatters]
    > keys=simpleFormatter
    >
    > [logger_root]
    > level=DEBUG
    > handlers=consoleHandler
    >
    > [logger_simpleExample]
    > level=DEBUG
    > handlers=timedRotatingFileHandler
    > qualname=simpleExample
    > propagate=0
    >
    > [handler_consoleHandler]
    > class=StreamHandler
    > level=DEBUG
    > formatter=simpleFormatter
    > args=(sys.stdout,)
    >
    > [handler_timedRotatingFileHandler]
    > class=handlers.TimedRotatingFileHandler
    > level=DEBUG
    > formatter=simpleFormatter
    > args=("G:\lok_sib\logs\rotate_test", 'midnight', 1)
    >
    > [formatter_simpleFormatter]
    > format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
    > datefmt='%Y-%m-%d %H:%M:%S'
    >
    >
    > Thanks or your time
    > Regards,
    > Lokesh
    >
    >

    I don't see code there, I see lots of config data, presumably in an .ini
    file. So I don't know how you're reading it in, and converting it to
    Python variables, but I know where I'd look, based on your error message.

    The following line:

    args=("G:\lok_sib\logs\rotate_test", 'midnight', 1)

    seems to contain a Python string. But there are unescaped backslashes
    within it. You can get away with it in two cases, because \l isn't a
    valid escape sequence. But in the case of \r, it looks like a newline
    character.

    Anyway, all three of those backslashes probably need to be doubled.

    args=("G:\\lok_sib\\logs\\rotate_test", 'midnight', 1)

    Two other cures that may work, depending on context: change the backslashes to forward slashes, or use a raw string.

    But as I said earlier, you don't show in any way what code is interpreting this line, so it's all just guesswork.

    DaveA
     
    Dave Angel, Aug 9, 2009
    #4
  5. Lokesh

    Dave Angel Guest

    Lokesh Maremalla wrote:
    > <snip>
    > Code:
    > mlogger = logging.getLogger("simpleExample")
    > def a_view(request):
    > mlogger.debug("a_view called")
    > if request.method== "POST" :
    > mlogger.debug("post function")
    > else:
    > mlogger.debug("serve function")
    >
    > Execution:
    > step1: Executed the code and got the log file with the information
    > step2: Modified the system time from current day to next day
    > step3: End up with the error and the error is pasted below
    >
    > Traceback (most recent call last):
    > File "c:\Python25\lib\logging\handlers.py", line 74, in emit
    > self.doRollover()
    > File "c:\Python25\lib\logging\handlers.py", line 274, in doRollover
    > os.rename(self.baseFilename, dfn)
    > WindowsError: [Error 32] The process cannot access the file because it is
    > being used by another process
    >
    > Python version - 2.5.4
    >
    > I guess my output should be like this for the next day
    > -a new log file rotate_test.<date format> should generate in the respective
    > location
    >
    > Please correct me if I am wrong.
    >
    >
    > Thanks for your time.
    >
    > Regards,
    > Lokesh
    >
    >

    Generally speaking, this error on os.rename() will occur if you haven't
    properly closed the file first, and if I recall correctly, Unix would
    have permitted a rename on an open file. You're on Windows. However, I
    don't know how that works with the logging package. If the program has
    terminated, and you run it a second time on the following day (or after
    changing the system time), then I'd expect no trouble. But I'm guessing
    your program is still running, and you just want to change from logging
    to yesterday's file to logging to today's file.

    If I had to guess, I'd say that you have two instances of the logger
    running, and the second one is still holding the handle open. But
    that's just a wild guess.

    Could someone else who is familiar with logging, and with
    TimedRotatingFileHandler in particular, jump in here? Preferably a
    Windows person?

    DaveA
     
    Dave Angel, Aug 10, 2009
    #5
  6. Lokesh

    Vinay Sajip Guest

    On Aug 10, 3:52 pm, Dave Angel <> wrote:
    > Lokesh Maremalla wrote:
    > > Traceback (most recent call last):
    > >   File "c:\Python25\lib\logging\handlers.py", line 74, in emit
    > >     self.doRollover()
    > >   File "c:\Python25\lib\logging\handlers.py", line 274, in doRollover
    > >     os.rename(self.baseFilename, dfn)
    > > WindowsError: [Error 32] The process cannot access the file because it is
    > > being used by another process

    >
    > Generally speaking, this error on os.rename() will occur if you haven't
    > properly closed the file first, and if I recall correctly, Unix would
    > have permitted a rename on an open file.  You're on Windows.  However, I
    > don't know how that works with theloggingpackage.  If the program has
    > terminated, and you run it a second time on the following day (or after
    > changing the system time), then I'd expect no trouble.  But I'm guessing
    > your program is still running, and you just want to change fromlogging
    > to yesterday's file tologgingto today's file.
    >
    > If I had to guess, I'd say that you have two instances of the logger
    > running, and the second one is still holding the handle open.  But
    > that's just a wild guess.
    >


    It's certainly possible that there are two instances of the handler,
    which would cause this problem. Other things which might cause this
    problem are the file handle inherited by a child process, an anti-
    virus scanner having the file open for scanning (anti-virus scanners
    often scan files which have just been written, like log files) and
    indexing software such as Google Desktop or Windows' own indexing
    service.

    I would advise using sysinternals tools such as FileMon and Handle to
    see what's holding the file open.

    Regards,


    Vinay Sajip
     
    Vinay Sajip, Aug 11, 2009
    #6
    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. Mark
    Replies:
    0
    Views:
    835
  2. Chris Shenton
    Replies:
    4
    Views:
    3,776
    Vinay Sajip
    Feb 23, 2007
  3. Gabriel Genellina

    Re: Problem with TimedRotatingFileHandler

    Gabriel Genellina, Jun 11, 2008, in forum: Python
    Replies:
    0
    Views:
    464
    Gabriel Genellina
    Jun 11, 2008
  4. Dave Angel
    Replies:
    0
    Views:
    219
    Dave Angel
    Oct 24, 2012
  5. Vinay Sajip
    Replies:
    0
    Views:
    231
    Vinay Sajip
    Oct 24, 2012
Loading...

Share This Page