Django在格式化程序中记录自定义属性 [英] Django logging custom attributes in formatter

查看:95
本文介绍了Django在格式化程序中记录自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django如何使用日志记录来记录格式化程序中的自定义属性?我正在考虑例如登录的用户名.

How can Django use logging to log using custom attributes in the formatter? I'm thinking of logging the logged in username for example.

settings.py脚本中,定义了LOGGING变量:

In the settings.py script, the LOGGING variable is defined:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        },
    },
    'formatters' : {
        'info_format' : {
            'format' : '%(asctime)s %(levelname)s - %(message)s',
        },
    }
}

我希望使用一种格式,例如:

I wish to use a format, something like:

'format' : '%(asctime).19s %(levelname)s - %(username)s: %(message)s'

其中username是当前登录的用户.也许可以在这里添加任何其他类型的会话变量.

Where username would be the currently logged in user. Maybe any other kind of session's variables may be added here.

一种解决方法是在记录器方法上使用extra参数,该参数会接收一个字典,其中包含键,作为我要在格式字符串上使用的字符串:

A workaround here is to use the extra parameter on the logger methods, which receives a dictionary with the keys as the strings I want to use on the format string:

logger.info(message, extra={'username' : request.user.username})

另一种(丑陋的)解决方法是构建消息属性,以包含不属于日志记录格式化程序默认属性的部分.

Another (ugly) workaround would be to build the message attribute to include the things that are not part of the default attributes that logging formatters have.

message = request.user.username + " - " + message
logger.info(message)

但是,有没有一种方法来设置具有某些属性的格式字符串,并使Django自动将其提供给日志记录API?例如,如果%(username)是其他用户的request.user.username ...

But, is there a way to set up the format string with certain attributes and make Django give them automatically to the logging API? If %(username)s for example, the request.user.username, of any others perhaps...

推荐答案

您可以使用过滤器添加自定义属性.例如:

You can use a filter to add your custom attribute. For example :

def add_my_custom_attribute(record):
    record.myAttribute = 'myValue'
    record.username = record.request.user.username 
    return True

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        ...
        'add_my_custom_attribute': {
            '()': 'django.utils.log.CallbackFilter',
            'callback': add_my_custom_attribute,
        }
    },
    'handlers': {
        ...
        'django.server': {
            'level': 'INFO',
            'class': 'logging.StreamHandler',
            'filters': ['add_my_custom_attribute'],
            'formatter': 'django.server',
        },            
    },
    ...
}


通过安装过滤器,您可以处理每个日志记录,并确定是否应将其从记录器传递到处理程序.


By installing a filter, you can process each log record and decide whether it should be passed from logger to handler.

过滤器获取包含所有日志详细信息(即:时间,严重性,请求,状态代码)的日志记录.

The filter get the log record which contains all the details of log (i.e : time, severity, request, status code).

格式化程序使用记录的属性将其格式化为字符串消息.如果您将自定义属性添加到该记录-格式化程序也可以使用它们.

The attributes of the record are used by the formatter to format it to string message. If you add your custom attributes to that record - they will also be available to the formatter.

这篇关于Django在格式化程序中记录自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆