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