Python coloredlogs 模块,ColoredFormatter() 实例源码

我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用coloredlogs.ColoredFormatter()

项目:MetaCI    作者:SalesforceFoundation    | 项目源码 | 文件源码
def init_logger(model):
    """ Initialize the logger. """

    logger = logging.getLogger('cumulusci')

    # Remove existing handlers
    for handler in list(logger.handlers):
        handler.stream.flush(force=True)
        logger.removeHandler(handler)

    # Create the custom handler
    formatter = coloredlogs.ColoredFormatter(fmt='%(asctime)s: %(message)s')
    handler = LogHandler(model)
    handler.setLevel(logging.DEBUG)
    handler.setFormatter(formatter)

    logger.addHandler(handler)
    logger.setLevel(logging.DEBUG)
    logger.propagate = False

    return logger
项目:satellite-populate    作者:SatelliteQE    | 项目源码 | 文件源码
def set_logger(verbose):
    """Set logger verbosity used when client is called with -vvvvv"""

    for _logger in list(logging.Logger.manager.loggerDict.values()):  # noqa
        if not isinstance(_logger, logging.Logger):
            continue
        if coloredlogs is not None:
            _logger.propagate = False

            handler = logging.StreamHandler()
            formatter = coloredlogs.ColoredFormatter(
                fmt="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
                datefmt="%Y-%m-%d %H:%M:%S",
            )
            handler.setFormatter(formatter)
            _logger.handlers = [handler]
        _logger.setLevel(verbose * 10)
项目:wpsik    作者:0x90    | 项目源码 | 文件源码
def __init__(self, interface, channel=None, timeout=5, output=None, passive=False, mac=None, logfile=None):
        self.interface = interface
        self.channel = channel
        self.timeout = timeout
        self.output = output
        self.passive = passive
        self.mac = mac if is_valid_mac_address(mac) else None
        self.logfile = logfile
        #
        self.aps = {}
        self.wps_aps = {}
        self.captured = []
        self.probes_sent = []
        self._stop = False
        #
        self.wps_parser = WpsParser()
        self.rtDecoder = RadioTapDecoder()

        # Initialize logger
        self.logger = logging.getLogger('airlog')
        self.logger.setLevel(logging.INFO)

        # Console logging
        ch = logging.StreamHandler()
        ch.setLevel(logging.INFO)
        formatter = coloredlogs.ColoredFormatter('[%(asctime)s] - %(levelname)s - %(message)s',
                                                 datefmt='%d.%m.%Y %H:%M:%S')
        ch.setFormatter(formatter)
        self.logger.addHandler(ch)

        # Logging to file
        if logfile is None:
            return
        fh = logging.FileHandler(logfile)
        fh.setLevel(logging.DEBUG)
        formatter = logging.Formatter('[%(asctime)s] - %(message)s', datefmt='%d.%m.%Y %H:%M:%S')
        fh.setFormatter(formatter)
        self.logger.addHandler(fh)
项目:OldSpeak    作者:0rbitAeolian    | 项目源码 | 文件源码
def configure_logging(level_name, stream=None, filename=None):
    relevant_loggers = [
        logging.getLogger(),
        logging.getLogger('oldspeak'),
        logging.getLogger('sqlalchemy'),
        logging.getLogger('werkzeug'),
    ]
    available_handlers = [
    ]

    logging.Logger.manager.loggerDict.clear()
    level = getattr(logging, bytes(level_name), b'INFO')

    TEST_MODE = os.getenv('TEST_MODE')
    TEST_NOLOGS = TEST_MODE in ['nologs', 'no-logs']

    if TEST_NOLOGS:
        stream = None
        filename = None
        level_name = 'WARNING'

    elif not TEST_NOLOGS:
        stream = sys.stderr
        filename = None
        level_name = 'DEBUG'

    stream_handler = None
    file_handler = None

    if stream:
        stream_handler = logging.StreamHandler(stream=stream)
        available_handlers.append(stream_handler)

    if filename:
        file_handler = logging.FileHandler(filename=filename, encoding='utf-8')
        available_handlers.append(file_handler)

    if not stream and not filename:
        available_handlers.append(logging.NullHandler())

    def setup_logger(logger):
        logger.handlers = []
        logger.setLevel(level=level)
        for handler in available_handlers:
            logger.addHandler(handler)

    if stream_handler:
        stream_handler.setLevel(level)
        stream_handler.setFormatter(ColoredFormatter(
            fmt='%(asctime)s %(name)s %(levelname)s %(message)s'))

    if file_handler:
        json_formatter = JsonFormatter(
            '%(levelname)s %(asctime)s %(module)s %(process)d %(message)s %(pathname)s $(lineno)d $(funcName)s')

        file_handler.setLevel(level)
        file_handler.setFormatter(json_formatter)

    for logger in relevant_loggers:
        setup_logger(logger)