Python 通过日志记录打印异常时发生异常。异常()

Python 通过日志记录打印异常时发生异常。异常(),python,docker,logging,character-encoding,traceback,Python,Docker,Logging,Character Encoding,Traceback,我有一个try块,它每次都会引发异常,在中,除了我使用logging.exception('some message')记录错误的块之外。但在处理上述异常时,发生了另一个异常 try: raise Exception('') #some key error exception raised. except Exception as e: abortMsg = "some error" logging.exceptio

我有一个try块,它每次都会引发异常,在中,除了我使用
logging.exception('some message')
记录错误的块之外。但在处理上述异常时,发生了另一个异常

    try:
        raise Exception('')
        #some key error exception raised.
    except Exception as e:
        abortMsg = "some error"
        logging.exception(abortMsg)
        return
我很确定这个错误是由异常的堆栈跟踪、任何与之相关的操作(如打印它或使用
traceback)引起的。print_exc()
递归地引发异常

使用
traceback.print\u exc()时的错误日志

错误日志:

--- Logging error ---
Unhandled exception in thread started by <bound method Thread._bootstrap of <Thread(Thread-5, 
started 139762597885696)>>
Traceback (most recent call last):
File "somefile.py", line 570, in postProcess
reporter.write_report()
File "someotherfile.py", line 115, in write_report
File "someotherfile.py", line 295, in _addSectionResult
File "someotherfile.py", line 315, in _addCheckSummary
KeyError: 'message_zero'


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/somefile.py", line 310, in execute
  self.postProcess()
  File "/somefile.py", line 580, in postProcess
  s = str(traceback.print_exc())
  File "/usr/lib/python3.5/traceback.py", line 159, in print_exc
  print_exception(*sys.exc_info(), limit=limit, file=file, chain=chain)
  File "/usr/lib/python3.5/traceback.py", line 100, in print_exception
  type(value), value, tb, limit=limit).format(chain=chain):
  File "/usr/lib/python3.5/traceback.py", line 474, in __init__
  capture_locals=capture_locals)
  File "/usr/lib/python3.5/traceback.py", line 358, in extract
  f.line
  File "/usr/lib/python3.5/traceback.py", line 282, in line
  self._line = linecache.getline(self.filename, self.lineno).strip()
  File "/usr/lib/python3.5/linecache.py", line 16, in getline
lines = getlines(filename, module_globals)
 File "/usr/lib/python3.5/linecache.py", line 47, in getlines
return updatecache(filename, module_globals)
 File "/usr/lib/python3.5/linecache.py", line 137, in updatecache
lines = fp.readlines()
 File "/usr/lib/python3.5/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa3 in position 4: invalid start byte'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.5/logging/__init__.py", line 980, in emit
msg = self.format(record)
  File "/usr/lib/python3.5/logging/__init__.py", line 830, in format
return fmt.format(record)
  File "/usr/lib/python3.5/logging/__init__.py", line 575, in format
record.exc_text = self.formatException(record.exc_info)
  File "/usr/lib/python3.5/logging/__init__.py", line 525, in formatException
traceback.print_exception(ei[0], ei[1], tb, None, sio)
  File "/usr/lib/python3.5/traceback.py", line 100, in print_exception
type(value), value, tb, limit=limit).format(chain=chain):
  File "/usr/lib/python3.5/traceback.py", line 487, in __init__
self._load_lines()
  File "/usr/lib/python3.5/traceback.py", line 500, in _load_lines
self.__context__._load_lines()
  File "/usr/lib/python3.5/traceback.py", line 498, in _load_lines
frame.line
  File "/usr/lib/python3.5/traceback.py", line 282, in line
self._line = linecache.getline(self.filename, self.lineno).strip()
  File "/usr/lib/python3.5/linecache.py", line 16, in getline
lines = getlines(filename, module_globals)
  File "/usr/lib/python3.5/linecache.py", line 47, in getlines
return updatecache(filename, module_globals)
  File "/usr/lib/python3.5/linecache.py", line 137, in updatecache
lines = fp.readlines()
  File "/usr/lib/python3.5/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa3 in position 4: invalid start byte
---日志记录错误---
由启动的线程中存在未处理的异常
回溯(最近一次呼叫最后一次):
文件“somefile.py”,第570行,在后处理中
记者:写报告
文件“someotherfile.py”,第115行,在write_报告中
文件“someotherfile.py”,第295行,在_addSectionResult中
文件“someotherfile.py”,第315行,在_addCheckSummary中
KeyError:“消息\u零”
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“/somefile.py”,执行中的第310行
self.postProcess()
文件“/somefile.py”,第580行,在后处理中
s=str(traceback.print\u exc())
文件“/usr/lib/python3.5/traceback.py”,第159行,在print_exc中
打印异常(*sys.exc_info(),limit=limit,file=file,chain=chain)
文件“/usr/lib/python3.5/traceback.py”,第100行,打印异常
类型(值),值,tb,限制=限制)。格式(链=链):
文件“/usr/lib/python3.5/traceback.py”,第474行,在__
捕获\u局部变量=捕获\u局部变量)
文件“/usr/lib/python3.5/traceback.py”,第358行,摘录
f、 线
文件“/usr/lib/python3.5/traceback.py”,第282行,第2行
self.\u line=linecache.getline(self.filename,self.lineno).strip()
文件“/usr/lib/python3.5/linecache.py”,第16行,在getline中
lines=getlines(文件名、模块\全局)
文件“/usr/lib/python3.5/linecache.py”,第47行,在getlines中
返回updatecache(文件名,模块\全局)
文件“/usr/lib/python3.5/linecache.py”,第137行,在updatecache中
lines=fp.readlines()
文件“/usr/lib/python3.5/codecs.py”,第321行,解码中
(结果,消耗)=自身缓冲区解码(数据,自身错误,最终)
UnicodeDecodeError:“utf-8”编解码器无法解码位置4中的字节0xa3:无效的开始字节”
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“/usr/lib/python3.5/logging/_init_uuu.py”,第980行,在emit中
msg=self.format(记录)
文件“/usr/lib/python3.5/logging/_init__uuu.py”,第830行,格式为
返回格式(记录)
文件“/usr/lib/python3.5/logging/_init__.py”,第575行,格式为
record.exc_text=self.formatException(record.exc_info)
formatException中的文件“/usr/lib/python3.5/logging/_init__uuu.py”,第525行
回溯打印异常(ei[0],ei[1],tb,无,sio)
文件“/usr/lib/python3.5/traceback.py”,第100行,打印异常
类型(值),值,tb,限制=限制)。格式(链=链):
文件“/usr/lib/python3.5/traceback.py”,第487行,在__
自身荷载线()
文件“/usr/lib/python3.5/traceback.py”,第500行,在加载行中
self.\uuuuu上下文\uuuuu.\u加载线()
文件“/usr/lib/python3.5/traceback.py”,第498行,在加载行中
帧线
文件“/usr/lib/python3.5/traceback.py”,第282行,第2行
self.\u line=linecache.getline(self.filename,self.lineno).strip()
文件“/usr/lib/python3.5/linecache.py”,第16行,在getline中
lines=getlines(文件名、模块\全局)
文件“/usr/lib/python3.5/linecache.py”,第47行,在getlines中
返回updatecache(文件名,模块\全局)
文件“/usr/lib/python3.5/linecache.py”,第137行,在updatecache中
lines=fp.readlines()
文件“/usr/lib/python3.5/codecs.py”,第321行,解码中
(结果,消耗)=自身缓冲区解码(数据,自身错误,最终)
UnicodeDecodeError:“utf-8”编解码器无法解码位置4中的字节0xa3:无效的开始字节
有没有办法查看和分析导致错误的堆栈跟踪

附言:我在码头工人里面用这个。当我在本地运行此文件时,不会引发递归异常


编辑:从代码的任何位置引发的任何异常都会导致递归异常(unicode解码错误)

您的机器和docker中的
区域设置是什么?在我的机器和docker上使用python 3.5.2,在bothI上使用ubuntu 16.04意味着,在终端中运行
locale
的输出是什么?我的docker locale'LANG=C.UTF-8 LANGUAGE=LC\u CTYPE=“C.UTF-8”LC\u NUMERIC=“C.UTF-8”LC\u TIME=“C.UTF-8”LC\u COLLATE=“C.UTF-8”LC\u MESSAGES=“C.UTF-8”LC\u PAPER=“C.UTF-8”LC\u NAME=“C.UTF-8”LC\u地址=“C.UTF-8”电话测量LC\u IDENTIFICATION=“C.UTF-8”LC\u ALL='my machine locale'LANG=en\u IN LANGUAGE=LC\u CTYPE=“en\u IN”LC\u NUMERIC=“en\u IN”LC\u TIME=“en\u IN”LC\u COLLATE=“en\u IN”LC\u MONETARY=“en\u IN”LC\u MESSAGES=“en\u IN”LC\u PAPER=“en\u IN”LC\u NAME=“en\u IN”LC\u ADDRESS=“en\u IN”LCLC\u ALL='在您的机器和docker中的
locale
设置是什么?在我的机器和docker上使用python 3.5.2,在bothI上使用ubuntu 16.04意味着,在终端中运行
locale
的输出是什么?我的docker locale'LANG=C.UTF-8 LANGUAGE=LC\u CTYPE=“C.UTF-8”LC\u NUMERIC=“C.UTF-8”LC\u TIME=“C.UTF-8”LC\u COLLATE=“C.UTF-8”LC\u MONETARY=“C.UTF-8”LC\u MESSAGES=“C.UTF-8”LC\u PAPER=“C.UTF-8”LC\u NAME=“C.UTF-8”LC\u ADDRESS=“C.UTF-8”LC\u MEASUREMENT=“C.UTF-8”LC\u IDENTIFICATION=“C.UTF-8”LC\u ALL='my machine locale'LANG=en\u语言=LC\u CTYPE=“en\u IN”LC\u NUMERIC=“en\u-IN”LC=“en\u-IN”LC\u TIME=“en\u-IN”LC\u-IN”LC=“en_IN”LC_NAME=“en_IN”LC_ADDRESS=“en_IN”LC_TELEPHONE=“en_IN”LC_MEASUREMENT=“en_IN”LC_IDENTIFICATION=“en_IN”LC_ALL=”