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 loggroup
to the directory logdir
.
Make the containing directory group-writable.
$ chgrp loggroup logdir
$ chmod g+w logdir
Set the setgid bit on logdir
. That makes new files in logdir
always owned by the group. Otherwise, new files are owned by the creator's group.
$ chmod g+s logdir
Ensure all logging users belong to loggroup
.
$ usermod -a -G loggroup myuser
Ensure all writing processes have the right umask so they can make newly created files group-writable.
$ umask 0002
Now all members of the loggroup
group can create files in logdir
and overwrite each other's files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With