class MultiproccessingStreamHandler(logging.Handler): """This stream handler makes it possible for several processes to log to the same stdout by using a queue. """ _handler = None _queue = None def __init__(self): logging.Handler.__init__(self) # create StreamHandler self._handler = StreamHandler() # setup queue and thread self._queue = multiprocessing.Queue(-1) thread = threading.Thread(target=self.receive) thread.daemon = True thread.start() def setFormatter(self, fmt): """Set the Formatter for this handler to fmt.""" logging.Handler.setFormatter(self, fmt) self._handler.setFormatter(fmt) def receive(self): """Thread which is run in the background. This Thread gets logging records from the queue and emits them.""" while True: try: record = self._queue.get() self._handler.emit(record) except (KeyboardInterrupt, SystemExit): raise except EOFError: break except: break def emit(self, record): """Log the logging record by putting it into the queue.""" try: self._queue.put_nowait(record) except (KeyboardInterrupt, SystemExit): raise except: self.handleError(record) def close(self): """Close handler.""" self._handler.close() logging.Handler.close(self)
def emit(self, record): """ Emit a record. If the stream was not opened because 'delay' was specified in the constructor, open it before calling the superclass's emit. """ brand = settings.BRAND if brand is None: brand = getattr(record, _BRAND) key = brand + self.baseFilename if self.stream_cache.get(key) is None: self.stream_cache[key] = self._open_file(brand) self.stream = self.stream_cache[key] if hasattr(record, _BRAND) is None: record.__dict__[_BRAND] = 'BACKGROUND-JOB' StreamHandler.emit(self, record)
#!encoding=utf-8 from logging import StreamHandler from logging import LogRecord import sys onelog=LogRecord('name','10',pathname=__file__,lineno=10,msg=u'haha哈',args=None,exc_info=None,func=None) S=StreamHandler(stream=sys.stdout) S.emit(onelog)
def emit(self, record): """ Emit the log record. """ record.__dict__['user'] = self.user record.__dict__['host'] = self.host return StreamHandler.emit(self, record)
class MPLogHandler(logging.Handler): """multiprocessing log handler This handler makes it possible for several processes to log to the same file by using a queue. :param out_file: name of output file, if None then is output to stdout/stderr :type out_file: str """ def __init__(self, out_file = None): logging.Handler.__init__(self) if out_file is not None: self._handler = FH(out_file) else: self._handler = SH() self.queue = multiprocessing.Queue(-1) atexit.register(logging.shutdown) self._thrd = None self._is_child = False # Children will automatically register themselves as chilcren register_after_fork(self, MPLogHandler.set_is_child) def set_is_child(self): self._is_child = True def start_recv_thread(self): if self._thrd: return self._shutdown = False thrd = threading.Thread(target=self.receive) thrd.daemon = True thrd.start() self._thrd = thrd def setFormatter(self, fmt): logging.Handler.setFormatter(self, fmt) self._handler.setFormatter(fmt) def receive(self): while not self._shutdown: try: record = self.queue.get(True, 0.3) self._handler.emit(record) except (Queue.Empty,IOError): pass except (KeyboardInterrupt, SystemExit): raise except (EOFError,TypeError): break except: traceback.print_exc(file=sys.stderr) def shutdown_recv_thread(self): if self._thrd: self._shutdown = True self._thrd.join() self._thrd = None def send(self, s): self.queue.put_nowait(s) def _format_record(self, record): if record.args: record.msg = record.msg % record.args record.args = None if record.exc_info: dummy = self.format(record) record.exc_info = None return record def emit(self, record): try: s = self._format_record(record) # If we are a child, then send the record, otherwise simply emit it if self._is_child: self.send(s) else: self._handler.emit(s) except (KeyboardInterrupt, SystemExit): raise except: self.handleError(record) def close(self): self._handler.close() self.shutdown_recv_thread() logging.Handler.close(self)
def emit(x, record): if self.debug: StreamHandler.emit(x, record)
def emit(self, record): if app.debug and _should_log_for(app, 'debug'): StreamHandler.emit(self, record)
def emit(x, record): StreamHandler.emit(x, record) if debug_on else None
def emit(self, record): if not app.debug and _should_log_for(app, 'production'): StreamHandler.emit(self, record)
def emit(self, record): if self.stream is None: self.stream = self._open() StreamHandler.emit(self, record)
def emit(self, record): global _stdout StreamHandler.emit(self, record) if _stdout else None
def emit(x, record): StreamHandler.emit(x, record) if config.debug else None
def emit(self, record): StreamHandler.emit(self, record)
def emit(x, record): StreamHandler.emit(x, record) if self.sandbox else None
def emit(x, record): StreamHandler.emit(x, record)
def emit(x, record): StreamHandler.emit(x, record) if config['DEBUG'] else None
def emit(self, record): if self.rex and self.rex.findall(self.format(record)): return StreamHandler.emit(self, record)
def emit(x, record): StreamHandler.emit(x, record) if app.debug else None
def emit(self, record): if not record.levelno == INFO: return StreamHandler.emit(self, record)
def emit(self, x, record): StreamHandler.emit(x, record) if APP_DEBUG else None
def emit(x, record): pp = PrettyPrinter(indent=2) record.msg = pp.pformat(record.msg) StreamHandler.emit(x, record) if app.debug else None