|
2 | 2 | import sys |
3 | 3 | from threading import Lock |
4 | 4 |
|
5 | | -logger_names_with_handlers_lock = Lock() |
6 | | -logger_names_with_handlers = set() |
7 | | - |
8 | | - |
9 | | -def get_logger(debug): |
10 | | - return __get_logger(debug, __name__) |
| 5 | +configured_loggers_lock = Lock() |
| 6 | +configured_loggers = set() |
11 | 7 |
|
12 | 8 |
|
13 | 9 | def get_stdout_logger(debug): |
14 | | - stdout_logger = __get_logger(debug, __name__ + "_stdout", logging.StreamHandler(sys.stdout)) |
15 | | - stdout_logger.propagate = False |
16 | | - return stdout_logger |
| 10 | + stdout_logger_name = __name__ + "_stdout" |
| 11 | + stdout_logger = logging.getLogger(stdout_logger_name) |
17 | 12 |
|
| 13 | + requires_config = False |
| 14 | + with configured_loggers_lock: |
| 15 | + if stdout_logger_name not in configured_loggers: |
| 16 | + configured_loggers.add(stdout_logger_name) |
| 17 | + requires_config = True |
18 | 18 |
|
19 | | -def __get_logger(debug, name, handler=None): |
20 | | - logger = logging.getLogger(name) |
| 19 | + if requires_config: |
| 20 | + stdout_logger.propagate = False |
| 21 | + stdout_logger.setLevel(logging.DEBUG if debug else logging.INFO) |
| 22 | + stdout_logger.addHandler(logging.StreamHandler(sys.stdout)) |
21 | 23 |
|
22 | | - with logger_names_with_handlers_lock: |
23 | | - if name not in logger_names_with_handlers: |
24 | | - logger.setLevel(logging.DEBUG if debug else logging.INFO) |
25 | | - if handler: |
26 | | - logger.addHandler(handler) |
27 | | - logger_names_with_handlers.add(name) |
28 | | - |
29 | | - return logger |
| 24 | + return stdout_logger |
0 commit comments