Read the logging configuration from a ConfigParser-format file. This can be called several times from an application, allowing an end user the ability to select from various pre-canned configurations (if the developer provides a mechanism to present the choices and load the chosen
(fname, defaults=None, disable_existing_loggers=True, encoding=None)
| 51 | _listener = None |
| 52 | |
| 53 | def fileConfig(fname, defaults=None, disable_existing_loggers=True, encoding=None): |
| 54 | """ |
| 55 | Read the logging configuration from a ConfigParser-format file. |
| 56 | |
| 57 | This can be called several times from an application, allowing an end user |
| 58 | the ability to select from various pre-canned configurations (if the |
| 59 | developer provides a mechanism to present the choices and load the chosen |
| 60 | configuration). |
| 61 | """ |
| 62 | import configparser |
| 63 | |
| 64 | if isinstance(fname, str): |
| 65 | if not os.path.exists(fname): |
| 66 | raise FileNotFoundError(f"{fname} doesn't exist") |
| 67 | elif not os.path.getsize(fname): |
| 68 | raise RuntimeError(f'{fname} is an empty file') |
| 69 | |
| 70 | if isinstance(fname, configparser.RawConfigParser): |
| 71 | cp = fname |
| 72 | else: |
| 73 | try: |
| 74 | cp = configparser.ConfigParser(defaults) |
| 75 | if hasattr(fname, 'readline'): |
| 76 | cp.read_file(fname) |
| 77 | else: |
| 78 | encoding = io.text_encoding(encoding) |
| 79 | cp.read(fname, encoding=encoding) |
| 80 | except configparser.ParsingError as e: |
| 81 | raise RuntimeError(f'{fname} is invalid: {e}') |
| 82 | |
| 83 | formatters = _create_formatters(cp) |
| 84 | |
| 85 | # critical section |
| 86 | with logging._lock: |
| 87 | _clearExistingHandlers() |
| 88 | |
| 89 | # Handlers add themselves to logging._handlers |
| 90 | handlers = _install_handlers(cp, formatters) |
| 91 | _install_loggers(cp, handlers, disable_existing_loggers) |
| 92 | |
| 93 | |
| 94 | def _resolve(name): |
no test coverage detected
searching dependent graphs…