python-3.x - 如何使用外部函数/包装器在 python 中的错误/信息日志中捕获额外属性?
问题描述
startTime = time.time()
params = {'user': request.user.username,
'class_name': request.__class__.__name__,
'method_name': sys._getframe().f_code.co_name,
'module_name': __name__,
'ip_address': socket.gethostbyname(socket.gethostname()),
'process_time': time.time() - startTime}
我需要捕获我的方法的错误日志/信息日志,并将上述参数传递给该方法,以捕获我正在捕获日志的特定方法的额外属性(用户类名称方法名称模块名称 IP 地址进程时间)。下面给出的是我要捕获日志的主要方法。
db_logger = logging.getLogger('db')
class Request(APIView):
@api_view(['GET'])
def test_api_method(request):
db_logger.info("Entering into function", params)
""""params is the extra attributes that I have mentioned above. I need to pass like this"""""
try:
1 / 0
except Exception as e:
db_logger.exception(e, params)
return Response({'data': True}, status=status.HTTP_200_OK)
可以使用装饰器或任何常用函数传递参数。请查看此内容并建议您的代码。
解决方案
推荐阅读
- ios - 存储 UICollectionViewCell 状态
- c# - 在标记范围和未标记范围内解析组件
- scala - 在使用 Semigroupal 的某些 monad 中,我们仍然会失去上下文吗?
- ruby - 我应该如何在 Ruby 中继承变量?
- sql-server - 使用 DDNS 和端口转发的 Google Apps 脚本 JDBC(特别是 sqlserver)连接问题
- java - 从 servlet 中的单选按钮值获取 null
- r - R语言中两个矩阵相乘的错误
- php - 如何在变体形式下拉列表中显示产品变体图像?
- python - 如何在标头中传递基本身份验证 base 64 密钥
- python-3.x - 如何使用 ReportLab 在 PDF 中定位表格?