def __init__(self, log_file, level): self.level = level self.log_info = "0.0.0.0: " self.file = log_file self.pid = os.getpid() self.real = 0 if self.file in ["stderr", "stdout"]: self.fd = getattr(sys, self.file) self.log_info = "" return newfileYN = 0 if not os.path.exists(self.file): newfileYN = 1 # just used for the chown/chmod # else, open it as a real file, with locking and stuff try: # try to open it in line buffered mode self.fd = open(self.file, "a", 1) set_close_on_exec(self.fd) if newfileYN: apache_uid, apache_gid = getUidGid('apache', 'apache') if os.getuid() == 0: os.chown(self.file, apache_uid, 0) else: os.chown(self.file, apache_uid, apache_gid) os.chmod(self.file, 0660) except: log_stderr("ERROR LOG FILE: Couldn't open log file %s" % self.file, sys.exc_info()[:2]) self.file = "stderr" self.fd = sys.stderr else: self.real = 1
def initLOG(log_file="stderr", level=0): global LOG # check if it already setup if LOG is not None: # We already have a logging object if log_file is None or LOG.file == log_file: # Keep the same logging object, change only the log level LOG.level = level return # We need a different type, so destroy the old one LOG = None elif log_file is None: log_file = "/dev/null" # attempt to create the path to the log file if neccessary log_path = os.path.dirname(log_file) if log_file not in ('stderr', 'stdout') \ and log_path and not os.path.exists(os.path.dirname(log_file)): log_stderr( "WARNING: log path not found; attempting to create %s" % log_path, sys.exc_info()[:2]) # fetch uid, gid so we can do a "chown ..." if isSUSE(): apache_uid, apache_gid = getUidGid('wwwrun', 'www') else: apache_uid, apache_gid = getUidGid('apache', 'apache') try: os.makedirs(log_path) if os.getuid() == 0: os.chown(log_path, apache_uid, 0) else: os.chown(log_path, apache_uid, apache_gid) except: log_stderr("ERROR: unable to create log file path %s" % log_path, sys.exc_info()[:2]) return # At this point, LOG is None and log_file is not None # Get a new LOG LOG = rhnLog(log_file, level) return 0
def initLOG(log_file="stderr", level=0): global LOG # check if it already setup if LOG is not None: # We already have a logging object if log_file is None or LOG.file == log_file: # Keep the same logging object, change only the log level LOG.level = level return # We need a different type, so destroy the old one LOG = None elif log_file is None: log_file = "/dev/null" # attempt to create the path to the log file if neccessary log_path = os.path.dirname(log_file) if log_file not in ('stderr', 'stdout') \ and log_path and not os.path.exists(os.path.dirname(log_file)): log_stderr("WARNING: log path not found; attempting to create %s" % log_path, sys.exc_info()[:2]) # fetch uid, gid so we can do a "chown ..." if isSUSE(): apache_uid, apache_gid = getUidGid('wwwrun', 'www') else: apache_uid, apache_gid = getUidGid('apache', 'apache') try: os.makedirs(log_path) if os.getuid() == 0: os.chown(log_path, apache_uid, 0) else: os.chown(log_path, apache_uid, apache_gid) except: log_stderr("ERROR: unable to create log file path %s" % log_path, sys.exc_info()[:2]) return # At this point, LOG is None and log_file is not None # Get a new LOG LOG = rhnLog(log_file, level) return 0