two*_*ine 10 python logging traceback
我在Python中创建的日志旨在临时存储为文件,然后将这些文件处理到日志数据库中.它们采用管道描述的格式来指示如何处理日志,但logging.exception()通过添加太多字段和太多新行来破坏我的标准.
import logging
logging.basicConfig(filename='output.txt',
format='%(asctime)s|%(levelname)s|%(message)s|',
datefmt='%m/%d/%Y %I:%M:%S %p',
level=logging.DEBUG)
logging.info('Sample message')
try:
x = 1 / 0
except ZeroDivisionError as e:
logging.exception('ZeroDivisionError: {0}'.format(e))
# output.txt
01/27/2015 02:09:01 PM|INFO|Sample message|
01/27/2015 02:09:01 PM|ERROR|ZeroDivisionError: integer division or modulo by zero|
Traceback (most recent call last):
File "C:\Users\matr06586\Desktop\ETLstage\Python\blahblah.py", line 90, in <module>
x = 1 / 0
ZeroDivisionError: integer division or modulo by zero
Run Code Online (Sandbox Code Playgroud)
如何使用空格和换行符最好地处理或格式化回溯?这些消息是logging.exception()中的一部分,但是当我尝试记录异常实例时,绕过函数感觉很奇怪.如何记录我的回溯并将其格式化?是否应该忽略追溯?
感谢您的时间!
Vin*_*jip 12
您可以定义自己Formatter的方法,您可以覆盖这些方法以根据您的需要格式化异常信息.这是一个简单(但有效)的例子:
import logging
class OneLineExceptionFormatter(logging.Formatter):
def formatException(self, exc_info):
result = super(OneLineExceptionFormatter, self).formatException(exc_info)
return repr(result) # or format into one line however you want to
def format(self, record):
s = super(OneLineExceptionFormatter, self).format(record)
if record.exc_text:
s = s.replace('\n', '') + '|'
return s
fh = logging.FileHandler('output.txt', 'w')
f = OneLineExceptionFormatter('%(asctime)s|%(levelname)s|%(message)s|', '%m/%d/%Y %I:%M:%S %p')
fh.setFormatter(f)
root = logging.getLogger()
root.setLevel(logging.DEBUG)
root.addHandler(fh)
logging.info('Sample message')
try:
x = 1 / 0
except ZeroDivisionError as e:
logging.exception('ZeroDivisionError: {0}'.format(e))
Run Code Online (Sandbox Code Playgroud)
这只产生两行:
01/28/2015 07:28:27 AM|INFO|Sample message|
01/28/2015 07:28:27 AM|ERROR|ZeroDivisionError: integer division or modulo by zero|'Traceback (most recent call last):\n File "logtest2.py", line 23, in <module>\n x = 1 / 0\nZeroDivisionError: integer division or modulo by zero'|
Run Code Online (Sandbox Code Playgroud)
当然,您可以在此示例的基础上进行精确的操作,例如通过traceback模块.
对于我的用例,Vinay Sajip 的代码运行得不够好(我使用了更复杂的消息格式),所以我想出了这个(对我来说它也更干净):
class OneLineExceptionFormatter(logging.Formatter):
def format(self, record):
if record.exc_info:
# Replace record.msg with the string representation of the message
# use repr() to prevent printing it to multiple lines
record.msg = repr(super().formatException(record.exc_info))
record.exc_info = None
record.exc_text = None
result = super().format(record)
return result
Run Code Online (Sandbox Code Playgroud)
因此,此 format() 方法可以检测到将要记录的异常,并且可以将其转换为其字符串表示形式,并且日志消息的格式仅针对该纯消息字符串进行。我在python 3中对其进行了测试。