2 回答
![?](http://img1.sycdn.imooc.com/5458477f0001cabd02200220-100-100.jpg)
TA贡献1824条经验 获得超5个赞
您最终可以使用回溯模块:
import traceback
def your_method(param):
print(traceback.format_stack()) # the stacktrace formatted
print(traceback.extract_stack()) # the stacktrace as a list, you could only get the last item
your_method(1)
您可以通过以下方式转换您的 debug() 方法来实现这一点:
def debug(self, message):
logging.debug('%s - %s', traceback.extract_stack()[-1], message)
注意:您不需要basicConfig每次调用 debug() 方法时都调用该方法,只需在您的记录器实例化时调用它即可;在此处查看有关日志记录良好做法的更多信息:https : //realpython.com/python-logging/
![?](http://img1.sycdn.imooc.com/533e4c9c0001975102200220-100-100.jpg)
TA贡献1765条经验 获得超5个赞
根据LogRecord 属性部分中的日志记录 Python 官方模块,并为了获得预期的日志输出:
2019 年 3 月 19 日,星期二 05:41:15 DEBUG file1 is_path_valid #line_number 输入参数 dc_path: /disks
您的格式日志记录属性应如下所示:
'%(asctime)-15s %(levelname)s %(module)s %(filename)s %(lineno)d %(message)s'
请注意从funcName
到的更改,filename
因为您要查找的是文件名,并且您还将获得:路径名的文件名部分。
添加回答
举报