django日志——django。请求日志记录器和附加上下文

时间:2021-02-02 22:04:10

Am on django 1.3., python 2.6

django 1.3。python 2.6

In the django docs here
https://docs.djangoproject.com/en/1.3/topics/logging/#django-request
it says that messages have the following extra context: status and request.
How do you get these to show up in the debug file? i tried in my logging config something like:

在django文档中,它说消息有以下额外的上下文:状态和请求。如何在调试文件中显示这些内容?我在我的日志记录配置中试过:

'formatters': {        
        'simple_debug': {
            'format': '[%(asctime)s] %(levelname)s %(module)s %(message)s %(request.user)s',        
        }
    },

but that causes the overall logging to fail (i.e. no logging output happens)

但是这会导致整个日志记录失败(也就是说,不会发生日志输出)


EDIT: So immediately after submitting the question i came across this: http://groups.google.com/group/django-users/browse_thread/thread/af682beb1e4af7f6/ace3338348e92a21

编辑:在提交了这个问题之后,我发现了这个:http://groups.google.com/group/django- users/browse_thread/af682beb1e4af7f6 /ace3338348e92a21

can someone help explain/elaborate on

有人能解释一下吗

All the quote from the docs really means is that all the places inside of django where django.request is used, the request is explicitly passed in as part of extra.

来自文档的所有引用都意味着django内部的所有地方。请求被使用,请求被显式地作为额外的一部分传入。

where is request explicitly passed in as part of extra to?

请求作为额外的一部分显式地传递到哪里?

2 个解决方案

#1


20  

You can't use request.user in the format string, as %-formatting doesn't handle that. You could use a format string such as

你不能使用的要求。格式字符串中的用户,%-格式不处理。您可以使用格式字符串,例如

'[%(asctime)s] %(levelname)s %(module)s %(message)s %(user)s'

and, in your logging call, use something like

在日志记录调用中,使用类似的方法

logger.debug('My message with %s', 'args', extra={'user': request.user})

The extra dict is merged into the logging event record, which ends up with a user attribute, and this then gets picked up through the format string and appears in the log.

附加的dict语句被合并到日志事件记录中,日志记录以一个user属性结束,然后通过格式字符串获取并显示在日志中。

If using the django.request logger, the status_code and the request will be passed in the extra dict by Django. If you need the request.user, you'll probably need to add a logging.Filter which does something like:

如果使用django。请求日志程序、status_code和请求将在Django的附加命令中传递。如果你需要的话。用户,您可能需要添加日志记录。过滤器的功能如下:

class RequestUserFilter(logging.Filter):
    def filter(self, record):
        record.user = record.request.user
        return True

so that you can show the user in the formatted output.

这样就可以在格式化输出中显示用户。

#2


0  

The django-requestlogging middleware plugin makes it easy to log request-related information without adjusting all your logging calls to add the request with the extra parameter. It's then just a matter of configuring your loggers.

django-requestlogging中间件插件可以轻松地记录与请求相关的信息,而无需调整所有日志调用来添加带有额外参数的请求。这只是一个配置你的记录器的问题。

The following items can be logged when using django-requestlogging:

使用django-requestlogging时可以记录以下内容:

  • username
  • 用户名
  • http_user_agent
  • http_user_agent
  • path_info
  • path_info
  • remote_add
  • remote_add
  • request_method
  • request_method
  • server_protocol
  • server_protocol

#1


20  

You can't use request.user in the format string, as %-formatting doesn't handle that. You could use a format string such as

你不能使用的要求。格式字符串中的用户,%-格式不处理。您可以使用格式字符串,例如

'[%(asctime)s] %(levelname)s %(module)s %(message)s %(user)s'

and, in your logging call, use something like

在日志记录调用中,使用类似的方法

logger.debug('My message with %s', 'args', extra={'user': request.user})

The extra dict is merged into the logging event record, which ends up with a user attribute, and this then gets picked up through the format string and appears in the log.

附加的dict语句被合并到日志事件记录中,日志记录以一个user属性结束,然后通过格式字符串获取并显示在日志中。

If using the django.request logger, the status_code and the request will be passed in the extra dict by Django. If you need the request.user, you'll probably need to add a logging.Filter which does something like:

如果使用django。请求日志程序、status_code和请求将在Django的附加命令中传递。如果你需要的话。用户,您可能需要添加日志记录。过滤器的功能如下:

class RequestUserFilter(logging.Filter):
    def filter(self, record):
        record.user = record.request.user
        return True

so that you can show the user in the formatted output.

这样就可以在格式化输出中显示用户。

#2


0  

The django-requestlogging middleware plugin makes it easy to log request-related information without adjusting all your logging calls to add the request with the extra parameter. It's then just a matter of configuring your loggers.

django-requestlogging中间件插件可以轻松地记录与请求相关的信息,而无需调整所有日志调用来添加带有额外参数的请求。这只是一个配置你的记录器的问题。

The following items can be logged when using django-requestlogging:

使用django-requestlogging时可以记录以下内容:

  • username
  • 用户名
  • http_user_agent
  • http_user_agent
  • path_info
  • path_info
  • remote_add
  • remote_add
  • request_method
  • request_method
  • server_protocol
  • server_protocol