| 1 |
import multiprocessing |
| 2 |
import os |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from brian2.core.preferences import prefs |
| 7 |
from brian2.utils.logger import BrianLogger, catch_logs, get_logger |
| 8 |
|
| 9 |
logger = get_logger("brian2.tests.test_logger") |
| 10 |
|
| 11 |
|
| 12 |
@pytest.mark.codegen_independent |
| 13 |
def test_file_logging(): |
| 14 |
BrianLogger.initialize() |
| 15 |
logger.error("error message xxx") |
| 16 |
logger.warn("warning message xxx") |
| 17 |
logger.info("info message xxx") |
| 18 |
logger.debug("debug message xxx") |
| 19 |
logger.diagnostic("diagnostic message xxx") |
| 20 |
BrianLogger.file_handler.flush() |
| 21 |
# By default, only >= debug messages should show up |
| 22 |
assert os.path.isfile(BrianLogger.tmp_log) |
| 23 |
with open(BrianLogger.tmp_log, encoding="utf-8") as f: |
| 24 |
log_content = f.readlines() |
| 25 |
for level, line in zip(["error", "warning", "info", "debug"], log_content[-4:]): |
| 26 |
assert "brian2.tests.test_logger" in line |
| 27 |
assert f"{level} message xxx" in line |
| 28 |
assert level.upper() in line |
| 29 |
|
| 30 |
|
| 31 |
@pytest.mark.codegen_independent |
| 32 |
def test_file_logging_special_characters(): |
| 33 |
BrianLogger.initialize() |
| 34 |
# Test logging with special characters that could occur in log messages and |
| 35 |
# require UTF-8 |
| 36 |
special_chars = "→ ≠ ≤ ≥ ← ∞ µ ∝ ∂ ∅" |
| 37 |
logger.debug(special_chars) |
| 38 |
BrianLogger.file_handler.flush() |
| 39 |
assert os.path.isfile(BrianLogger.tmp_log) |
| 40 |
with open(BrianLogger.tmp_log, encoding="utf-8") as f: |
| 41 |
log_content = f.readlines() |
| 42 |
last_line = log_content[-1] |
| 43 |
assert "brian2.tests.test_logger" in last_line |
| 44 |
assert special_chars in last_line |
| 45 |
|
| 46 |
|
| 47 |
def run_in_process(x): |
| 48 |
logger.info(f"subprocess info message {x}") |
| 49 |
|
| 50 |
|
| 51 |
def run_in_process_with_logger(x): |
| 52 |
prefs.logging.delete_log_on_exit = False |
| 53 |
BrianLogger.initialize() |
| 54 |
logger.info(f"subprocess info message {x}") |
| 55 |
BrianLogger.file_handler.flush() |
| 56 |
return BrianLogger.tmp_log |
| 57 |
|
| 58 |
|
| 59 |
@pytest.mark.codegen_independent |
| 60 |
def test_file_logging_multiprocessing(): |
| 61 |
logger.info("info message before multiprocessing") |
| 62 |
p = multiprocessing.Pool() |
| 63 |
|
| 64 |
try: |
| 65 |
p.map(run_in_process, range(3)) |
| 66 |
finally: |
| 67 |
p.close() |
| 68 |
p.join() |
| 69 |
|
| 70 |
BrianLogger.file_handler.flush() |
| 71 |
assert os.path.isfile(BrianLogger.tmp_log) |
| 72 |
with open(BrianLogger.tmp_log, encoding="utf-8") as f: |
| 73 |
log_content = f.readlines() |
| 74 |
# The subprocesses should not have written to the log file |
| 75 |
assert "info message before multiprocessing" in log_content[-1] |
| 76 |
|
| 77 |
|
| 78 |
@pytest.mark.codegen_independent |
| 79 |
def test_file_logging_multiprocessing_with_loggers(): |
| 80 |
logger.info("info message before multiprocessing") |
| 81 |
|
| 82 |
p = multiprocessing.Pool() |
| 83 |
try: |
| 84 |
log_files = p.map(run_in_process_with_logger, range(3)) |
| 85 |
finally: |
| 86 |
p.close() |
| 87 |
p.join() |
| 88 |
|
| 89 |
BrianLogger.file_handler.flush() |
| 90 |
assert os.path.isfile(BrianLogger.tmp_log) |
| 91 |
with open(BrianLogger.tmp_log, encoding="utf-8") as f: |
| 92 |
log_content = f.readlines() |
| 93 |
# The subprocesses should not have written to the main log file |
| 94 |
assert "info message before multiprocessing" in log_content[-1] |
| 95 |
|
| 96 |
# Each subprocess should have their own log file |
| 97 |
for x, log_file in enumerate(log_files): |
| 98 |
assert os.path.isfile(log_file) |
| 99 |
with open(log_file, encoding="utf-8") as f: |
| 100 |
log_content = f.readlines() |
| 101 |
assert f"subprocess info message {x}" in log_content[-1] |
| 102 |
|
| 103 |
prefs.logging.delete_log_on_exit = True |
| 104 |
|
| 105 |
|
| 106 |
@pytest.mark.codegen_independent |
| 107 |
def test_submodule_logging(): |
| 108 |
submodule_logger = get_logger("submodule.dummy") |
| 109 |
BrianLogger.initialize() |
| 110 |
submodule_logger.error("error message xxx") |
| 111 |
submodule_logger.warn("warning message xxx") |
| 112 |
submodule_logger.info("info message xxx") |
| 113 |
submodule_logger.debug("debug message xxx") |
| 114 |
submodule_logger.diagnostic("diagnostic message xxx") |
| 115 |
BrianLogger.file_handler.flush() |
| 116 |
# By default, only >= debug messages should show up |
| 117 |
assert os.path.isfile(BrianLogger.tmp_log) |
| 118 |
with open(BrianLogger.tmp_log, encoding="utf-8") as f: |
| 119 |
log_content = f.readlines() |
| 120 |
for level, line in zip(["error", "warning", "info", "debug"], log_content[-4:]): |
| 121 |
assert "submodule.dummy" in line |
| 122 |
# The logger name has brian2 internally prefixed, but this shouldn't show up in logs |
| 123 |
assert not "brian2.submodule.dummy" in line |
| 124 |
assert f"{level} message xxx" in line |
| 125 |
assert level.upper() in line |
| 126 |
|
| 127 |
with catch_logs() as l: |
| 128 |
logger.warn("warning message from Brian") |
| 129 |
submodule_logger.warn("warning message from submodule") |
| 130 |
# only the warning from Brian should be logged |
| 131 |
assert len(l) == 1 |
| 132 |
assert "warning message from Brian" in l[0] |
| 133 |
|
| 134 |
with catch_logs(only_from=("submodule",)) as l: |
| 135 |
logger.warn("warning message from Brian") |
| 136 |
submodule_logger.warn("warning message from submodule") |
| 137 |
# only the warning from submodule should be logged |
| 138 |
assert len(l) == 1 |
| 139 |
assert "warning message from submodule" in l[0] |
| 140 |
|
| 141 |
# Make sure that a submodule with a name starting with "brian2" gets handled correctly |
| 142 |
submodule_logger = get_logger("brian2submodule.dummy") |
| 143 |
BrianLogger.initialize() |
| 144 |
submodule_logger.error("error message xxx") |
| 145 |
submodule_logger.warn("warning message xxx") |
| 146 |
submodule_logger.info("info message xxx") |
| 147 |
submodule_logger.debug("debug message xxx") |
| 148 |
submodule_logger.diagnostic("diagnostic message xxx") |
| 149 |
BrianLogger.file_handler.flush() |
| 150 |
# By default, only >= debug messages should show up |
| 151 |
assert os.path.isfile(BrianLogger.tmp_log) |
| 152 |
with open(BrianLogger.tmp_log, encoding="utf-8") as f: |
| 153 |
log_content = f.readlines() |
| 154 |
for level, line in zip(["error", "warning", "info", "debug"], log_content[-4:]): |
| 155 |
assert "submodule.dummy" in line |
| 156 |
# The logger name has brian2 internally prefixed, but this shouldn't show up in logs |
| 157 |
assert not "brian2.submodule.dummy" in line |
| 158 |
assert f"{level} message xxx" in line |
| 159 |
assert level.upper() in line |
| 160 |
|
| 161 |
with catch_logs() as l: |
| 162 |
logger.warn("warning message from Brian") |
| 163 |
submodule_logger.warn("warning message from submodule") |
| 164 |
# only the warning from Brian should be logged |
| 165 |
assert len(l) == 1 |
| 166 |
assert "warning message from Brian" in l[0] |
| 167 |
|
| 168 |
|
| 169 |
if __name__ == "__main__": |
| 170 |
test_file_logging() |
| 171 |
test_file_logging_special_characters() |
| 172 |
test_file_logging_multiprocessing() |
| 173 |
test_file_logging_multiprocessing_with_loggers() |