def test_valid_logging(self):
        test_logger = logging.getLogger('test_valid_logging')
        test_logger.setLevel(logging.WARNING)
        mock_handler = ScribeLogHandler(self.HOST, self.PORT)
        mock_handler.client = self.mock_connection
        mock_handler.setLevel(logging.DEBUG)
        test_logger.addHandler(mock_handler)

        self.assertIsNone(test_logger.debug('message'))
        self.assertIsNone(test_logger.warning('message'))
Ejemplo n.º 2
0
def init_scribe_logging():
    scribe_host = os.getenv('SCRIBE_HOST')
    scribe_port = os.getenv('SCRIBE_PORT', 1463)
    scribe_category = os.getenv('SCRIBE_CATEGORY', 'securitybot')

    if scribe_host:
        environment = os.getenv('ENVIRONMENT', 'default')
        formatter = logging.Formatter(
            '%(asctime)s ' + environment +
            ' %(name)s %(levelname)-8s %(message)s\n')
        scribe_handler = ScribeLogHandler(scribe_host,
                                          scribe_port,
                                          category=scribe_category)
        scribe_handler.setLevel(logging.DEBUG)
        scribe_handler.setFormatter(formatter)
        logging.getLogger().addHandler(scribe_handler)
Ejemplo n.º 3
0
def create_logger(conf, logger_name):
    log_format = getattr(conf, 'log_format', '') \
        or '%(asctime)s %(levelname)-5s %(message)s'
    console_log_level = getattr(conf, 'console_log_level', logging.ERROR) \
        or logging.ERROR
    log_level = getattr(conf, 'log_level', logging.INFO) or logging.INFO
    scribe_log_level = getattr(conf, 'scribe_log_level', log_level) \
        or log_level
    #if isinstance(scribe_log_level, basestring):
    if isinstance(scribe_log_level, str):
        scribe_log_level = eval(scribe_log_level)
    #if isinstance(console_log_level, basestring):
    if isinstance(console_log_level, str):
        console_log_level = eval(console_log_level)
    #if isinstance(log_level, basestring):
    if isinstance(log_level, str):
        log_level = eval(log_level)

    logger = logging.getLogger(logger_name)
    logger.setLevel(log_level)

    ch = logging.StreamHandler()
    ch.setFormatter(logging.Formatter(log_format))
    ch.setLevel(console_log_level)
    logger.addHandler(ch)

    log_file = conf.get('%s_file' % (logger_name))
    if log_file:
        fh = MultiProcessRotatingFileHandler(log_file, 'midnight')
        fh.setFormatter(logging.Formatter(log_format))
        fh.setLevel(log_level)
        logger.addHandler(fh)

    # 通过配置文本文件加载的conf, hasattr(conf, 'any_key')始终为真, 故使用getattr代替
    if getattr(conf, 'log_category', None):
        log_host = getattr(conf, 'log_host', None) or '127.0.0.1'
        log_port = int(getattr(conf, 'log_port', None) or 1464)
        scribe = ScribeLogHandler(
            category=conf.log_category, host=log_host, port=log_port)
        scribe.setLevel(scribe_log_level)
        scribe.setFormatter(logging.Formatter(log_format))
        logger.addHandler(scribe)

    # 将warning打印到日志便于分析问题
    if getattr(conf, 'log_capture_warning', None) in ['True', '1', True, 1]:
        logging.captureWarnings(True)
    return logger
    def test_invalid_logging(self):
        """
        Not passing valid extra parameter to
        """
        test_logger = logging.getLogger('test_invalid_logging')
        test_logger.setLevel(logging.WARNING)
        formatter = logging.Formatter('%(bad_arg)s - %(levelname)s - %(message)s')
        mock_handler = ScribeLogHandler(self.HOST, self.PORT)
        mock_handler.client = self.mock_connection
        mock_handler.setLevel(logging.DEBUG)
        mock_handler.setFormatter(formatter)
        test_logger.addHandler(mock_handler)

        self.assertIsNone(test_logger.debug('message'))
        self.assertIsNone(test_logger.warning('message', extra={'bad_arg': 'fail'}))

        with self.assertRaises(KeyError):
            test_logger.warning('message')

        with self.assertRaises(KeyError):
            test_logger.warning('message', extra={'k': 'v'})
Ejemplo n.º 5
0
from scribe_logger.logger import ScribeLogHandler
import logging

my_logger = logging.getLogger('MyLogger')
my_logger.setLevel(logging.DEBUG)

scribe = ScribeLogHandler(category="test_category")
scribe.setLevel(logging.DEBUG)
my_logger.addHandler(scribe)

my_logger.info("This is a test message")