带有用户信息的Django记录器 [英] Django Logger with User info

查看:100
本文介绍了带有用户信息的Django记录器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇.是否可以在setting.pyLOGGING配置的formatters信息中放入User信息?

I'm just curious. Is it possible to put User info within the formatters info in LOGGING config in setting.py?

现在,我只是将该信息放在消息中以进行日志记录,但是也许有一种方法可以在formatters参数中进行设置.

Right now I just put that info in the message to log but maybe there's a way to set it in formatters argument.

这是我现在的LOGGING配置:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
        'simple': {
            'format': '[%(asctime)s] %(levelname)s [%(funcName)s] %(message)s'
        }
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': BASE_DIR + '/logs/uca_{:%d_%m_%Y}.log'.format(time.now()),
            'formatter': 'simple'
        }
    },
    'loggers': {
        'ucalog': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True
        }
    }
}

我在django的文档中没有看到任何类似的东西,我认为获取WHO所做的事情的记录将很有用.

I haven't seen anything similar in django's documentation and I think it would be usefull to get records of WHO did WHAT.

推荐答案

尝试一下:

'format': '[%(asctime)s] %(levelname)s [%(funcName)s] - %(username)s: %(message)s'

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

这篇关于带有用户信息的Django记录器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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