Django: prevent log permissions errors from the shell

From the Django shell (manage.py shell), when I try to import a python module that uses logging, I run into permission problems: the log files belong to the user of the web application.

IOError: [Errno 13] Permission denied: '/path/to/my.log' 

Is there a way to disable / mock / otherwise work with this problem so that I can use the module from the shell?

+4
source share
4 answers

Use an alternative settings file that loads all the usual settings and then changes the location of the log file.

+1
source

I am dealing with this problem at the moment, and my current plan is to simply check write access to the log directory in settings.py.

If I don’t have one, I will write a stderr warning and skip the configuration of the log handlers, not the failure.

+1
source

you can use the try-except block: try writing to the log file, and if theres an IOException, either write the log in another place (/ tmp, or in the current directory), or just turn off logging.

See also: http://docs.python.org/tutorial/errors.html#handling-exceptions

0
source

Just to give a more complete answer about why this is in the interest of Google users. Your server or shell most likely has an umask set. By default, it looks like the Python logger wants to set the log files to 666 , but first runs this value through the current set umask value, which is probably set to something like 002 so that it removes read and write permissions from any files created.

In bash, you can check your umask by running:

 umask 

and you can get rid of it by doing:

 umask 0 

In Python, you can reset it by adding:

 import os os.umask(0) 

I would make sure that both of them are 0 before trying to create any log files to avoid accidental permission errors.

-2
source

All Articles