当前位置: 首页>>代码示例>>Python>>正文


Python coloredlogs.ColoredFormatter方法代码示例

本文整理汇总了Python中coloredlogs.ColoredFormatter方法的典型用法代码示例。如果您正苦于以下问题:Python coloredlogs.ColoredFormatter方法的具体用法?Python coloredlogs.ColoredFormatter怎么用?Python coloredlogs.ColoredFormatter使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在coloredlogs的用法示例。


在下文中一共展示了coloredlogs.ColoredFormatter方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _build_formatter

# 需要导入模块: import coloredlogs [as 别名]
# 或者: from coloredlogs import ColoredFormatter [as 别名]
def _build_formatter(self):
        prefix_format = f'{self.prefix} ' if self.prefix else ''
        if COLORED_LOGS and sys.stdout.isatty():
            return coloredlogs.ColoredFormatter(
                prefix_format + COLORED_FORMAT,
                datefmt=CONSOLE_DATE_FORMAT,
                level_styles=COLORED_LEVEL_STYLES,
                field_styles={},
            )
        elif sys.stdout.isatty():
            return logging.Formatter(
                prefix_format + CONSOLE_FORMAT, datefmt=CONSOLE_DATE_FORMAT,
            )
        else:
            return logging.Formatter(
                prefix_format + LOGFILE_FORMAT, datefmt=LOGFILE_DATE_FORMAT,
            ) 
开发者ID:facebookresearch,项目名称:ParlAI,代码行数:19,代码来源:logging.py

示例2: init_logger

# 需要导入模块: import coloredlogs [as 别名]
# 或者: from coloredlogs import ColoredFormatter [as 别名]
def init_logger(log_requests=False):
    """ Initialize the logger """

    logger = logging.getLogger(__name__.split(".")[0])
    for handler in logger.handlers:  # pragma: no cover
        logger.removeHandler(handler)

    if os.name == "nt" and "colorama" in sys.modules:  # pragma: no cover
        colorama.init()

    formatter = coloredlogs.ColoredFormatter(fmt="%(asctime)s: %(message)s")
    handler = logging.StreamHandler(stream=sys.stdout)
    handler.setLevel(logging.DEBUG)
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    logger.setLevel(logging.DEBUG)
    logger.propagate = False

    if log_requests:
        requests.packages.urllib3.add_stderr_logger() 
开发者ID:SFDO-Tooling,项目名称:CumulusCI,代码行数:22,代码来源:logger.py

示例3: _make_stream_handler

# 需要导入模块: import coloredlogs [as 别名]
# 或者: from coloredlogs import ColoredFormatter [as 别名]
def _make_stream_handler(cls, level=None, stream=sys.stdout, colored=False):
        ch = logging.StreamHandler(stream=stream)
        ch.setLevel(level)
        formatter = None

        # if colored, try to import colorama & coloredlogs (by default, not in the requirements)
        if colored:
            try:
                import colorama
                from coloredlogs import ColoredFormatter
                colorama.init()
                formatter = ColoredFormatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
            except ImportError:
                colored = False

        # if we don't need or failed getting colored formatter
        if not colored or not formatter:
            formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

        ch.setFormatter(formatter)
        return ch 
开发者ID:allegroai,项目名称:trains,代码行数:23,代码来源:log.py

示例4: init_logger

# 需要导入模块: import coloredlogs [as 别名]
# 或者: from coloredlogs import ColoredFormatter [as 别名]
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 
开发者ID:SFDO-Tooling,项目名称:MetaCI,代码行数:23,代码来源:logger.py

示例5: init_log

# 需要导入模块: import coloredlogs [as 别名]
# 或者: from coloredlogs import ColoredFormatter [as 别名]
def init_log(logger, loglevel=0, no_ansi=False):
    """
    Initializes logging.
    Prints logs to console with level defined by loglevel
    Also prints verbose log to the multiqc data directory if available.
    (multiqc_data/multiqc.log)

    Args:
        loglevel (str): Determines the level of the log output.
    """
    # File for logging
    global log_tmp_dir, log_tmp_fn
    log_tmp_dir = tempfile.mkdtemp()
    log_tmp_fn = os.path.join(log_tmp_dir, 'multiqc.log')

    # Logging templates
    debug_template = '[%(asctime)s] %(name)-50s [%(levelname)-7s]  %(message)s'
    info_template = '[%(levelname)-7s] %(module)15s : %(message)s'

    # Base level setup
    logger.setLevel(getattr(logging, 'DEBUG'))

    # Set up the console logging stream
    console = logging.StreamHandler()
    console.setLevel(getattr(logging, loglevel))
    level_styles = coloredlogs.DEFAULT_LEVEL_STYLES
    level_styles['debug'] = {'faint': True}
    if loglevel == 'DEBUG':
        if no_ansi or not sys.stderr.isatty():
            console.setFormatter(logging.Formatter(debug_template))
        else:
            console.setFormatter(coloredlogs.ColoredFormatter(fmt=debug_template, level_styles=level_styles))
    else:
        if no_ansi or not sys.stderr.isatty():
            console.setFormatter(logging.Formatter(info_template))
        else:
            console.setFormatter(coloredlogs.ColoredFormatter(fmt=info_template, level_styles=level_styles))
    logger.addHandler(console)

    # Now set up the file logging stream if we have a data directory
    file_handler = logging.FileHandler(log_tmp_fn, encoding='utf-8')
    file_handler.setLevel(getattr(logging, 'DEBUG')) # always DEBUG for the file
    file_handler.setFormatter(logging.Formatter(debug_template))
    logger.addHandler(file_handler) 
开发者ID:ewels,项目名称:MultiQC,代码行数:46,代码来源:log.py

示例6: _setup_logger

# 需要导入模块: import coloredlogs [as 别名]
# 或者: from coloredlogs import ColoredFormatter [as 别名]
def _setup_logger():
    """
    Configures the root logger based on the default or user-specified settings.
    As needed, a StreamHandler is created for console logging or FileHandler
    is created for file logging. Either or both are attached to the root
    logger for use throughout FLORIS.

    Returns:
        logging.Logger: The root logger from the `logging` module.
    """
    # Configure logging for the root logger
    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)
    # level_styles = {'warning': {'color': 'red', 'bold': False}}
    fmt_console = "%(name)s %(levelname)s %(message)s"
    fmt_file = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"

    file_name = "floris_{:%Y-%m-%d-%H_%M_%S}.log".format(datetime.now())

    # TODO: understand why this doesnt work and fix it!
    # if logger.hasHandlers():
    #     print(logger.handlers, len(logger.handlers))
    #     for i, handler in enumerate(logger.handlers):
    #         print(i, handler)
    #         logger.removeHandler(handler)
    #     print(logger.handlers, len(logger.handlers))

    # Remove all existing handlers before adding new ones
    while logger.hasHandlers():
        logger.removeHandler(logger.handlers[0])

    # Configure and add the console handler
    if LOG_TO_CONSOLE:
        console_handler = logging.StreamHandler()
        console_handler.setLevel(CONSOLE_LEVEL)
        console_format = coloredlogs.ColoredFormatter(
            # level_styles=level_styles,
            fmt=fmt_console
        )
        console_handler.setFormatter(console_format)
        console_handler.addFilter(TracebackInfoFilter(clear=True))
        logger.addHandler(console_handler)

    # Configure and add the file handler
    if LOG_TO_FILE:
        file_handler = logging.FileHandler(file_name)
        file_handler.setLevel(FILE_LEVEL)
        file_format = logging.Formatter(fmt_file)
        file_handler.setFormatter(file_format)
        file_handler.addFilter(TracebackInfoFilter(clear=False))
        logger.addHandler(file_handler)

    return logger 
开发者ID:NREL,项目名称:floris,代码行数:55,代码来源:logging_manager.py


注:本文中的coloredlogs.ColoredFormatter方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。