<commit_before>
import os
import sys
import socket
import logging
import logging.handlers

from alerta.common import config

CONF = config.CONF

_DEFAULT_LOG_FORMAT = "%(asctime)s.%(msecs).03d %(name)s[%(process)d] %(threadName)s %(levelname)s - %(message)s"
_DEFAULT_LOG_DATE_FORMAT = "%Y-%m-%d %H:%M:%S"

# TODO(nsatterl): log exceptions (checkout how OpenStack do it)


def setup(name):
    """Setup logging."""

    log_root = getLogger(name)

    if CONF.use_syslog is True:
        facility = CONF.syslog_facility
        syslog = logging.handlers.SysLogHandler(address='/dev/log', facility=facility)
        # syslog = logging.handlers.SysLogHandler(address=('localhost', 514), facility=facility, socktype=socket.SOCK_STREAM)
        log_root.addHandler(syslog)

    logpath = _get_log_file_path()
    if logpath:
        try:
            filelog = logging.handlers.WatchedFileHandler(logpath)
        except IOError:
            raise
        log_root.addHandler(filelog)

        # TODO(nsatterl): test mode like openstack??

    if CONF.use_stderr:
        streamlog = ColorHandler()
        log_root.addHandler(streamlog)

    for handler in log_root.handlers:
        log_format = _DEFAULT_LOG_FORMAT
        date_format = _DEFAULT_LOG_DATE_FORMAT
        handler.setFormatter(logging.Formatter(fmt=log_format, datefmt=date_format))

    if CONF.debug:
        log_root.setLevel(logging.DEBUG)
    elif CONF.verbose:
        log_root.setLevel(logging.INFO)
    else:
        log_root.setLevel(logging.WARNING)


def getLogger(name=None):

    if name:
        return logging.getLogger(name)
    else:
        return logging.root


def _get_prog_name():
    return os.path.basename(sys.argv[0])


def _get_log_file_path():
    logfile = CONF.log_file
    logdir = CONF.log_dir

    if logfile and not logdir:
        return logfile

    if logfile and logdir:
        return os.path.join(logdir, logfile)

    if logdir:
        prog = _get_prog_name()
        return '%s.log' % (os.path.join(logdir, prog))


class ColorHandler(logging.StreamHandler):

    # XXX - OpenStack colours for reference
    #
    # LEVEL_COLORS = {
    #     logging.NOTSET: '',
    #     logging.DEBUG: '\033[00;32m',  # GREEN
    #     logging.INFO: '\033[00;36m',  # CYAN
    #     #logging.AUDIT: '\033[01;36m',  # BOLD CYAN
    #     logging.WARN: '\033[01;33m',  # BOLD YELLOW
    #     logging.ERROR: '\033[01;31m',  # BOLD RED
    #     logging.CRITICAL: '\033[01;31m',  # BOLD RED
    # }

    LEVEL_COLORS = {
        logging.NOTSET: '',
        logging.DEBUG: '\033[90m',     # BLACK
        logging.INFO: '\033[92m',      # GREEN
        logging.WARN: '\033[96m',      # CYAN
        logging.ERROR: '\033[93m',     # YELLOW
        logging.CRITICAL: '\033[91m',  # RED
    }

    def format(self, record):
        record.color = self.LEVEL_COLORS[record.levelno]
        color_fmt = logging.Formatter("%(color)s" + _DEFAULT_LOG_FORMAT + "\033[0m")
        self.setFormatter(color_fmt)
        return logging.StreamHandler.format(self, record)


<commit_msg>assign color formatter to handler<commit_after>
import os
import sys
import socket
import logging
import logging.handlers

from alerta.common import config

CONF = config.CONF

_DEFAULT_LOG_FORMAT = "%(asctime)s.%(msecs).03d %(name)s[%(process)d] %(threadName)s %(levelname)s - %(message)s"
_DEFAULT_LOG_DATE_FORMAT = "%Y-%m-%d %H:%M:%S"

# TODO(nsatterl): log exceptions (checkout how OpenStack do it)


def setup(name):
    """Setup logging."""

    log_root = getLogger(name)

    if CONF.use_syslog is True:
        facility = CONF.syslog_facility
        syslog = logging.handlers.SysLogHandler(address='/dev/log', facility=facility)
        # syslog = logging.handlers.SysLogHandler(address=('localhost', 514), facility=facility, socktype=socket.SOCK_STREAM)
        log_root.addHandler(syslog)

    logpath = _get_log_file_path()
    if logpath:
        try:
            filelog = logging.handlers.WatchedFileHandler(logpath)
        except IOError:
            raise
        log_root.addHandler(filelog)

        # TODO(nsatterl): test mode like openstack??

    for handler in log_root.handlers:
        log_format = _DEFAULT_LOG_FORMAT
        date_format = _DEFAULT_LOG_DATE_FORMAT
        handler.setFormatter(logging.Formatter(fmt=log_format, datefmt=date_format))

    if CONF.use_stderr:
        streamlog = ColorHandler()
        color_fmt = logging.Formatter("%(color)s" + _DEFAULT_LOG_FORMAT + "\033[0m")
        streamlog.setFormatter(color_fmt)
        log_root.addHandler(streamlog)

    if CONF.debug:
        log_root.setLevel(logging.DEBUG)
    elif CONF.verbose:
        log_root.setLevel(logging.INFO)
    else:
        log_root.setLevel(logging.WARNING)


def getLogger(name=None):

    if name:
        return logging.getLogger(name)
    else:
        return logging.root


def _get_prog_name():
    return os.path.basename(sys.argv[0])


def _get_log_file_path():
    logfile = CONF.log_file
    logdir = CONF.log_dir

    if logfile and not logdir:
        return logfile

    if logfile and logdir:
        return os.path.join(logdir, logfile)

    if logdir:
        prog = _get_prog_name()
        return '%s.log' % (os.path.join(logdir, prog))


class ColorHandler(logging.StreamHandler):

    # XXX - OpenStack colours for reference
    #
    # LEVEL_COLORS = {
    #     logging.NOTSET: '',
    #     logging.DEBUG: '\033[00;32m',  # GREEN
    #     logging.INFO: '\033[00;36m',  # CYAN
    #     #logging.AUDIT: '\033[01;36m',  # BOLD CYAN
    #     logging.WARN: '\033[01;33m',  # BOLD YELLOW
    #     logging.ERROR: '\033[01;31m',  # BOLD RED
    #     logging.CRITICAL: '\033[01;31m',  # BOLD RED
    # }

    LEVEL_COLORS = {
        logging.NOTSET: '',
        logging.DEBUG: '\033[90m',     # BLACK
        logging.INFO: '\033[92m',      # GREEN
        logging.WARN: '\033[96m',      # CYAN
        logging.ERROR: '\033[93m',     # YELLOW
        logging.CRITICAL: '\033[91m',  # RED
    }

    def format(self, record):
        record.color = self.LEVEL_COLORS[record.levelno]
        return logging.StreamHandler.format(self, record)



{'fraction_non_alphanumeric': 0.12115290669272105, 'fraction_numerical': 0.025077348965966455, 'mean_word_length': 2.720169594185342, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3352010', 'n_tokens_mistral': 2223, 'n_tokens_neox': 2057, 'n_words': 396}