當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。