forked from optuna/optuna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_logging.py
96 lines (72 loc) · 3.13 KB
/
test_logging.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import logging
import _pytest.capture
import _pytest.logging
import optuna.logging
def test_get_logger(caplog: _pytest.logging.LogCaptureFixture) -> None:
# Log propagation is necessary for caplog to capture log outputs.
optuna.logging.enable_propagation()
logger = optuna.logging.get_logger("optuna.foo")
with caplog.at_level(logging.INFO, logger="optuna.foo"):
logger.info("hello")
assert "hello" in caplog.text
def test_default_handler(capsys: _pytest.capture.CaptureFixture) -> None:
# We need to reconstruct our default handler to properly capture stderr.
optuna.logging._reset_library_root_logger()
optuna.logging.set_verbosity(optuna.logging.INFO)
library_root_logger = optuna.logging._get_library_root_logger()
example_logger = optuna.logging.get_logger("optuna.bar")
# Default handler enabled
optuna.logging.enable_default_handler()
assert library_root_logger.handlers
example_logger.info("hey")
_, err = capsys.readouterr()
assert "hey" in err
# Default handler disabled
optuna.logging.disable_default_handler()
assert not library_root_logger.handlers
example_logger.info("yoyo")
_, err = capsys.readouterr()
assert "yoyo" not in err
def test_verbosity(capsys: _pytest.capture.CaptureFixture) -> None:
# We need to reconstruct our default handler to properly capture stderr.
optuna.logging._reset_library_root_logger()
library_root_logger = optuna.logging._get_library_root_logger()
example_logger = optuna.logging.get_logger("optuna.hoge")
optuna.logging.enable_default_handler()
# level INFO
optuna.logging.set_verbosity(optuna.logging.INFO)
assert library_root_logger.getEffectiveLevel() == logging.INFO
example_logger.warning("hello-warning")
example_logger.info("hello-info")
example_logger.debug("hello-debug")
_, err = capsys.readouterr()
assert "hello-warning" in err
assert "hello-info" in err
assert "hello-debug" not in err
# level WARNING
optuna.logging.set_verbosity(optuna.logging.WARNING)
assert library_root_logger.getEffectiveLevel() == logging.WARNING
example_logger.warning("bye-warning")
example_logger.info("bye-info")
example_logger.debug("bye-debug")
_, err = capsys.readouterr()
assert "bye-warning" in err
assert "bye-info" not in err
assert "bye-debug" not in err
def test_propagation(caplog: _pytest.capture.CaptureFixture) -> None:
optuna.logging._reset_library_root_logger()
logger = optuna.logging.get_logger("optuna.foo")
# Propagation is disabled by default.
with caplog.at_level(logging.INFO, logger="optuna"):
logger.info("no-propagation")
assert "no-propagation" not in caplog.text
# Enable propagation.
optuna.logging.enable_propagation()
with caplog.at_level(logging.INFO, logger="optuna"):
logger.info("enable-propagate")
assert "enable-propagate" in caplog.text
# Disable propagation.
optuna.logging.disable_propagation()
with caplog.at_level(logging.INFO, logger="optuna"):
logger.info("disable-propagation")
assert "disable-propagation" not in caplog.text