handle UnicodeDecodeError exceptions when json encoding log messages
This commit is contained in:
parent
08135d5cd6
commit
f4861009f8
2 changed files with 9 additions and 1 deletions
|
@ -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:
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in a new issue