Beispiel #1
0
def test_counter():
    """Make sure the CountingHandler has learned how to count"""
    logger = logging.getLogger('test_counter')
    handler = CountingHandler(default_type='0')
    logger.addHandler(handler)
    logger.setLevel(1)
    logger = TypeAdapter(logger)

    logger.info('')
    logger.info('')
    logger.warning('', type='a')
    logger.error('', type='a')
    logger.debug('', type='b')
    logger.debug('', type='b')
    expected = {
        logging.DEBUG: {
            'b': 2
        },
        logging.INFO: {
            'general': 2
        },
        logging.WARNING: {
            'a': 1
        },
        logging.ERROR: {
            'a': 1
        }
    }
    assert handler.counts == expected
def test_style_type_adapter(logger, handler, args, kwargs, type_, default_type, extra):
    """Make sure that if you have both a TypeAdapter and a StyleAdapter the
    type you provide ends up in the right place, and that it doesn't interfere
    with keyword formatting."""
    def test(record):
        """Make sure the type attribute is as expected"""
        rectype = getattr(record, 'type', None)
        if rectype is None:
            assert False, "No type for record!"
        if type_ is None:
            assert rectype == default_type
        else:
            assert rectype == type_
        if extra is not None:
            for key, val in extra.items():
                assert getattr(record, key, None) == val

    logger.addHandler(handler)
    logger = TypeAdapter(logger, default_type=default_type)
    logger = StyleAdapter(logger, extra=extra)
    handler.set_test(test)
    fmt = ['{}']*len(args) + ['{' + name + '}' for name in kwargs]
    fmt = ' '.join(fmt)
    print('-' * 60)

    note('fmt={}'.format(fmt))
    if type_ is None:
        logger.info(fmt, *args, **kwargs)
        event('type is None')
    else:
        logger.info(fmt, *args, type=type_, **kwargs)
def test_type_adapter(logger, handler, args, type_, default_type, extra):
    """Make sure the TypeAdapter sets the correct type attr"""
    def test(record):
        """Make sure the type attribute is as expected"""
        rectype = getattr(record, 'type', None)
        if rectype is None:
            assert False, "No type for record!"
        if type_ is None:
            assert rectype == default_type
        else:
            assert rectype == type_
        if extra is not None:
            note(str(dir(record)))
            for key, val in extra.items():
                assert getattr(record, key, None) == val

    logger.addHandler(handler)
    logger = TypeAdapter(logger, default_type=default_type, extra=extra)
    handler.set_test(test)
    fmt = ['%s'] * len(args)
    fmt = ' '.join(fmt)

    note('fmt={}'.format(fmt))

    if type_ is None:
        logger.info(fmt, *args)
        event('type is None')
    else:
        logger.info(fmt, *args, type=type_)
Beispiel #4
0
import logging
from vermouth.log_helpers import (
    StyleAdapter,
    BipolarFormatter,
    CountingHandler,
    TypeAdapter,
)

# Implement Logger
LOGGER = TypeAdapter(logging.getLogger('polyply'))
PRETTY_FORMATTER = logging.Formatter(fmt='{levelname:} - {type} - {message}',
                                     style='{')
DETAILED_FORMATTER = logging.Formatter(
    fmt='{levelname:} - {type} - {name} - {message}', style='{')
COUNTER = CountingHandler()

# Control above what level message we want to count
COUNTER.setLevel(logging.WARNING)

CONSOLE_HANDLER = logging.StreamHandler()
FORMATTER = BipolarFormatter(DETAILED_FORMATTER,
                             PRETTY_FORMATTER,
                             logging.DEBUG,
                             logger=LOGGER)

CONSOLE_HANDLER.setFormatter(FORMATTER)
LOGGER.addHandler(CONSOLE_HANDLER)
LOGGER.addHandler(COUNTER)

LOGGER = StyleAdapter(LOGGER)