gunicorn的log如何傳遞給django,由django管理


gunicorn配置文件為gunicorn_config.py里面有日志的配置

# errorlog = '/home/admin/output/erebus/logs/gunicorn_error.log'
# loglevel = 'info'
# loglevel = 'debug'
# accesslog = '/home/admin/output/erebus/logs/gunicorn_access.log'
# access_log_format = '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"'

django如何捕獲gunicorn的日志

注釋掉上面的配置,則相關日志不輸出,然后在django日志配置里添加

logconfig_dict = {
'version':1,
'disable_existing_loggers': False,
'loggers':{
    "gunicorn.error": {
        "level": "DEBUG",# 打日志的等級可以換的,下面的同理
        "handlers": ["error_file"], # 對應下面的鍵
        "propagate": 1,
        "qualname": "gunicorn.error"
    },

    "gunicorn.access": {
        "level": "DEBUG",
        "handlers": ["access_file"],
        "propagate": 0,
        "qualname": "gunicorn.access"
    }
},
'handlers':{
    "error_file": {
        "class": "logging.handlers.RotatingFileHandler",
        "maxBytes": 1024*1024*1024,# 打日志的大小,我這種寫法是1個G
        "backupCount": 1,# 備份多少份,經過測試,最少也要寫1,不然控制不住大小
        "formatter": "generic",# 對應下面的鍵
        # 'mode': 'w+',
        "filename": "/你要放日志的路徑/gunicorn.error.log"# 打日志的路徑
    },
    "access_file": {
        "class": "logging.handlers.RotatingFileHandler",
        "maxBytes": 1024*1024*1024,
        "backupCount": 1,
        "formatter": "generic",
        "filename": "/你要放日志的路徑/gunicorn.access.log",
    }
},
'formatters':{
    "generic": {
        "format": "'[%(process)d] [%(asctime)s] %(levelname)s [%(filename)s:%(lineno)s] %(message)s'", # 打日志的格式
        "datefmt": "[%Y-%m-%d %H:%M:%S %z]",# 時間顯示方法
        "class": "logging.Formatter"
    },
    "access": {
        "format": "'[%(process)d] [%(asctime)s] %(levelname)s [%(filename)s:%(lineno)s] %(message)s'",
        "class": "logging.Formatter"
    }
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM