【发布时间】:2017-10-04 18:39:59
【问题描述】:
我在我的 django 项目中使用日志记录来记录错误。但是,只要在任何行发生任何错误,日志中的行号就是记录错误的位置,而不是发生错误的位置。
例如,在下面的代码中,如果错误发生在第 3 行(获取数据行 1 的代码),那么在日志中的行号将打印为 7,其中发生了日志记录。
def get_gender():
try:
# code to get data line 1
# code to get data line 2
# return data dictionary
except Exception as e:
logging.getLogger("error_logger").error(repr(e))
return {}
以下是日志设置文件中的格式化代码。
formatters':{
'large':{
'format':'%(asctime)s %(levelname)s %(process)d %(pathname)s '+
'%(funcName)s %(lineno)d %(message)s '
},
错误信息示例:
2017-05-06 15:20:07,065 ERROR 7000 /home/USER/ASonline/common/services/category_services.py get_gender 7 IndexError('list index out of range',)
如何获取发生错误的行号,而不是记录错误的行号。
【问题讨论】:
标签: django error-logging