handle UnicodeDecodeError exceptions when json encoding log messages

This commit is contained in:
Alan Hamlett 2013-11-21 01:13:10 -08:00
parent 08135d5cd6
commit f4861009f8
2 changed files with 9 additions and 1 deletions

View file

@ -63,6 +63,9 @@ def parseConfigFile(configFile):
'verbose': False,
}
if not os.path.isfile(configFile):
return configs
try:
with open(configFile) as fh:
for line in fh:

View file

@ -25,7 +25,12 @@ class CustomEncoder(json.JSONEncoder):
if isinstance(obj, bytes):
obj = bytes.decode(obj)
return json.dumps(obj)
return super(CustomEncoder, self).default(obj)
try:
encoded = super(CustomEncoder, self).default(obj)
except UnicodeDecodeError:
obj = obj.decode('utf-8', 'ignore')
encoded = super(CustomEncoder, self).default(obj)
return encoded
class JsonFormatter(logging.Formatter):