我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用logging.handlers.MemoryHandler()。
def log_register(log_name): """ Acquire a logger object, initialized with the global level and output options :param log_name: logger name (as in `logging.getLogger`), will be prefixed to log lines :return: A `logging.Logger` instance. """ logger = logging.getLogger(log_name) logger.setLevel(settings.LOG_LEVEL) if _enable_stdout: _add_stream_handler(logger) if _log_filename: _add_file_handler(logger) if not _log_filename and not _enable_stdout: # Prevent 'No handlers could be found' spam logger.addHandler(MemoryHandler(0)) _loggers.add(logger) return logger
def make_mem_logger(name, to_log, size=8192): """ Create a memory logger to avoid writing concurrently to the main logger """ from logging.handlers import MemoryHandler logger = logging.getLogger(name) hdlr = MemoryHandler(size, target=to_log) formatter = logging.Formatter('%(message)s') hdlr.setFormatter(formatter) logger.addHandler(hdlr) logger.memhandler = hdlr logger.setLevel(logging.DEBUG) return logger
def __init__(self, locale=None, target=None): """Initialize a TranslationHandler :param locale: locale to use for translating messages :param target: logging.Handler object to forward LogRecord objects to after translation """ # NOTE(luisg): In order to allow this handler to be a wrapper for # other handlers, such as a FileHandler, and still be able to # configure it using logging.conf, this handler has to extend # MemoryHandler because only the MemoryHandlers' logging.conf # parsing is implemented such that it accepts a target handler. handlers.MemoryHandler.__init__(self, capacity=0, target=target) self.locale = locale