Azam Khan
Azam Khan

Reputation: 47

Unable to get the backup log file when using RotatingFileHandler from python

Brief :

base.py:

import logging
from logging.handlers import TimedRotatingFileHandler
import os

slogFile = os.path.join(os.getcwd(), 'LOGS', 'App_Debug.log')
if True != os.path.isdir(os.path.join(os.getcwd(), 'LOGS')):
    os.mkdir(os.path.join(os.getcwd(), 'LOGS'))
logging.basicConfig(filename=slogFile, level=logging.DEBUG)
logging.basicConfig(format='%(asctime)s %(message)s')
logger = logging.getLogger("myApp")   
fmt = logging.Formatter(fmt='%(asctime)s %(message)s')
size=1024*1024*1 #1mb file size
logger = logging.getLogger("myApp")   
fmt = logging.Formatter(fmt='%(asctime)s %(message)s')

hdlr = logging.handlers.RotatingFileHandler(filename = slogFile ,mode='w', maxBytes=size, backupCount=5, encoding=None, delay=0)
hdlr.setFormatter(fmt)
logger.addHandler(hdlr)</em>

app_main1.py:

import base

base.logger.debug('xxx')

app_main2.py:

import base

base.logger.debug('xxx')

NOTE : for my application i am using another module that also recording the logs into the same file .

I am getting this error:

Traceback (most recent call last):

 File "C:\Python27\lib\logging\handlers.py", line 78, in emit

   self.doRollover()

 File "C:\Python27\lib\logging\handlers.py", line 141, in doRollover

  os.rename(self.baseFilename, dfn)

WindowsError: [Error 32] The process cannot access the file because it is being used by another process

Logged from file app_main1.py, line 59

Could you explain this to me?

I want do backup the log file when its reaches the max(1mb) size.

Upvotes: 2

Views: 2302

Answers (3)

Lone Ronin
Lone Ronin

Reputation: 2870

I got the same error while developing a flask application. To solve the problem, I had to change the environmental variable from "FLASK_DEBUG=1" to "FLASK_DEBUG=0". The reason being that turning debugging on leads to threading errors. I got the solution after reading this blog

Upvotes: 2

azalea
azalea

Reputation: 12610

My guess is that since you imported base.py twice, the RotatingFileHandler is setup twice, therefore it is accessed by two processes.

I had a similar problem today due to this reason. Details here.

I would suggest not importing base.py, but creating a child logger in app_main1.py and app_main2.py. You can refer to the documentation here.

Upvotes: 1

Vinay Sajip
Vinay Sajip

Reputation: 99317

You probably have the log file open in some other program, which is why it can't be renamed. This could be one of your programs, or an anti-virus or full-text indexer operating on disk files.

Upvotes: 2

Related Questions