How to use the colorlog.ColoredFormatter function in colorlog

To help you get started, we’ve selected a few colorlog examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github borntyping / python-colorlog / colorlog / colorlog.tests.py View on Github external
def setUp(self):
		formatter = colorlog.ColoredFormatter(self.logformat)

		stream = logging.StreamHandler()
		stream.setLevel(logging.DEBUG)
		stream.setFormatter(formatter)

		self.logger = logging.getLogger('colorlog')
		self.logger.setLevel(logging.DEBUG)
		self.logger.addHandler(stream)
github hyperledger / grid-contrib / core / sawtooth / cli / stats_lib / utils.py View on Github external
logger.setLevel(loglevel)

    if 'LogFile' in config and config['LogFile'] != '__screen__':
        logfile = config['LogFile']
        if not os.path.isdir(os.path.dirname(logfile)):
            warnings.warn("Logging directory {0} does not exist".format(
                os.path.abspath(os.path.dirname(logfile))))
            sys.exit(-1)

        flog = logging.FileHandler(logfile)
        flog.setFormatter(logging.Formatter(
            '[%(asctime)s, %(levelno)d, %(module)s] %(message)s', "%H:%M:%S"))
        logger.addHandler(flog)
    else:
        clog = logging.StreamHandler()
        formatter = ColoredFormatter(
            "%(log_color)s[%(asctime)s %(levelname)-8s%(module)s]%(reset)s "
            "%(white)s%(message)s",
            datefmt="%H:%M:%S",
            reset=True,
            log_colors={
                'DEBUG': 'cyan',
                'INFO': 'green',
                'WARNING': 'yellow',
                'ERROR': 'red',
                'CRITICAL': 'red',
            })

        clog.setFormatter(formatter)
        clog.setLevel(loglevel)
        logger.addHandler(clog)
github thanethomson / statik / statik / cmdline.py View on Github external
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
    )
github hyperledger / sawtooth-core / sdk / examples / intkey_jvm_sc / sawtooth_intkey / cli / main.py View on Github external
def create_console_handler(verbose_level):
    clog = logging.StreamHandler()
    formatter = ColoredFormatter(
        "%(log_color)s[%(asctime)s %(levelname)-8s%(module)s]%(reset)s "
        "%(white)s%(message)s",
        datefmt="%H:%M:%S",
        reset=True,
        log_colors={
            'DEBUG': 'cyan',
            'INFO': 'green',
            'WARNING': 'yellow',
            'ERROR': 'red',
            'CRITICAL': 'red',
        })

    clog.setFormatter(formatter)

    if verbose_level == 0:
        clog.setLevel(logging.WARN)
github jiangsir404 / wam-monitor / log.py View on Github external
#!/usr/bin/env python        
#coding:utf-8

import os
import colorlog
import logging
from logging import handlers

log_path = 'logs'
if os.path.isdir(log_path) is not True:
    os.mkdir(log_path, 0o755)
logfile = os.path.join(log_path, 'wam.log')

handler = colorlog.StreamHandler()
formatter = colorlog.ColoredFormatter(
    '%(log_color)s%(asctime)s [%(name)s] [%(levelname)s] %(message)s%(reset)s',
    datefmt=None,
    reset=True,
    log_colors={
        'DEBUG': 'cyan',
        'INFO': 'green',
        'WARNING': 'yellow',
        'ERROR': 'red',
        'CRITICAL': 'red,bg_white'
    },
    secondary_log_colors={},
    style='%'
)
handler.setFormatter(formatter)

file_handler = handlers.RotatingFileHandler(logfile, maxBytes=(1048576 * 5), backupCount=7)
github nstack / stackhut / stackhut_toolkit / common / utils.py View on Github external
def setup_logging(verbose_mode):
    global VERBOSE
    global log
    VERBOSE = verbose_mode
    log.propagate = False
    log.setLevel(logging.DEBUG if verbose_mode else logging.INFO)

    logFormatter = ColoredFormatter(
        '%(blue)s%(asctime)s%(reset)s [%(log_color)s%(levelname)-5s%(reset)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='%'
    )

    # file output
    # fileHandler = logging.FileHandler(LOGFILE, mode='w')
github cjerdonek / open-rcv / openrcv / scripts / run.py View on Github external
def make_formatter():
    # Prefix log messages unobtrusively with "log" to distinguish log
    # messages more obviously from other text sent to the error stream.
    format_string = ("%(bg_black)s%(log_color)slog: %(display_name)s: "
                     "[%(levelname)s]%(reset)s %(message)s")
    colors = colorlog.default_log_colors
    colors['DEBUG'] = 'white'
    formatter = colorlog.ColoredFormatter(format_string, log_colors=colors)
    return formatter
github hyperledger / sawtooth-core / extensions / arcade / sawtooth_xo / xo_cli.py View on Github external
def create_console_handler(verbose_level):
    clog = logging.StreamHandler()
    formatter = ColoredFormatter(
        "%(log_color)s[%(asctime)s %(levelname)-8s%(module)s]%(reset)s "
        "%(white)s%(message)s",
        datefmt="%H:%M:%S",
        reset=True,
        log_colors={
            'DEBUG': 'cyan',
            'INFO': 'green',
            'WARNING': 'yellow',
            'ERROR': 'red',
            'CRITICAL': 'red',
        })

    clog.setFormatter(formatter)

    if verbose_level == 0:
        clog.setLevel(logging.WARN)
github zestedesavoir / zds-site / zds / settings / dev.py View on Github external
'formatters': {
        'verbose': {
            '()': ColoredFormatter,
            'format': '%(log_color)s %(levelname)s %(reset)s %(bold_black)s%(name)s%(reset)s %(message)s',
            'log_colors': {
                'DEBUG': 'fg_white,bg_black',
                'INFO': 'fg_black,bg_bold_white',
                'WARNING': 'fg_black,bg_bold_yellow',
                'ERROR': 'fg_bold_white,bg_bold_red',
                'CRITICAL': 'fg_bold_white,bg_bold_red',
            },
        },

        'django.server': {
            '()': ColoredFormatter,
            'format': '%(log_color)s%(message)s',
            'log_colors': {
                'INFO': 'bold_black',
                'WARNING': 'bold_yellow',
                'ERROR': 'bold_red',
                'CRITICAL': 'bold_red',
            },
        },
    },

    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose',
        },
github theacodes / nox / nox / logger.py View on Github external
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging

from colorlog import ColoredFormatter  # type: ignore

SUCCESS = 25
OUTPUT = logging.DEBUG - 1


class NoxFormatter(ColoredFormatter):
    def __init__(self, *args, **kwargs):
        super(NoxFormatter, self).__init__(*args, **kwargs)
        self._simple_fmt = logging.Formatter("%(message)s")

    def format(self, record):
        if record.levelname == "OUTPUT":
            return self._simple_fmt.format(record)
        return super(NoxFormatter, self).format(record)


class LoggerWithSuccessAndOutput(logging.getLoggerClass()):  # type: ignore
    def __init__(self, name, level=logging.NOTSET):
        super(LoggerWithSuccessAndOutput, self).__init__(name, level)
        logging.addLevelName(SUCCESS, "SUCCESS")
        logging.addLevelName(OUTPUT, "OUTPUT")