如何使用包装器 class/ 装饰器在 python django 中传递错误记录参数并将其用于每个方法?

How to pass error logging parameters in python django using a wrapper class/ decorator and use it for every method?

我正在使用 Django-DB-logger 在每个 method.I 中捕获我的错误日志有多个日志参数----> user, class_name, method_name, module_name、ip_address、process_time。我需要从每种方法获取日志。下面给出了我使用日志记录参数的示例方法。

            import logging
            import sys
            import socket
            import time
            from django.shortcuts import render
            from django.urls import resolve
            from rest_framework import status, request
            from rest_framework.decorators import api_view
            from rest_framework.response import Response
            from rest_framework.views import APIView

            class Request(APIView):

                @api_view(['GET'])
                def test_api_method(self):
                    startTime = time.time()
                    print('This is Test API')
                    db_logger = logging.getLogger('db')
                    try:
                        1 / 0
                    except Exception as e:
                        endTime = time.time()
                        db_logger.exception(e, extra={'user': self.user.username, 
                                                    'class_name': self.__class__.__name__,
                                                    'method_name': sys._getframe().f_code.co_name,
                                                    'module_name': __package__, 
                                                    'ip_address': socket.gethostbyname(socket.gethostname())
                                                    'process_time': endTime - startTime})

                    return Response({'data': True}, status=status.HTTP_200_OK)

我不想像上面那样在每个方法中使用所有参数,而是想使用装饰器/包装器 class(在一个单独的 .py 文件中)这样我每次都可以调用那个装饰器来捕获不同 .py 文件中所有方法的异常处理期间的参数。 请尽快回复。

我得到了另一种方法。

customdblogger_views.py

            import getpass
            import socket
            import time
            import logging

            class logEngine:

                def logger(request):
                    t1 = time.time()
                    params = {'user': request.user.username,
                              'ip_address': socket.gethostbyname(socket.gethostname()),
                              'process_time': time.time() - t1,
                              }
                    return params

                def django_logger(self):
                    django_logger = logging.getLogger('db')
                    return django_logger


            attributes = logEngine().logger()
            db_logger = logEngine().django_logger()
            
            
            
            
            
            
            
            

在这里创建您的观点。

            import sys

            from rest_framework import status
            from rest_framework.decorators import api_view
            from rest_framework.response import Response
            from rest_framework.views import APIView
            from CustomDBLogger.customdblogger_views import attributes, db_logger


            class Request(APIView):

                @api_view(['GET'])
                def test_api_method(self):

                    params2 = {'class_name': __class__.__name__,
                               'method_name': sys._getframe().f_code.co_name,
                               'module_name': __name__,
                               }
                    attributes.update(params2)

                    db_logger.info("Successfully entered into function", extra=attributes)

                    try:
                        1 / 0
                    except Exception as e:
                        db_logger.exception(e, extra=attributes)
                        return Response({'data': True}, status=status.HTTP_200_OK)
                    finally:
                        db_logger.info("exited from the function", extra=attributes)

                @api_view(['GET'])
                def test_logger(self):
                    params2 = {'class_name': __class__.__name__,
                               'method_name': sys._getframe().f_code.co_name,
                               'module_name': __name__,
                               }
                    attributes.update(params2)
                    db_logger.info("Successfully entered test_logger into function", extra=attributes)
                    try:
                        '2' + 2
                    except Exception as e:
                        db_logger.exception(e, extra=attributes)
                        return Response({'data': True}, status=status.HTTP_200_OK)