Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python logging to same file, different users

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 :)

like image 696
Juan Antonio Gomez Moriano Avatar asked Sep 03 '25 07:09

Juan Antonio Gomez Moriano


1 Answers

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.

  1. Make the containing directory group-writable.

    $ chgrp loggroup logdir
    $ chmod g+w logdir
    
  2. 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
    
  3. Ensure all logging users belong to loggroup.

    $ usermod -a -G loggroup myuser
    
  4. Ensure all writing processes have the right umask so they can make newly created files group-writable.

    $ umask 0002
    
  5. Now all members of the loggroup group can create files in logdir and overwrite each other's files.

like image 123
Noel Burton-Krahn Avatar answered Sep 04 '25 19:09

Noel Burton-Krahn