简体   繁体   English

如何将额外的参数传递给 django 日志格式化程序

[英]How to pass extra arguments to the django log formatter

I am trying to do the following:我正在尝试执行以下操作:

    # settings.py
    'json': {
        'format': 'Avails: {"levelname": "%(levelname)s", "asctime": "%(asctime)s", "funcName": "%(funcName)s", "filename": "%(filename)s", "lineno": "%(lineno)s", "message": "%(message)s"}',
    },

And in the view:在视图中:

log.info('this is my message', extra={'user': request.user}

How would I grab the extra info in the log formatter?我将如何在日志格式化程序中获取extra信息?

The user in extra can be added in the logging json formatter as extra的用户可以添加到日志记录 json 格式化程序中

'json': {
    'format': (
        'Avails: {'
            '"levelname": "%(levelname)s",'
            '"asctime": "%(asctime)s",'
            '"funcName": "%(funcName)s",'
            '"filename": "%(filename)s",'
            '"lineno": "%(lineno)s",'
            '"message": "%(message)s",' 
            '"user": "%(user)s"'
        '}',
    )
},

Note that you need to pass a string value for user instead of the user object in the extra options.请注意,您需要为用户传递一个字符串值,而不是在额外选项中传递用户对象。 When this is not a string object, the __repr__ or __str__ value of the object passed replaces the corresponding conversion specification in the format string.当这不是字符串对象时,传递的对象的__repr____str__值将替换格式字符串中的相应转换规范。

log.info('this was your message :D', extra={'user': request.user.pk}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM