Django logging with RotatingFileHandler error
May 01, 2014When using RotatingFileHandler for django logging, the following error occurs:
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
tl; dr
If you use settings.py to specify the logging configuration, and you are running django development server, run it with “–noreload” option may help, i.e.
python manage.py runserver --noreload
The reason is that by default, two processes of Django servers are running. One is the actual server, while the other is to detect changes in the code and reload the server. Therefore, settings.py is imported twice, and consequently the two processes are accessing the log file at the same time.
Warning: the following is irrelavent.
I found out this simple solution through hours of googling.
I came across multiple discusssions about this issue (eg1, eg2, eg3), but none gave the solution. Amid continuous frustrations, this amused me:
At one point, I thought this was because file handles are inherited by child processes in Python, and I even tried to overwrite the __builtin__.open()
function.
I also checked out ConcurrentLogHandler, and thought about writing my own log handler that uses socket.
Finally, this answer enlightened me. I never knew settings.py was imported twice by Django development server!
Lessons learned:
-
Post the answer to your own question if you find out the solution. Someone may thank you one day, denvercoder9.
-
The most obvious solution is most easily neglected.