Mercurial > p > roundup > code
comparison roundup/configuration.py @ 8434:66284037142e
refactor: also error on missing file or invalid extension
Refactored the code to reuse check that logging config file is set and
that the file exists.
Now throws error and exits if file name does not end in .ini or .json.
Now throws error if file doesn't exist. Before it would just configure
default logging as though file wasn't specified.
Added tests for these two cases.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Tue, 26 Aug 2025 23:06:40 -0400 |
| parents | de1dac9abcb6 |
| children | 39a6825d10ca |
comparison
equal
deleted
inserted
replaced
| 8433:de1dac9abcb6 | 8434:66284037142e |
|---|---|
| 2390 | 2390 |
| 2391 return config_dict | 2391 return config_dict |
| 2392 | 2392 |
| 2393 def init_logging(self): | 2393 def init_logging(self): |
| 2394 _file = self["LOGGING_CONFIG"] | 2394 _file = self["LOGGING_CONFIG"] |
| 2395 if _file and os.path.isfile(_file) and _file.endswith(".ini"): | 2395 if _file and os.path.isfile(_file): |
| 2396 logging.config.fileConfig( | 2396 if _file.endswith(".ini"): |
| 2397 _file, | 2397 logging.config.fileConfig( |
| 2398 disable_existing_loggers=self["LOGGING_DISABLE_LOGGERS"]) | 2398 _file, |
| 2399 disable_existing_loggers=self["LOGGING_DISABLE_LOGGERS"]) | |
| 2400 elif _file.endswith(".json"): | |
| 2401 config_dict = self.load_config_dict_from_json_file(_file) | |
| 2402 try: | |
| 2403 logging.config.dictConfig(config_dict) | |
| 2404 except ValueError as e: | |
| 2405 # docs say these exceptions: | |
| 2406 # ValueError, TypeError, AttributeError, ImportError | |
| 2407 # could be raised, but | |
| 2408 # looking through the code, it looks like | |
| 2409 # configure() maps all exceptions (including | |
| 2410 # ImportError, TypeError) raised by functions to | |
| 2411 # ValueError. | |
| 2412 context = "No additional information available." | |
| 2413 if hasattr(e, '__context__') and e.__context__: | |
| 2414 # get additional error info. E.G. if INFO | |
| 2415 # is replaced by MANGO, context is: | |
| 2416 # ValueError("Unknown level: 'MANGO'") | |
| 2417 # while str(e) is "Unable to configure handler 'access'" | |
| 2418 context = e.__context__ | |
| 2419 | |
| 2420 raise LoggingConfigError( | |
| 2421 'Error loading logging dict from %(file)s.\n' | |
| 2422 '%(msg)s\n%(context)s\n' % { | |
| 2423 "file": _file, | |
| 2424 "msg": type(e).__name__ + ": " + str(e), | |
| 2425 "context": context | |
| 2426 }, | |
| 2427 config_file=self.filepath, | |
| 2428 source="dictConfig" | |
| 2429 ) | |
| 2430 else: | |
| 2431 raise OptionValueError( | |
| 2432 self.options['LOGGING_CONFIG'], | |
| 2433 _file, | |
| 2434 "Unable to load logging config file. " | |
| 2435 "File extension must be '.ini' or '.json'.\n" | |
| 2436 ) | |
| 2437 | |
| 2399 return | 2438 return |
| 2400 | 2439 |
| 2401 if _file and os.path.isfile(_file) and _file.endswith(".json"): | 2440 if _file: |
| 2402 config_dict = self.load_config_dict_from_json_file(_file) | 2441 raise OptionValueError(self.options['LOGGING_CONFIG'], |
| 2403 try: | 2442 _file, |
| 2404 logging.config.dictConfig(config_dict) | 2443 "Unable to find logging config file.") |
| 2405 except ValueError as e: | 2444 |
| 2406 # docs say these exceptions: | |
| 2407 # ValueError, TypeError, AttributeError, ImportError | |
| 2408 # could be raised, but | |
| 2409 # looking through the code, it looks like | |
| 2410 # configure() maps all exceptions (including | |
| 2411 # ImportError, TypeError) raised by functions to | |
| 2412 # ValueError. | |
| 2413 context = "No additional information available." | |
| 2414 if hasattr(e, '__context__') and e.__context__: | |
| 2415 # get additional error info. E.G. if INFO | |
| 2416 # is replaced by MANGO, context is: | |
| 2417 # ValueError("Unknown level: 'MANGO'") | |
| 2418 # while str(e) is "Unable to configure handler 'access'" | |
| 2419 context = e.__context__ | |
| 2420 | |
| 2421 raise LoggingConfigError( | |
| 2422 'Error loading logging dict from %(file)s.\n' | |
| 2423 '%(msg)s\n%(context)s\n' % { | |
| 2424 "file": _file, | |
| 2425 "msg": type(e).__name__ + ": " + str(e), | |
| 2426 "context": context | |
| 2427 }, | |
| 2428 config_file=self.filepath, | |
| 2429 source="dictConfig" | |
| 2430 ) | |
| 2431 | |
| 2432 return | |
| 2433 | |
| 2434 _file = self["LOGGING_FILENAME"] | 2445 _file = self["LOGGING_FILENAME"] |
| 2435 # set file & level on the roundup logger | 2446 # set file & level on the roundup logger |
| 2436 logger = logging.getLogger('roundup') | 2447 logger = logging.getLogger('roundup') |
| 2437 hdlr = logging.FileHandler(_file) if _file else \ | 2448 hdlr = logging.FileHandler(_file) if _file else \ |
| 2438 logging.StreamHandler(sys.stdout) | 2449 logging.StreamHandler(sys.stdout) |
