Package gavo :: Package protocols :: Module gavolog
[frames] | no frames]

Source Code for Module gavo.protocols.gavolog

 1  """ 
 2  Helper code for logging to files. 
 3   
 4  All logs that could be used both interactively and from the web server 
 5  but have group ownership gavo and mode (at least) 664.  Only then can 
 6  both parties write logs. 
 7   
 8  The RotatingFileHandler in this module tries to ensure this. 
 9  """ 
10   
11  #c Copyright 2008-2019, the GAVO project 
12  #c 
13  #c This program is free software, covered by the GNU GPL.  See the 
14  #c COPYING file in the source distribution. 
15   
16   
17  import grp 
18  import os 
19  import warnings 
20  from logging import handlers 
21   
22  from gavo import base 
23   
24   
25  try: 
26          GAVO_GROUP_ID = grp.getgrnam(base.getConfig("group"))[2] 
27  except KeyError: 
28          warnings.warn("Cannot figure out id of group '%s'.  Logging will break.") 
29          GAVO_GROUP_ID = -1 
30   
31   
32 -class RotatingFileHandler(handlers.RotatingFileHandler):
33 """logging.handler.RotatingFile with forced group support. 34 """
35 - def __init__(self, *args, **kwargs):
36 handlers.RotatingFileHandler.__init__(self, *args, **kwargs) 37 self._setOwnership()
38
39 - def _setOwnership(self):
40 # This will fail if we don't own the file. This doesn't hurt as long 41 # as whoever created the file already fixed the permission 42 try: 43 os.chmod(self.stream.name, 0664) 44 os.chown(self.stream.name, -1, GAVO_GROUP_ID) 45 except os.error: # don't worry, see above 46 pass
47
48 - def doRollover(self):
49 handlers.RotatingFileHandler.doRollover(self) 50 self._setOwnership()
51