test_logging.py 4.32 KB
Newer Older
1
# Copyright (c) OpenMMLab. All rights reserved.
Kai Chen's avatar
Kai Chen committed
2
import logging
3
import os
Jintao Lin's avatar
Jintao Lin committed
4
import platform
Kai Chen's avatar
Kai Chen committed
5
6
7
8
9
10
11
import tempfile
from unittest.mock import patch

import pytest

from mmcv import get_logger, print_log

Jintao Lin's avatar
Jintao Lin committed
12
13
14
15
16
if platform.system() == 'Windows':
    import regex as re
else:
    import re

Kai Chen's avatar
Kai Chen committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

@patch('torch.distributed.get_rank', lambda: 0)
@patch('torch.distributed.is_initialized', lambda: True)
@patch('torch.distributed.is_available', lambda: True)
def test_get_logger_rank0():
    logger = get_logger('rank0.pkg1')
    assert isinstance(logger, logging.Logger)
    assert len(logger.handlers) == 1
    assert isinstance(logger.handlers[0], logging.StreamHandler)
    assert logger.handlers[0].level == logging.INFO

    logger = get_logger('rank0.pkg2', log_level=logging.DEBUG)
    assert isinstance(logger, logging.Logger)
    assert len(logger.handlers) == 1
    assert logger.handlers[0].level == logging.DEBUG

33
34
35
36
    # the name can not be used to open the file a second time in windows,
    # so `delete` should be set as `False` and we need to manually remove it
    # more details can be found at https://github.com/open-mmlab/mmcv/pull/1077
    with tempfile.NamedTemporaryFile(delete=False) as f:
Kai Chen's avatar
Kai Chen committed
37
        logger = get_logger('rank0.pkg3', log_file=f.name)
38
39
40
41
42
43
44
45
        assert isinstance(logger, logging.Logger)
        assert len(logger.handlers) == 2
        assert isinstance(logger.handlers[0], logging.StreamHandler)
        assert isinstance(logger.handlers[1], logging.FileHandler)
        logger_pkg3 = get_logger('rank0.pkg3')
        assert id(logger_pkg3) == id(logger)
        # flushing and closing all handlers in order to remove `f.name`
        logging.shutdown()
Kai Chen's avatar
Kai Chen committed
46

47
    os.remove(f.name)
Kai Chen's avatar
Kai Chen committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

    logger_pkg3 = get_logger('rank0.pkg3.subpkg')
    assert logger_pkg3.handlers == logger_pkg3.handlers


@patch('torch.distributed.get_rank', lambda: 1)
@patch('torch.distributed.is_initialized', lambda: True)
@patch('torch.distributed.is_available', lambda: True)
def test_get_logger_rank1():
    logger = get_logger('rank1.pkg1')
    assert isinstance(logger, logging.Logger)
    assert len(logger.handlers) == 1
    assert isinstance(logger.handlers[0], logging.StreamHandler)
    assert logger.handlers[0].level == logging.INFO

63
64
65
66
    # the name can not be used to open the file a second time in windows,
    # so `delete` should be set as `False` and we need to manually remove it
    # more details can be found at https://github.com/open-mmlab/mmcv/pull/1077
    with tempfile.NamedTemporaryFile(delete=False) as f:
Kai Chen's avatar
Kai Chen committed
67
        logger = get_logger('rank1.pkg2', log_file=f.name)
68
69
70
71
72
73
74
        assert isinstance(logger, logging.Logger)
        assert len(logger.handlers) == 1
        assert logger.handlers[0].level == logging.INFO
        # flushing and closing all handlers in order to remove `f.name`
        logging.shutdown()

    os.remove(f.name)
Kai Chen's avatar
Kai Chen committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96


def test_print_log_print(capsys):
    print_log('welcome', logger=None)
    out, _ = capsys.readouterr()
    assert out == 'welcome\n'


def test_print_log_silent(capsys, caplog):
    print_log('welcome', logger='silent')
    out, _ = capsys.readouterr()
    assert out == ''
    assert len(caplog.records) == 0


def test_print_log_logger(caplog):
    print_log('welcome', logger='mmcv')
    assert caplog.record_tuples[-1] == ('mmcv', logging.INFO, 'welcome')

    print_log('welcome', logger='mmcv', level=logging.ERROR)
    assert caplog.record_tuples[-1] == ('mmcv', logging.ERROR, 'welcome')

97
98
99
100
    # the name can not be used to open the file a second time in windows,
    # so `delete` should be set as `False` and we need to manually remove it
    # more details can be found at https://github.com/open-mmlab/mmcv/pull/1077
    with tempfile.NamedTemporaryFile(delete=False) as f:
Kai Chen's avatar
Kai Chen committed
101
102
103
        logger = get_logger('abc', log_file=f.name)
        print_log('welcome', logger=logger)
        assert caplog.record_tuples[-1] == ('abc', logging.INFO, 'welcome')
104
        with open(f.name) as fin:
Kai Chen's avatar
Kai Chen committed
105
106
107
108
109
            log_text = fin.read()
            regex_time = r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3}'
            match = re.fullmatch(regex_time + r' - abc - INFO - welcome\n',
                                 log_text)
            assert match is not None
110
111
112
113
        # flushing and closing all handlers in order to remove `f.name`
        logging.shutdown()

    os.remove(f.name)
Kai Chen's avatar
Kai Chen committed
114
115
116
117
118


def test_print_log_exception():
    with pytest.raises(TypeError):
        print_log('welcome', logger=0)