Python logging.handlers.append() Examples

The following are 9 code examples of logging.handlers.append(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module logging.handlers , or try the search function .
Example #1
Source File: setup_logger.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def multi_func():

    # 启动多进程
    pool = multiprocessing.Pool(multiprocessing.cpu_count())

    setup_logger('logs/MyLog')
    logger = get_logger()
    logger.info('main process')
    l = []

    for i in range(0,10):
        l.append(pool.apply_async(single_func,(i,)))

    results = [res.get() for res in l]

    pool.close()
    pool.join() 
Example #2
Source File: log.py    From xscontainer with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def configurelogging():
    _LOGGER.setLevel(logging.DEBUG)
    formatter = logging.Formatter(
        '%(asctime)s - [%(process)d] - %(levelname)s - %(message)s',
        '%Y-%m-%d %H:%M:%S')

    handlers = []
    log_level = logging.INFO

    if os.access(os.path.dirname(LOG_FILE), os.W_OK):
        fileh = logging.handlers.RotatingFileHandler(
            LOG_FILE, maxBytes=5 * M, backupCount=5)
        handlers.append(fileh)

    if os.path.exists(ENABLE_DEV_LOGGING_FILE) or not handlers:
        handlers.append(logging.StreamHandler(sys.stdout))
        log_level = logging.DEBUG

    # Configure and add all handlers
    for handler in handlers:
        handler.setLevel(log_level)
        handler.setFormatter(formatter)
        _LOGGER.addHandler(handler)

    signal.signal(signal.SIGPIPE, signal.SIG_DFL) 
Example #3
Source File: setup_logger.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def getFilesToDelete(self):
        """获得过期需要删除的日志文件"""
        #分离出日志文件夹绝对路径
        #split返回一个元组(absFilePath,fileName)
        #例如:split('I:\ScripPython\char4\mybook\util\logs\mylog.2017-03-19)
        #返回(I:\ScripPython\char4\mybook\util\logs, mylog.2017-03-19)
        # _ 表示占位符,没什么实际意义,
        dirName,_ = os.path.split(self.baseFilename)
        fileNames = os.listdir(dirName)
        result = []
        #self.prefix 为日志文件名 列如:mylog.2017-03-19 中的 mylog
        #加上 点号 . 方便获取点号后面的日期
        prefix = self.prefix + '.'
        plen = len(prefix)
        for fileName in fileNames:
            if fileName[:plen] == prefix:
                #日期后缀 mylog.2017-03-19 中的 2017-03-19
                suffix = fileName[plen:]
                #匹配符合规则的日志文件,添加到result列表中
                if re.compile(self.extMath).match(suffix):
                    result.append(os.path.join(dirName,fileName))
        result.sort()

        #返回  待删除的日志文件
        #   多于 保留文件个数 backupCount的所有前面的日志文件。
        if len(result) < self.backupCount:
            result = []
        else:
            result = result[:len(result) - self.backupCount]
        return result 
Example #4
Source File: configuration.py    From loopchain with Apache License 2.0 5 votes vote down vote up
def _update_handlers(self, logger):
        logging._acquireLock()
        try:
            for handler in logger.handlers[:]:
                logger.removeHandler(handler)

            handlers = []

            partial_new_excepthook = new_excepthook
            partial_new_print_exception = new_print_exception

            if self.log_output_type & conf.LogOutputType.console:
                stream_handler = self._create_stream_handler()
                handlers.append(stream_handler)

                partial_new_excepthook = partial(partial_new_excepthook, console=True)
                partial_new_print_exception = partial(partial_new_print_exception, console=True)
            else:
                partial_new_excepthook = partial(partial_new_excepthook, console=False)
                partial_new_print_exception = partial(partial_new_print_exception, console=False)

            if self.log_output_type & conf.LogOutputType.file and self.log_file_location:
                self._update_log_file_path()

                file_handler = self._create_file_handler()
                handlers.append(file_handler)

                sys.excepthook = partial(partial_new_excepthook, output_file=file_handler.stream)
                traceback.print_exception = partial(partial_new_print_exception, output_file=file_handler.stream)
            else:
                sys.excepthook = partial(partial_new_excepthook, output_file=None)
                traceback.print_exception = partial(partial_new_print_exception, output_file=None)

            logging.basicConfig(handlers=handlers,
                                format=self._log_format,
                                datefmt="%Y-%m-%d %H:%M:%S",
                                level=self._log_level)
        finally:
            logging._releaseLock() 
Example #5
Source File: __main__.py    From vidcutter with GNU General Public License v3.0 5 votes vote down vote up
def init_logger(self) -> None:
        try:
            log_path = self.get_app_config_path()
        except AttributeError:
            if sys.platform == 'win32':
                log_path = os.path.join(QDir.homePath(), 'AppData', 'Local', qApp.applicationName().lower())
            elif sys.platform == 'darwin':
                log_path = os.path.join(QDir.homePath(), 'Library', 'Preferences', qApp.applicationName().lower())
            else:
                log_path = os.path.join(QDir.homePath(), '.config', qApp.applicationName().lower())
        os.makedirs(log_path, exist_ok=True)
        self.console = ConsoleWidget(self)
        self.consoleLogger = ConsoleHandler(self.console)
        handlers = [logging.handlers.RotatingFileHandler(os.path.join(log_path, '%s.log'
                                                                      % qApp.applicationName().lower()),
                                                         maxBytes=1000000, backupCount=1),
                    self.consoleLogger]
        if self.parser.isSet(self.debug_option) or self.verboseLogs:
            # noinspection PyTypeChecker
            handlers.append(logging.StreamHandler())
        logging.setLoggerClass(VideoLogger)
        logging.basicConfig(handlers=handlers,
                            format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                            datefmt='%Y-%m-%d %H:%M',
                            level=logging.INFO)
        logging.captureWarnings(capture=True)
        sys.excepthook = MainWindow.log_uncaught_exceptions
        if os.getenv('DEBUG', False):
            logging.info('appconfig folder: {}'.format(log_path)) 
Example #6
Source File: logging.py    From reframe with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _create_file_handler(site_config, config_prefix):
    filename = site_config.get(f'{config_prefix}/name')
    timestamp = site_config.get(f'{config_prefix}/timestamp')
    if timestamp:
        basename, ext = os.path.splitext(filename)
        filename = '%s_%s%s' % (basename, time.strftime(timestamp), ext)

    append = site_config.get(f'{config_prefix}/append')
    return logging.handlers.RotatingFileHandler(filename,
                                                mode='a+' if append else 'w+') 
Example #7
Source File: logging.py    From reframe with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _create_filelog_handler(site_config, config_prefix):
    basedir = os.path.abspath(site_config.get(f'{config_prefix}/basedir'))
    prefix  = site_config.get(f'{config_prefix}/prefix')
    filename_patt = os.path.join(basedir, prefix)
    append = site_config.get(f'{config_prefix}/append')
    return MultiFileHandler(filename_patt, mode='a+' if append else 'w+') 
Example #8
Source File: logging.py    From reframe with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _extract_handlers(site_config, handlers_group):
    handler_prefix = f'logging/0/{handlers_group}'
    handlers_list = site_config.get(handler_prefix)
    handlers = []
    for i, handler_config in enumerate(handlers_list):
        handler_type = handler_config['type']
        if handler_type == 'file':
            hdlr = _create_file_handler(site_config, f'{handler_prefix}/{i}')
        elif handler_type == 'filelog':
            hdlr = _create_filelog_handler(
                site_config, f'{handler_prefix}/{i}'
            )
        elif handler_type == 'syslog':
            hdlr = _create_syslog_handler(site_config, f'{handler_prefix}/{i}')
        elif handler_type == 'stream':
            hdlr = _create_stream_handler(site_config, f'{handler_prefix}/{i}')
        elif handler_type == 'graylog':
            hdlr = _create_graylog_handler(
                site_config, f'{handler_prefix}/{i}'
            )
            if hdlr is None:
                getlogger().warning('could not initialize the '
                                    'graylog handler; ignoring ...')
                continue
        else:
            # Should not enter here
            raise AssertionError(f"unknown handler type: {handler_type}")

        level = site_config.get(f'{handler_prefix}/{i}/level')
        fmt = site_config.get(f'{handler_prefix}/{i}/format')
        datefmt = site_config.get(f'{handler_prefix}/{i}/datefmt')
        hdlr.setFormatter(RFC3339Formatter(fmt=fmt, datefmt=datefmt))
        hdlr.setLevel(_check_level(level))
        handlers.append(hdlr)

    return handlers 
Example #9
Source File: log.py    From pytest-services with MIT License 5 votes vote down vote up
def remove_handlers():
    """Remove root services_logging handlers."""
    handlers = []
    for handler in logging.root.handlers:
        if not isinstance(handler, logging.StreamHandler):
            handlers.append(handler)
    logging.root.handlers = handlers