Exemplo n.º 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
Exemplo n.º 2
0
    def test_tagged(self):
        from logbook.more import TaggingLogger, TaggingHandler
        stream = StringIO()
        second_handler = logbook.StreamHandler(stream)

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

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

        stderr = captured.getvalue()

        self.assert_('info message' in stderr)
        self.assert_('all message' in stderr)
        self.assert_('cmd message' not in stderr)

        stringio = stream.getvalue()

        self.assert_('info message' not in stringio)
        self.assert_('all message' in stringio)
        self.assert_('cmd message' in stringio)
Exemplo n.º 3
0
def test_tagging_logger(default_handler):
    from logbook import StderrHandler
    from logbook.more import TaggingLogger

    logger = TaggingLogger('tagged', ['a', 'b'])
    handler = StderrHandler(format_string="{record.msg}|{record.extra[tags]}")

    with handler:
        with capturing_stderr_context() as captured:
            logger.a("a")
            logger.b("b")

    stderr = captured.getvalue()

    assert "a|['a']" in stderr
    assert "a|['b']" not in stderr
    assert "b|['b']" in stderr
    assert "b|['a']" not in stderr
Exemplo n.º 4
0
def initialize(eventlog_file=None):
    """
    Initialize the analytics output. This will cause analytics events to be output to either a file or stdout.

    If this function is not called, analytics events will not be output. If it is called with a filename, the events
    will be output to that file. If it is called with 'STDOUT' or None, the events will be output to stdout.

    :param eventlog_file: The filename to output events to, 'STDOUT' to output to stdout, None to disable event logging
    :type eventlog_file: str | None
    """
    global _analytics_logger, _eventlog_file

    _eventlog_file = eventlog_file
    if not eventlog_file:
        _analytics_logger = None
        return

    if eventlog_file.upper() == 'STDOUT':
        event_handler = StreamHandler(sys.stdout)
    else:
        fs.create_dir(os.path.dirname(eventlog_file))
        previous_log_file_exists = os.path.exists(eventlog_file)

        event_handler = RotatingFileHandler(
            filename=eventlog_file,
            max_size=Configuration['max_eventlog_file_size'],
            backup_count=Configuration['max_eventlog_file_backups'],
        )
        if previous_log_file_exists:
            event_handler.perform_rollover(
            )  # force starting a new eventlog file on application startup

    event_handler.format_string = '{record.message}'  # only output raw log message -- no timestamp or log level
    handler = TaggingHandler(
        {'event': event_handler
         },  # enable logging to the event_handler with the event() method
        bubble=True,
    )
    handler.push_application()

    _analytics_logger = TaggingLogger('analytics', ['event'])
Exemplo n.º 5
0
def test_tagging_logger(default_handler):
    from logbook import StderrHandler
    from logbook.more import TaggingLogger

    logger = TaggingLogger('tagged', ['a', 'b'])
    handler = StderrHandler(format_string="{record.msg}|{record.extra[tags]}")

    with handler:
        with capturing_stderr_context() as captured:
            logger.a("a")
            logger.b("b")

    stderr = captured.getvalue()

    assert "a|['a']" in stderr
    assert "a|['b']" not in stderr
    assert "b|['b']" in stderr
    assert "b|['a']" not in stderr
Exemplo n.º 6
0
    def test_tagged(self):
        from logbook.more import TaggingLogger, TaggingHandler
        stream = StringIO()
        second_handler = logbook.StreamHandler(stream)

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

        handler.push_thread()
        try:
            captured = capture_stderr.start()
            try:
                logger.log('info', 'info message')
                logger.log('both', 'all message')
                logger.cmd('cmd message')
            finally:
                capture_stderr.end()
        finally:
            handler.pop_thread()

        stderr = captured.getvalue()

        self.assert_('info message' in stderr)
        self.assert_('all message' in stderr)
        self.assert_('cmd message' not in stderr)

        stringio = stream.getvalue()

        self.assert_('info message' not in stringio)
        self.assert_('all message' in stringio)
        self.assert_('cmd message' in stringio)
Exemplo n.º 7
0
 def _initialize_analytics_logger(self):
     """
     Initializes the analytics
     """
     return TaggingLogger('analytics', ['event'])