def emit(self, record): self._lock.acquire(timeout=2) try: WatchedFileHandler.emit(self, record) finally: self._lock.release()
def emit(self, record): old_umask = os.umask(self.umask) try: # This does not work on py 2.6 # WatchedFileHandler is probably old style class in 2.6 # super(UmaskWatchedFileHandler, self).emit(record) WatchedFileHandler.emit(self, record) finally: os.umask(old_umask)
def emit(self, record): while True: try: WatchedFileHandler.emit(self, record) self.tries = 0 return except IOError as err: if self.tries == self.max_tries: raise self.stream.close() self.stream = self._open() self.tries += 1
class TextLogger(Logger): '''Logs messages with plain text. Rotating-friendly.''' FORMAT = '%(asctime)s [%(protocol)s:%(pid)s] %(srcname)s >> %(text)s' def __init__(self, filename, tz=timezone.utc): self.loghandler = WatchedFileHandler(filename, encoding='utf-8', delay=True) self.loghandler.setLevel(logging.INFO) self.loghandler.setFormatter(logging.Formatter('%(message)s')) self.tz = tz def log(self, msg: Message): d = msg._asdict() d['asctime'] = datetime.fromtimestamp(msg.time, self.tz).strftime('%Y-%m-%d %H:%M:%S') d['srcname'] = msg.src.alias d['srcid'] = msg.src.id self.loghandler.emit(logging.makeLogRecord({'msg': self.FORMAT % d})) def commit(self): pass
class TextLogger(Logger): """Logs messages with plain text. Rotating-friendly.""" FORMAT = "%(asctime)s [%(protocol)s:%(pid)s] %(srcname)s >> %(text)s" def __init__(self, filename, tz=timezone.utc): self.loghandler = WatchedFileHandler(filename, encoding="utf-8", delay=True) self.loghandler.setLevel(logging.INFO) self.loghandler.setFormatter(logging.Formatter("%(message)s")) self.tz = tz def log(self, msg: Message): d = msg._asdict() d["asctime"] = datetime.fromtimestamp(msg.time, self.tz).strftime("%Y-%m-%d %H:%M:%S") d["srcname"] = msg.src.alias d["srcid"] = msg.src.id self.loghandler.emit(logging.makeLogRecord({"msg": self.FORMAT % d})) def commit(self): pass
class MPHandler(logging.Handler): """ Logger class allowing serial logging from multiple processes into a single log file. To realize this, the MPHandler creates a thread listening for log messages, whereas the emit method has been overwritten by a method sending the log message to the listening thread. Therefore, only one process (the one hosting the listener thread) actually writes into the log file. """ def __init__(self, name, mode): logging.Handler.__init__(self) self._handler = WatchedFileHandler(name, mode) self.queue = multiprocessing.Queue(-1) t = threading.Thread(target=self.receive) t.daemon = True t.start() def setFormatter(self, fmt): logging.Handler.setFormatter(self, fmt) self._handler.setFormatter(fmt) def receive(self): while True: try: record = self.queue.get() self._handler.emit(record) except (KeyboardInterrupt, SystemExit): raise except EOFError: break except: traceback.print_exc(file=sys.stderr) def send(self, s): self.queue.put_nowait(s) def _format_record(self, record): # Ensure that exc_info and args have been serialized to strings # to suppress any chance of them not being able to be pickled. # Furthermore, this possibly reduces the size of messages sent # over the pipe. if record.args: record.msg = record.msg % record.args record.args = None if record.exc_info: self.format(record) record.exc_info = None return record def emit(self, record): try: s = self._format_record(record) self.send(s) except (KeyboardInterrupt, SystemExit): raise except: self.handleError(record) def close(self): self._handler.close() logging.Handler.close(self)
def emit(self, record): if not record.levelno == self.level: return WatchedFileHandler.emit(self, record)