I am using the logging module of python.
In the case of unit tests (we use py.test) every time tests are launched, some log information goes to a certain file. In the integration server, everytime someone pushes code (we also use git 🙂 we run the tests.
The problem is that once the file is created, by user A when user B tries to run the tests, the tests will fail as user B has no permission to write on the same file.
So far, we have changed the file permissions manually, but looks like a dirty solution. Also we though of creating a log file per user, but again, does not feel right.
Our code for the logging in the tests is
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M',
filename='/tmp/py.test.log',
filemode='w')
log.setLevel(logging.DEBUG)
log.addHandler(logging.StreamHandler())
Is there a way to avoid this problem? Maybe using filemode = ‘a’ could do it, but lets assume I want a new file everytime (to be honest this is more curiosity that a real problem, still I want to do it right)
Thanks 🙂
It sounds like the logging process is trying to write on top of an existing file that’s owned by another user. Here is a procedure for allowing group access for the group
loggroupto the directorylogdir.Make the containing directory group-writable.
Set the setgid bit on
logdir. That makes new files inlogdiralways owned by the group. Otherwise, new files are owned by the creator’s group.Ensure all logging users belong to
loggroup.Ensure all writing processes have the right umask so they can make newly created files group-writable.
Now all members of the
loggroupgroup can create files inlogdirand overwrite each other’s files.