Beispiel #1
0
def test_tagged(default_handler):
    from logbook.more import TaggingLogger, TaggingHandler
    stream = StringIO()
    second_handler = logbook.StreamHandler(stream)

    logger = TaggingLogger('name', ['cmd'])
    handler = TaggingHandler(dict(
        info=default_handler,
        cmd=second_handler,
        both=[default_handler, second_handler],
    ))
    handler.bubble = False

    with handler:
        with capturing_stderr_context() as captured:
            logger.log('info', 'info message')
            logger.log('both', 'all message')
            logger.cmd('cmd message')

    stderr = captured.getvalue()

    assert 'info message' in stderr
    assert 'all message' in stderr
    assert 'cmd message' not in stderr

    stringio = stream.getvalue()

    assert 'info message' not in stringio
    assert 'all message' in stringio
    assert 'cmd message' in stringio
def test_redirect_logbook():
    import logging
    out = StringIO()
    logger = logging.getLogger()
    logbook_logger = logbook.Logger('testlogger')
    old_handlers = logger.handlers[:]
    handler = logging.StreamHandler(out)
    handler.setFormatter(logging.Formatter(
        '%(name)s:%(levelname)s:%(message)s'))
    logger.handlers[:] = [handler]
    try:
        with logbook.compat.LoggingHandler():
            logbook_logger.warn("This goes to logging")
            pieces = out.getvalue().strip().split(':')
            assert pieces == ['testlogger', 'WARNING', 'This goes to logging']
    finally:
        logger.handlers[:] = old_handlers
Beispiel #3
0
def test_redirect_logbook():
    import logging
    out = StringIO()
    logger = logging.getLogger()
    logbook_logger = logbook.Logger('testlogger')
    old_handlers = logger.handlers[:]
    handler = logging.StreamHandler(out)
    handler.setFormatter(logging.Formatter(
        '%(name)s:%(levelname)s:%(message)s'))
    logger.handlers[:] = [handler]
    try:
        with logbook.compat.LoggingHandler():
            logbook_logger.warn("This goes to logging")
            pieces = out.getvalue().strip().split(':')
            assert pieces == ['testlogger', 'WARNING', 'This goes to logging']
    finally:
        logger.handlers[:] = old_handlers