Author: tmckay
Date: 2011-02-22 17:55:24 +0000 (Tue, 22 Feb 2011)
New Revision: 4550
Modified:
trunk/parsley/python/parsley/loggingex.py
Log:
If a log file is created by root, make the ownership
match the ownership if the containing directory. This
applies in a rollover case as well.
Modified: trunk/parsley/python/parsley/loggingex.py
===================================================================
--- trunk/parsley/python/parsley/loggingex.py 2011-02-22 15:30:12 UTC (rev 4549)
+++ trunk/parsley/python/parsley/loggingex.py 2011-02-22 17:55:24 UTC (rev 4550)
@@ -1,6 +1,6 @@
import logging
-from logging import handlers
-
+import os
+from logging.handlers import RotatingFileHandler
from collectionsex import defaultdict
_levels_by_name = {
@@ -13,6 +13,31 @@
_handlers_by_logger = defaultdict(list)
+def set_log_owner(file_obj):
+ # If the current user is root, make sure
+ # that the file has the same ownership
+ # as the containing directory
+ if os.getuid() == 0:
+ try:
+ dir_name = os.path.split(file_obj)[0]
+ dir_stat = os.stat(dir_name)
+ os.chown(file_obj, dir_stat.st_uid, dir_stat.st_gid)
+ except:
+ pass
+
+class MyRotatingFileHandler(RotatingFileHandler):
+ def __init__(self, filename, maxBytes, backupCount):
+ RotatingFileHandler.__init__(self,
+ filename,
+ maxBytes = maxBytes,
+ backupCount = backupCount)
+
+ def doRollover(self):
+ RotatingFileHandler.doRollover(self)
+ if hasattr(self, "baseFilename"):
+ # Try to fix up permissions on new file
+ set_log_owner(self.baseFilename)
+
def enable_logging(name, level, file_obj, max_MB = 0, max_archives = 0):
assert level
assert file_obj
@@ -23,9 +48,16 @@
level = _levels_by_name[level.lower()]
if type(file_obj) is str:
- handler = logging.handlers.RotatingFileHandler(file_obj,
- maxBytes = max_MB * 1024 *1024,
- backupCount = max_archives)
+ # Assume file_obj is a path name. If the file does not
+ # exist try to create the file with the correct uid/gid
+ # based on the containing dir.
+ if not os.path.exists(file_obj):
+ open(file_obj, 'a+')
+ set_log_owner(file_obj)
+
+ handler = MyRotatingFileHandler(file_obj,
+ maxBytes = max_MB * 1024 *1024,
+ backupCount = max_archives)
else:
handler = logging.StreamHandler(file_obj)
Show replies by date