python - django记录访问到日志文件中
PHP中文网
PHP中文网 2017-04-18 09:16:20
0
1
298

我需要把用户的日志访问日志到文件中,我在setting.py中做了以下处理。

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
        'log': {
            'format': '%(levelname)s %(asctime)s %(message)s'
        }
    },
    'handlers': {
        'null': {
            'level': 'DEBUG',
            'class': 'logging.NullHandler',
        },
        'console': {
            'class': 'logging.StreamHandler',
        },
        'file': {
            'level': 'INFO',
            'class': 'api.Handler.FileHandler', #自定义的类
            'filename': '/var/log/django.log',
            'formatter': 'ndautolog'
        }
    },
    'loggers': {
        #default
        '': {
            'handlers': ['console'],
            'level': 'DEBUG',
        },
        'django': {
            'handlers': ['console'],
            'propagate': True,
            'level': 'INFO',
        },
        'django.request': {
            'handlers': ['console', 'file'],
            'level': 'DEBUG',
            'propagate': True,
        },
    },
}

然后在日志中只获取到了warn或更高级别的错误:

WARNING 2016-07-29 15:38:09,901 Not Found: /web/xxxxxx/ post:{
    "check_cmd":"ps -ef |grep xxxxx.xxxx.com|grep nginx |grep -v grep",
    "cmd":"/usr/local/tomcat/xxxxxx.xxxx.com/startup/run.sh",
    "boss_key":"33333333333333333"
}
WARNING 2016-07-29 15:38:09,901 Not Found: /web/xxxxxx/ post:{
    "check_cmd":"ps -ef |grep xxxxx.xxxx.com|grep nginx |grep -v grep",
    "cmd":"/usr/local/tomcat/xxxxxx.xxxx.com/startup/run.sh",
    "boss_key":"33333333333333333"
}
WARNING 2016-07-29 15:38:09,901 Not Found: /web/xxxxxx/ post:{
    "check_cmd":"ps -ef |grep xxxxx.xxxx.com|grep nginx |grep -v grep",
    "cmd":"/usr/local/tomcat/xxxxxx.xxxx.com/startup/run.sh",
    "boss_key":"33333333333333333"
}

我试过在django''中加入'handlers': ['console', 'file'],,但保存的信息不是我要的:

INFO 2016-07-29 15:29:03,888 Scheduler started
DEBUG 2016-07-29 15:29:03,890 Looking for jobs to run
DEBUG 2016-07-29 15:29:03,890 No jobs; waiting until a job is added

这让人很苦恼。

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(1)
刘奇

In fact, you don’t need to leave the logging work to the project. Just leave it to nginx. Configure nginx’s access_log, and all access will be recorded.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template