當前位置: 首頁>>代碼示例>>Python>>正文


Python colorlog.StreamHandler方法代碼示例

本文整理匯總了Python中colorlog.StreamHandler方法的典型用法代碼示例。如果您正苦於以下問題:Python colorlog.StreamHandler方法的具體用法?Python colorlog.StreamHandler怎麽用?Python colorlog.StreamHandler使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在colorlog的用法示例。


在下文中一共展示了colorlog.StreamHandler方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: get_logger

# 需要導入模塊: import colorlog [as 別名]
# 或者: from colorlog import StreamHandler [as 別名]
def get_logger(command, verbose, daemon):
    """Return a Logger object with the right level, formatter and handler."""

    if daemon:
        handler = SysLogHandler(address=get_domain_socket())
        formatter = logging.Formatter(fmt=DAEMON_FORMAT.format(command))
        logger = logging.getLogger(command)
    else:
        handler = colorlog.StreamHandler(stream=sys.stdout)
        formatter = colorlog.ColoredFormatter(INTERACTIVE_FORMAT,
                                              log_colors=LOG_COLORS)
        logger = colorlog.getLogger(command)

    if verbose:
        logger.setLevel(logging.DEBUG)
    else:
        logger.setLevel(logging.INFO)

    handler.setFormatter(formatter)
    logger.addHandler(handler)

    return logger 
開發者ID:koenvervloesem,項目名稱:hermes-audio-server,代碼行數:24,代碼來源:logger.py

示例2: get

# 需要導入模塊: import colorlog [as 別名]
# 或者: from colorlog import StreamHandler [as 別名]
def get(cls, tag):
        """
        Get the global logger instance for the given tag

        :param tag: the log tag
        :return: the logger instance
        """
        if tag not in cls._LOGGERS:
            if cls._use_color:
                handler = colorlog.StreamHandler()
                handler.setFormatter(colorlog.ColoredFormatter( \
                    ' %(log_color)s%(name)s/%(levelname)-8s%(reset)s |'
                    ' %(log_color)s%(message)s%(reset)s'))
            else:
                handler = logging.StreamHandler()
                handler.setFormatter(logging.Formatter( \
                    ' %(name)s/%(levelname)-8s | %(message)s'))

            logger = logging.getLogger(tag)
            logger.addHandler(handler)

            cls._LOGGERS[tag] = logger

        return cls._LOGGERS[tag] 
開發者ID:cyanogen,項目名稱:uchroma,代碼行數:26,代碼來源:log.py

示例3: setup_logger

# 需要導入模塊: import colorlog [as 別名]
# 或者: from colorlog import StreamHandler [as 別名]
def setup_logger(logger_name, log_file, level=logging.DEBUG):
    l = logging.getLogger(logger_name)

    formatter = logging.Formatter('[%(name)s] %(asctime)s : %(message)s')

    logdir = "./log/"
    if not os.path.exists(logdir):
        os.makedirs(logdir)
    fileHandler = logging.FileHandler(logdir + log_file, mode='w')
    fileHandler.setFormatter(formatter)

    l.setLevel(level)
    l.addHandler(fileHandler)

    handler = colorlog.StreamHandler()
    colorformat = colorlog.ColoredFormatter('%(log_color)s[%(name)s] %(message)s - (%(asctime)s) %(lineno)d')
    handler.setFormatter(colorformat)

    l.addHandler(handler) 
開發者ID:economicnetwork,項目名稱:archon,代碼行數:21,代碼來源:custom_logger.py

示例4: configure_logging

# 需要導入模塊: import colorlog [as 別名]
# 或者: from colorlog import StreamHandler [as 別名]
def configure_logging(verbose=False, quiet=False, fail_silently=False, colorize=True):
    handler = colorlog.StreamHandler() if colorize else logging.StreamHandler()
    formatter = colorlog.ColoredFormatter(
        '%(log_color)s%(asctime)s\t%(name)s\t%(levelname)s\t%(message)s'
    ) if colorize else logging.Formatter(
        '%(asctime)s\t%(name)s\t%(levelname)s\t%(message)s'
    )
    handler.setFormatter(formatter)
    root_logger = logging.getLogger("")
    root_logger.addHandler(handler)
    root_logger.setLevel(
        logging.CRITICAL if quiet and fail_silently else
        logging.ERROR if quiet else
        logging.DEBUG if verbose else
        logging.INFO
    ) 
開發者ID:thanethomson,項目名稱:statik,代碼行數:18,代碼來源:cmdline.py

示例5: setup_logging

# 需要導入模塊: import colorlog [as 別名]
# 或者: from colorlog import StreamHandler [as 別名]
def setup_logging(log_level_str: str = 'info') -> None:
    EVENT_LOG_LEVEL = 25
    logging.addLevelName(EVENT_LOG_LEVEL, 'EVENT')

    def event(self, message, *args, **kwargs):
        if self.isEnabledFor(EVENT_LOG_LEVEL):
            self._log(EVENT_LOG_LEVEL, message, args, **kwargs)
    # we're adding a new function to Logger so ignore the type here to make mypy happy
    logging.Logger.event = event  # type: ignore

    handler = colorlog.StreamHandler()
    handler.setFormatter(colorlog.ColoredFormatter('%(log_color)s%(levelname)s:%(name)s:%(message)s'))
    logger = colorlog.getLogger()
    logger.addHandler(handler)

    log_level = getattr(logging, log_level_str.upper())
    logging.getLogger().setLevel(log_level)
    logging.getLogger('botocore').setLevel(max(logging.INFO, log_level))
    logging.getLogger('boto3').setLevel(max(logging.INFO, log_level)) 
開發者ID:Yelp,項目名稱:clusterman,代碼行數:21,代碼來源:util.py

示例6: log

# 需要導入模塊: import colorlog [as 別名]
# 或者: from colorlog import StreamHandler [as 別名]
def log(loglevel, log_name):
    if os.path.isdir(log_path) is not True:
        os.mkdir(log_path, 0o755)

    logfile = os.path.join(log_path, log_name + '.log')

    handler = colorlog.StreamHandler()
    handler.setFormatter(
        colorlog.ColoredFormatter(
            fmt='%(log_color)s[%(levelname)s] [%(threadName)s] [%(asctime)s] [%(filename)s:%(lineno)d] %(message)s',
            datefmt="%H:%M:%S",
            log_colors={
                'DEBUG': 'cyan',
                'INFO': 'green',
                'WARNING': 'yellow',
                'ERROR': 'red',
                'CRITICAL': 'red,bg_white',
            },
        )
    )
    f = open(logfile, 'a+')
    handler2 = logging.StreamHandler(f)
    formatter = logging.Formatter(
        "[%(levelname)s] [%(threadName)s] [%(asctime)s] [%(filename)s:%(lineno)d] %(message)s")
    handler2.setFormatter(formatter)
    logger.addHandler(handler2)
    logger.addHandler(handler)

    logger.setLevel(logging.INFO) 
開發者ID:LoRexxar,項目名稱:Cobra-W,代碼行數:31,代碼來源:log.py

示例7: __init__

# 需要導入模塊: import colorlog [as 別名]
# 或者: from colorlog import StreamHandler [as 別名]
def __init__(self):

        self.logger = logging.getLogger('plumbery')
        self.logger.propagate = 0

        # logging to console
        #
        handler = colorlog.StreamHandler()
        formatter = colorlog.ColoredFormatter(
            "%(asctime)-2s %(log_color)s%(message)s",
            datefmt='%H:%M:%S',
            reset=True,
            log_colors={
                'DEBUG':    'cyan',
                'INFO':     'green',
                'WARNING':  'yellow',
                'ERROR':    'red',
                'CRITICAL': 'red,bg_white',
            },
            secondary_log_colors={},
            style='%'
        )
        handler.setFormatter(formatter)

        self.logger.addHandler(handler)

        logging.getLogger('paramiko.transport').addHandler(handler)
        logging.getLogger('paramiko.transport').propagate = 0

        self.reset() 
開發者ID:DimensionDataCBUSydney,項目名稱:plumbery,代碼行數:32,代碼來源:plogging.py

示例8: test_example

# 需要導入模塊: import colorlog [as 別名]
# 或者: from colorlog import StreamHandler [as 別名]
def test_example():
    """Tests the usage example from the README"""
    import colorlog

    handler = colorlog.StreamHandler()
    handler.setFormatter(colorlog.ColoredFormatter(
        '%(log_color)s%(levelname)s:%(name)s:%(message)s'))
    logger = colorlog.getLogger('example')
    logger.addHandler(handler) 
開發者ID:aeroxis,項目名稱:sultan,代碼行數:11,代碼來源:test_example.py

示例9: __init__

# 需要導入模塊: import colorlog [as 別名]
# 或者: from colorlog import StreamHandler [as 別名]
def __init__(self, path: str, recursive=False, worker_count=cpu_count() * 2, verbose=0) -> None:
        """
        Create an OCyara object that can scan the specified directory or file and store the results.

        Arguments:
            path -- File or directory to be processed

        Keyword Arguments:
            recursive -- Whether the specified path should be recursivly searched for images (default False)
            worker_count -- The number of worker processes that should be spawned when
                            run() is executed (default available CPU cores * 2)
            verbose -- An int() from 0-2 that sets the verbosity level.
                       0 is default, 1 is information and 2 is debug
        """
        self.path = path
        self.recursive = recursive
        self.q = JoinableQueue()
        self.results = {}
        self.threads = worker_count
        self.lock = Lock()
        self.workers = []
        if os.path.isdir(self.path):
            if self.path[-1] is '/':
                self.path += '*'
            else:
                self.path += '/*'
        self.manager = Manager()
        self.matchedfiles = None
        self.total_items_to_queue = None
        self.total_added_to_queue = None
        self.queue_items_completed = None
        self.tempdir = tempfile.TemporaryDirectory()
        handler = colorlog.StreamHandler()
        handler.setFormatter(colorlog.ColoredFormatter(
                '%(log_color)s%(levelname)s:%(name)s:%(message)s'))
        self.logger = colorlog.getLogger('OCyara')
        self.logger.addHandler(handler)
        if verbose == 1:
            self.logger.setLevel('INFO')
        elif verbose > 1:
            self.logger.setLevel('DEBUG')
        else:
            self.logger.setLevel('WARN') 
開發者ID:bandrel,項目名稱:OCyara,代碼行數:45,代碼來源:ocyara.py

示例10: _process_image

# 需要導入模塊: import colorlog [as 別名]
# 或者: from colorlog import StreamHandler [as 別名]
def _process_image(self, yara_rule: str, save_context: bool) -> None:
        """
        Perform OCR and yara rule matching as a worker.

        process_image() is used by the run() method to create multiple worker processes for
        parallel execution.  process_image normally will not be called directly.

        Arguments:
            yara_rule -- File path pointing to a Yara rule file
        """
        context = None
        handler = colorlog.StreamHandler()
        handler.setFormatter(colorlog.ColoredFormatter(
                '%(log_color)s%(levelname)s:%(name)s:%(message)s'))
        # Creates a logger object for the individual workers that contains the PID as part of the message header
        worker_logger = colorlog.getLogger('worker_'+str(os.getpid()))
        worker_logger.addHandler(handler)
        worker_logger.setLevel(self.logger.level)
        worker_logger.info('PID {0} created to process queue'.format(str(os.getpid())))
        while True:
            try:
                image, filepath = self.q.get(timeout=.25)
            except Empty:
                if self.total_added_to_queue[0] == self.total_items_to_queue[0]:
                    worker_logger.debug('Queue Empty PID %d exiting' % os.getpid())
                    return
                else:
                    worker_logger.debug('Queue still loading')
                    continue
            ocrtext = tesserocr.image_to_text(image)
            try:
                rules = yara.compile(yara_rule)
            except yara.Error:
                worker_logger.error('Invalid rule file/rule file not found: {0}'.format(yara_rule))
            matches = rules.match(data=ocrtext)
            # If there is a match, store the filename and the rule in a dictionary local to this process.
            # Create an editable copy of the master manager dict.
            with self.lock:
                local_results_dict = self.matchedfiles[0]
                if matches:
                    if save_context:
                        context = ocrtext
                    for x in matches:
                        try:
                            local_results_dict[filepath].append((x.rule, context))
                        except KeyError:
                            local_results_dict[filepath] = [(x.rule, context)]
                    self.matchedfiles[0] = local_results_dict
            self.queue_items_completed[0] += 1
            self.q.task_done() 
開發者ID:bandrel,項目名稱:OCyara,代碼行數:52,代碼來源:ocyara.py

示例11: initialize_logging

# 需要導入模塊: import colorlog [as 別名]
# 或者: from colorlog import StreamHandler [as 別名]
def initialize_logging(prefix: str, logging_config: Dict, root_path: Path):
    log_path = path_from_root(
        root_path, logging_config.get("log_filename", "log/debug.log")
    )
    mkdir(str(log_path.parent))
    if logging_config["log_stdout"]:
        handler = colorlog.StreamHandler()
        handler.setFormatter(
            colorlog.ColoredFormatter(
                f"{prefix}: %(log_color)s%(levelname)-8s%(reset)s %(asctime)s.%(msecs)03d %(message)s",
                datefmt="%H:%M:%S",
                reset=True,
            )
        )

        logger = colorlog.getLogger()
        logger.addHandler(handler)
    else:
        logging.basicConfig(
            filename=log_path,
            filemode="a",
            format=f"{prefix}: %(levelname)-8s %(asctime)s.%(msecs)03d %(message)s",
            datefmt="%H:%M:%S",
        )

        logger = logging.getLogger()
        handler = RotatingFileHandler(log_path, maxBytes=20000000, backupCount=7)
        logger.addHandler(handler)

    if "log_level" in logging_config:
        if logging_config["log_level"] == "CRITICAL":
            logger.setLevel(logging.CRITICAL)
        elif logging_config["log_level"] == "ERROR":
            logger.setLevel(logging.ERROR)
        elif logging_config["log_level"] == "WARNING":
            logger.setLevel(logging.WARNING)
        elif logging_config["log_level"] == "INFO":
            logger.setLevel(logging.INFO)
        elif logging_config["log_level"] == "DEBUG":
            logger.setLevel(logging.DEBUG)
        else:
            logger.setLevel(logging.INFO)
    else:
        logger.setLevel(logging.INFO) 
開發者ID:Chia-Network,項目名稱:chia-blockchain,代碼行數:46,代碼來源:logging.py

示例12: pytest_configure

# 需要導入模塊: import colorlog [as 別名]
# 或者: from colorlog import StreamHandler [as 別名]
def pytest_configure(config):
    if config.option.logging_level is None:
        logging_level = logging.INFO
    else:
        logging_level = config.option.logging_level
        if logging_level not in logging_level_map:
            raise PytestConfigurationError(
                '{} not a recognised logging level'.format(logging_level)
            )
        logging_level = logging_level_map[logging_level]

    sh = colorlog.StreamHandler()
    sh.setLevel(logging_level)
    formatter = colorlog.ColoredFormatter(
        "%(white)s%(name)s %(reset)s %(log_color)s%"
        "(levelname)-8s%(reset)s %(blue)s%(message)s",
        datefmt=None,
        reset=True,
        log_colors={
            'DEBUG': 'cyan',
            'INFO': 'green',
            'WARNING': 'yellow',
            'ERROR': 'red',
            'CRITICAL': 'red,bg_white',
        },
        secondary_log_colors={},
        style='%'
    )

    sh.setFormatter(formatter)
    root = logging.getLogger()
    # remove the default streamhandler
    handler = next(
        (
            handler for handler in root.handlers if
            isinstance(handler, logging.StreamHandler)
        ), None
    )
    if handler:
        index = root.handlers.index(handler)
        root.handlers.pop(index)
    # and add our fancy coloured one
    root.addHandler(sh)

    if config.option.file_logging is True:
        add_file_logging() 
開發者ID:noisyboiler,項目名稱:wampy,代碼行數:48,代碼來源:pytest_plugin.py

示例13: logger_setup

# 需要導入模塊: import colorlog [as 別名]
# 或者: from colorlog import StreamHandler [as 別名]
def logger_setup():
    # Formats for colorlog.LevelFormatter
    log_level_formats = {'DEBUG': '%(log_color)s%(msg)s (%(module)s:%(lineno)d)',
                         'INFO': '%(log_color)s%(msg)s',
                         'WARNING': '%(log_color)sWARNING: %(msg)s (%(module)s:%(lineno)d)',
                         'ERROR': '%(log_color)sERROR: %(msg)s (%(module)s:%(lineno)d)',
                         'CRITICAL': '%(log_color)sCRITICAL: %(msg)s (%(module)s:%(lineno)d)',}

    log_colors = {'DEBUG': 'blue', 'INFO': 'cyan', 'WARNING': 'bold_yellow',
                  'ERROR': 'red', 'CRITICAL': 'red,bg_white'}

    # Initialize logger that can be used
    user_name = getpass.getuser()
    logger = logging.getLogger('elektronn3log')
    # Only set up the logger if it hasn't already been initialised before:
    if not len(logger.handlers) > 0:
        logger.setLevel(logging.DEBUG)

        lfile_formatter = logging.Formatter(
            '[%(asctime)s] [%(levelname)s]\t%(message)s',
            datefmt='%Y-%m-%d %H:%M:%S')
        uu = uuid.uuid4()
        # Temporary log file path, to be changed later.
        lfile_path = os.path.abspath(f'/tmp/{user_name}_{uu}_elektronn3.log')
        lfile_level = logging.DEBUG
        lfile_handler = logging.FileHandler(lfile_path)
        lfile_handler.setLevel(lfile_level)
        lfile_handler.setFormatter(lfile_formatter)
        logger.addHandler(lfile_handler)

        lstream_handler = colorlog.StreamHandler(sys.stdout)
        lstream_handler.setFormatter(
            colorlog.LevelFormatter(fmt=log_level_formats,
                                    log_colors=log_colors))
        # set this to logging.DEBUG to enable output for logger.debug() calls
        lstream_level = logging.INFO
        lstream_handler.setLevel(lstream_level)
        logger.addHandler(lstream_handler)

        logger.propagate = False

        if False:  # Test log levels:
            logger.critical('== critical')
            logger.error('== error')
            logger.warning('== warn')
            logger.info('== info')
            logger.debug('== debug') 
開發者ID:ELEKTRONN,項目名稱:elektronn3,代碼行數:49,代碼來源:logger.py


注:本文中的colorlog.StreamHandler方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。