<commit_before>
import os
import sys
import logging
import logging.handlers
import cStringIO
import traceback

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"

log_opts = {
    'syslog_facility': 'local7',
}


def _create_logging_excepthook(name):
    def logging_excepthook(type, value, tb):

        stringbuffer = cStringIO.StringIO()
        traceback.print_exception(type, value, tb,
                                  None, stringbuffer)
        lines = stringbuffer.getvalue()
        stringbuffer.close()

        getLogger(name).critical(lines)
    return logging_excepthook


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

    config.register_opts(log_opts)

    sys.excepthook = _create_logging_excepthook(name)

    log_root = getLogger(name)

    if CONF.use_syslog:
        facility = CONF.syslog_facility
        try:
            syslog = logging.handlers.SysLogHandler(address='/dev/log', facility=facility)
        except IOError:
            pass
        else:
            log_root.addHandler(syslog)

    logpath = _get_log_file_path()
    if logpath:
        try:
            filelog = logging.handlers.WatchedFileHandler(logpath, encoding='utf-8')
        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)<commit_msg>bugfix: dont fail silently if logging not configured<commit_after>
import os
import sys
import logging
import logging.handlers
import cStringIO
import traceback

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"

log_opts = {
    'syslog_facility': 'local7',
}


def _create_logging_excepthook(name):
    def logging_excepthook(type, value, tb):

        stringbuffer = cStringIO.StringIO()
        traceback.print_exception(type, value, tb,
                                  None, stringbuffer)
        lines = stringbuffer.getvalue()
        stringbuffer.close()

        getLogger(name).critical(lines)
    return logging_excepthook


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

    config.register_opts(log_opts)

    sys.excepthook = _create_logging_excepthook(name)

    log_root = getLogger(name)

    if CONF.use_syslog:
        if sys.platform == "darwin":
            socket = '/var/run/syslog'
        else:
            socket = '/dev/log'
        facility = CONF.syslog_facility

        try:
            syslog = logging.handlers.SysLogHandler(address=socket, facility=facility)
        except IOError, e:
            print >>sys.stderr, 'ERROR - Failed to log to syslog socket %s: %s' % (socket, e)
        else:
            log_root.addHandler(syslog)

    logpath = _get_log_file_path()
    if logpath:
        try:
            filelog = logging.handlers.WatchedFileHandler(logpath, encoding='utf-8')
        except IOError, e:
            print >>sys.stderr, 'ERROR - Failed to log to logfile %s: %s' % (logpath, e)
        else:
            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)
{'fraction_non_alphanumeric': 0.10490856592877768, 'fraction_numerical': 0.0038498556304138597, 'mean_word_length': 2.849074074074074, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7618099', 'n_tokens_mistral': 1350, 'n_tokens_neox': 1309, 'n_words': 258}