File: logger.py

package info (click to toggle)
borgbackup 1.0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,572 kB
  • ctags: 5,885
  • sloc: python: 11,127; ansic: 628; makefile: 129; sh: 70
file content (54 lines) | stat: -rw-r--r-- 1,575 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import logging
from io import StringIO

import pytest

from ..logger import find_parent_module, create_logger, setup_logging
logger = create_logger()


@pytest.fixture()
def io_logger():
    io = StringIO()
    handler = setup_logging(stream=io, env_var=None)
    handler.setFormatter(logging.Formatter('%(name)s: %(message)s'))
    logger.setLevel(logging.DEBUG)
    return io


def test_setup_logging(io_logger):
    logger.info('hello world')
    assert io_logger.getvalue() == "borg.testsuite.logger: hello world\n"


def test_multiple_loggers(io_logger):
    logger = logging.getLogger(__name__)
    logger.info('hello world 1')
    assert io_logger.getvalue() == "borg.testsuite.logger: hello world 1\n"
    logger = logging.getLogger('borg.testsuite.logger')
    logger.info('hello world 2')
    assert io_logger.getvalue() == "borg.testsuite.logger: hello world 1\nborg.testsuite.logger: hello world 2\n"
    io_logger.truncate(0)
    io_logger.seek(0)
    logger = logging.getLogger('borg.testsuite.logger')
    logger.info('hello world 2')
    assert io_logger.getvalue() == "borg.testsuite.logger: hello world 2\n"


def test_parent_module():
    assert find_parent_module() == __name__


def test_lazy_logger():
    # just calling all the methods of the proxy
    logger.setLevel(logging.DEBUG)
    logger.debug("debug")
    logger.info("info")
    logger.warning("warning")
    logger.error("error")
    logger.critical("critical")
    logger.log(logging.INFO, "info")
    try:
        raise Exception
    except Exception:
        logger.exception("exception")