예제 #1
0
    def emit(self, record):
        self._lock.acquire(timeout=2)
        try:
            WatchedFileHandler.emit(self, record)

        finally:
            self._lock.release()
예제 #2
0
    def emit(self, record):
        self._lock.acquire(timeout=2)
        try:
            WatchedFileHandler.emit(self, record)

        finally:
            self._lock.release()
예제 #3
0
 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)
예제 #4
0
 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
예제 #5
0
 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
예제 #6
0
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
예제 #7
0
파일: logger.py 프로젝트: gumblex/orizonhub
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
예제 #8
0
파일: log.py 프로젝트: daemotron/backtory
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)
예제 #9
0
 def emit(self, record):
     if not record.levelno == self.level:
         return
     WatchedFileHandler.emit(self, record)