Skip to content

Commit 50a8b00

Browse files
committed
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.
1 parent 0c1dcc3 commit 50a8b00

File tree

2 files changed

+86
-37
lines changed

2 files changed

+86
-37
lines changed

roundup/configuration.py

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2392,45 +2392,56 @@ def load_config_dict_from_json_file(self, filename):
23922392

23932393
def init_logging(self):
23942394
_file = self["LOGGING_CONFIG"]
2395-
if _file and os.path.isfile(_file) and _file.endswith(".ini"):
2396-
logging.config.fileConfig(
2397-
_file,
2398-
disable_existing_loggers=self["LOGGING_DISABLE_LOGGERS"])
2399-
return
2400-
2401-
if _file and os.path.isfile(_file) and _file.endswith(".json"):
2402-
config_dict = self.load_config_dict_from_json_file(_file)
2403-
try:
2404-
logging.config.dictConfig(config_dict)
2405-
except ValueError as e:
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-
2395+
if _file and os.path.isfile(_file):
2396+
if _file.endswith(".ini"):
2397+
logging.config.fileConfig(
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+
24322438
return
24332439

2440+
if _file:
2441+
raise OptionValueError(self.options['LOGGING_CONFIG'],
2442+
_file,
2443+
"Unable to find logging config file.")
2444+
24342445
_file = self["LOGGING_FILENAME"]
24352446
# set file & level on the roundup logger
24362447
logger = logging.getLogger('roundup')

test/test_config.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,3 +1370,41 @@ def testDictLoggerConfigViaJson(self):
13701370
"%s'\n" % (log_config_filename, access_filename))
13711371
self.assertEqual(output, target)
13721372

1373+
def test_missing_logging_config_file(self):
1374+
saved_config = self.db.config['LOGGING_CONFIG']
1375+
1376+
self.db.config['LOGGING_CONFIG'] = 'logging.json'
1377+
1378+
with self.assertRaises(configuration.OptionValueError) as cm:
1379+
self.db.config.init_logging()
1380+
1381+
self.assertEqual(cm.exception.args[1], "_test_instance/logging.json")
1382+
self.assertEqual(cm.exception.args[2],
1383+
"Unable to find logging config file.")
1384+
1385+
self.db.config['LOGGING_CONFIG'] = 'logging.ini'
1386+
1387+
with self.assertRaises(configuration.OptionValueError) as cm:
1388+
self.db.config.init_logging()
1389+
1390+
self.assertEqual(cm.exception.args[1], "_test_instance/logging.ini")
1391+
self.assertEqual(cm.exception.args[2],
1392+
"Unable to find logging config file.")
1393+
1394+
self.db.config['LOGGING_CONFIG'] = saved_config
1395+
1396+
def test_unknown_logging_config_file_type(self):
1397+
saved_config = self.db.config['LOGGING_CONFIG']
1398+
1399+
self.db.config['LOGGING_CONFIG'] = 'schema.py'
1400+
1401+
1402+
with self.assertRaises(configuration.OptionValueError) as cm:
1403+
self.db.config.init_logging()
1404+
1405+
self.assertEqual(cm.exception.args[1], "_test_instance/schema.py")
1406+
self.assertEqual(cm.exception.args[2],
1407+
"Unable to load logging config file. "
1408+
"File extension must be '.ini' or '.json'.\n")
1409+
1410+
self.db.config['LOGGING_CONFIG'] = saved_config

0 commit comments

Comments
 (0)