首页 > 解决方案 > gunicorn - 未知日志输出到控制台

问题描述

我正在用 gunicorn 和烧瓶运行我的网络服务。我将日志输出设置为日志文件,但在控制台中发现了一条未知的日志记录:
[2021-04-19 14:26:42 +0800] [1717]: [INFO] POST http://127.0.0.1:9200/papers/_search [status:200 request:1.347s]
我在服务中使用了elasticsearch,这是发送到es服务器的请求。我希望将此信息打印在一个文件中,并尝试了一些方法,但都没有奏效。这是我的独角兽设置:

bind = "0.0.0.0:5001"
workers = 4
threads = 4
proc_name = "app"
logconfig_dict = {
    'version':1,
    'disable_existing_loggers': False,
    "root": {
          "level": "INFO",
          "propagate": True,
          "handlers": ["console"]
    },
    'loggers':{
        "gunicorn.error": {
            "level": "INFO",
            "handlers": ["error_file"], 
            "propagate": False, 
            "qualname": "gunicorn_error"
        },
        "gunicorn.access": {
            "level": "INFO",
            "handlers": ["access_file"],
            "propagate": False,
            "qualname": "access"
        }
    },
    'handlers':{
        "error_file": {
            "class": "logging.handlers.RotatingFileHandler",
            "maxBytes": 1024*1024*100,
            "backupCount": 1,
            "formatter": "generic",
            "filename": "/home/wh/SearchEngine-test/log_file/gunicorn_error.log"
        },
        "access_file": {
            "class": "logging.handlers.RotatingFileHandler",
            "maxBytes": 1024*1024*100,
            "backupCount": 1,
            "formatter": "generic",
            "filename": "/home/wh/SearchEngine-test/log_file/gunicorn_access.log",
        },
        'console': {
            'class': 'logging.StreamHandler',
            #"maxBytes": 1024*1024*100,
            #"backupCount": 1,
            'level': 'INFO',
            'formatter': 'generic',
            #"filename": "/home/wh/SearchEngine-test/log_file/gunicorn_console.log"
        },
    },
    'formatters':{
        "generic": {
            "format": "%(asctime)s [%(process)d]: [%(levelname)s] %(message)s",
            "datefmt": "[%Y-%m-%d %H:%M:%S %z]",
            "class": "logging.Formatter"
        }
    }
}

更重要的是,当我使用命令运行 gunicorn 时:
gunicorn -w 4 --threads 4 -b 0.0.0.0:5001 main:app
我不再获得此记录。
谁能帮帮我?
谢谢!

标签: pythonflasklogginggunicorn

解决方案


如果要将错误写入日志文件:

--error-logfile yourfilename or --log-file yourfilename

对于您的访问日志:

--access-logfile yourfilename

有关更多信息,请查看文档。


推荐阅读