|
1 | 1 | import logging |
2 | 2 |
|
| 3 | +from rich.logging import RichHandler |
| 4 | + |
3 | 5 | import fancylog |
4 | 6 |
|
5 | 7 | lateral_separator = "**************" |
@@ -38,3 +40,104 @@ def test_logger_name(tmp_path): |
38 | 40 | fancylog.start_logging(tmp_path, fancylog, logger_name=logger_name) |
39 | 41 |
|
40 | 42 | assert logger_name in logging.root.manager.loggerDict |
| 43 | + |
| 44 | + |
| 45 | +def test_correct_handlers_are_set(tmp_path): |
| 46 | + """ |
| 47 | + Test the handlers on the logger are as specified by the |
| 48 | + `start_logging` call. Note this cannot be tested |
| 49 | + on the root logger has it holds handlers that |
| 50 | + were assigned in earlier tests. |
| 51 | + """ |
| 52 | + logger_name = "hello_world" |
| 53 | + |
| 54 | + # Test no handlers are assigned when non requested |
| 55 | + fancylog.start_logging( |
| 56 | + tmp_path, |
| 57 | + fancylog, |
| 58 | + logger_name=logger_name, |
| 59 | + log_to_console=False, |
| 60 | + log_to_file=False, |
| 61 | + ) |
| 62 | + |
| 63 | + logger = logging.getLogger(logger_name) |
| 64 | + |
| 65 | + assert logger.handlers == [] |
| 66 | + |
| 67 | + # Test RichHandler only is assigned when requested |
| 68 | + fancylog.start_logging( |
| 69 | + tmp_path, |
| 70 | + fancylog, |
| 71 | + logger_name=logger_name, |
| 72 | + log_to_console=True, |
| 73 | + log_to_file=False, |
| 74 | + ) |
| 75 | + |
| 76 | + assert len(logger.handlers) == 1 |
| 77 | + assert isinstance(logger.handlers[0], RichHandler) |
| 78 | + |
| 79 | + # Test FileHandler only is assigned when requested |
| 80 | + fancylog.start_logging( |
| 81 | + tmp_path, |
| 82 | + fancylog, |
| 83 | + logger_name=logger_name, |
| 84 | + log_to_console=False, |
| 85 | + log_to_file=True, |
| 86 | + ) |
| 87 | + |
| 88 | + assert len(logger.handlers) == 1 |
| 89 | + assert isinstance(logger.handlers[0], logging.FileHandler) |
| 90 | + |
| 91 | + # Test both handlers are assigned when requested |
| 92 | + fancylog.start_logging( |
| 93 | + tmp_path, |
| 94 | + fancylog, |
| 95 | + logger_name=logger_name, |
| 96 | + log_to_console=True, |
| 97 | + log_to_file=True, |
| 98 | + ) |
| 99 | + |
| 100 | + assert len(logger.handlers) == 2 |
| 101 | + assert isinstance(logger.handlers[0], logging.FileHandler) |
| 102 | + assert isinstance(logger.handlers[1], RichHandler) |
| 103 | + |
| 104 | + |
| 105 | +def test_handlers_are_refreshed(tmp_path): |
| 106 | + """ |
| 107 | + When a named logger is requested, the handlers |
| 108 | + are reset to that handlers assigned on previous |
| 109 | + calls are not carried over to the most recent call. |
| 110 | + """ |
| 111 | + logger_name = "hello_world" |
| 112 | + fancylog.start_logging( |
| 113 | + tmp_path, |
| 114 | + fancylog, |
| 115 | + logger_name=logger_name, |
| 116 | + log_to_console=False, |
| 117 | + log_to_file=False, |
| 118 | + ) |
| 119 | + |
| 120 | + logger = logging.getLogger(logger_name) |
| 121 | + |
| 122 | + assert logger.handlers == [] |
| 123 | + |
| 124 | + fancylog.start_logging( |
| 125 | + tmp_path, |
| 126 | + fancylog, |
| 127 | + logger_name=logger_name, |
| 128 | + log_to_console=True, |
| 129 | + log_to_file=False, |
| 130 | + ) |
| 131 | + |
| 132 | + assert len(logger.handlers) == 1 |
| 133 | + assert isinstance(logger.handlers[0], RichHandler) |
| 134 | + |
| 135 | + fancylog.start_logging( |
| 136 | + tmp_path, |
| 137 | + fancylog, |
| 138 | + logger_name=logger_name, |
| 139 | + log_to_console=False, |
| 140 | + log_to_file=False, |
| 141 | + ) |
| 142 | + |
| 143 | + assert logger.handlers == [] |
0 commit comments