Beispiel #1
0
    def test_get_exec_info(self):
        template = '{source} {line} {function} {process}: {message}'
        logger = Logger(name='exec-info', template=template)

        with CaptureOutput() as co:
            logger.info('hello')
        output = co.get_text()

        regex = '[\w/]+\.py \d+ \w+ \d+: [^\n]+'
        assert re.match(regex, output)
Beispiel #2
0
    def test_additional_context_static(self):
        template = '{name} {person}: {message}'
        logger = Logger(name='ad-static',
                        template=template,
                        additional_context={'person': 'vince'})

        with CaptureOutput() as co:
            logger.info('hello')
        output = co.get_text()

        assert output == 'ad-static vince: hello'
Beispiel #3
0
    def test_additional_context_static(self):
        template = "{name} {person}: {message}"
        logger = Logger(name="ad-static",
                        template=template,
                        additional_context={"person": "vince"})

        with CaptureOutput() as co:
            logger.info("hello")
        output = co.get_text()

        assert output == "ad-static vince: hello"
Beispiel #4
0
    def test_additional_context_function(self):
        template = '{uuid} {message}'
        logger = Logger(name='ad-func',
                        template=template,
                        additional_context={'uuid': uuid4})

        with CaptureOutput() as co:
            logger.info('hello')
        output = co.get_text()

        assert re.match(
            '[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12} hello',
            output)
Beispiel #5
0
    def test_additional_context_function(self):
        template = "{uuid} {message}"
        logger = Logger(name="ad-func",
                        template=template,
                        additional_context={"uuid": uuid4})

        with CaptureOutput() as co:
            logger.info("hello")
        output = co.get_text()

        assert re.match(
            "[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12} hello",
            output,
        )
Beispiel #6
0
    def test_init_already_registered(self):
        logger = Logger(name='test')

        assert logger.name == self.logger.name
        assert logger.handlers == self.logger.handlers
        assert logger.template == self.logger.template
        assert logger.keys == self.logger.keys
Beispiel #7
0
    def test_set_template(self):
        logger = Logger(name='template')
        assert logger.template == Logger.DEFAULT_TEMPLATE

        template = '{timestamp} {message}'
        keys = {'timestamp', 'message'}
        logger.template = template

        assert logger.template == template
        assert logger.keys == keys

        with CaptureOutput() as co:
            logger.info('Hello, world!')
        output = co.get_text()

        regex = f'{_timestamp_group} {_message_group}'
        assert re.match(regex, output)
Beispiel #8
0
    def test_set_template(self):
        logger = Logger(name="template")
        assert logger.template == Logger.DEFAULT_TEMPLATE

        template = "{timestamp} {message}"
        keys = {"timestamp", "message"}
        logger.template = template

        assert logger.template == template
        assert logger.keys == keys

        with CaptureOutput() as co:
            logger.info("Hello, world!")
        output = co.get_text()

        regex = f"{_timestamp_group} {_message_group}"
        assert re.match(regex, output)
Beispiel #9
0
 def test_init_with_multiple_handlers(self):
     handler0 = StdErrHandler(name='test-stderr')
     handler1 = StdOutHandler(name='test-stdout')
     logger = Logger(name='test-handlers', handlers=[handler0, handler1])
     assert logger.handlers == [handler0, handler1]
Beispiel #10
0
    def test_init_with_one_handler(self):
        handler = StdErrHandler(name='test-stderr')
        logger = Logger(name='test-handler', handler=handler)

        assert logger.handlers == [handler]
Beispiel #11
0
 def setup_class(cls):
     cls.logger = Logger(name='test', level=LogLevel.debug)
Beispiel #12
0
 def test_init_no_keys(self):
     with pytest.raises(ValueError):
         template = 'ohai'
         Logger(name='no-keys', template=template)
Beispiel #13
0
 def test_init_no_keys(self):
     with pytest.raises(ValueError):
         template = "ohai"
         Logger(name="no-keys", template=template)