我正在尝试获取在本地计算机上的终端模拟器上看到的默认日志,该日志运行良好。但是,在 centos 服务器上,当我重新启动 Gunicorn 服务时,我无法将输出输出到日志文件。我想要的日志如下所示:我的解决方法是在虚拟环境中获取 shell 上的日志:
[2023-10-21 18:01:38,550] INFO in _internal: * Restarting with stat
[2023-10-21 18:01:39,049] WARNING in _internal: * Debugger is active!
[2023-10-21 18:01:39,052] INFO in _internal: * Debugger PIN: 691-655-964
[2023-10-21 18:58:06,111] INFO in _internal: [31m[1mWARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.[0m
* Running on all addresses (0.0.0.0)
* Running on http://127.0.0.1:5000
* Running on http://10.200.16.168:5000
[2023-10-21 18:58:06,111] INFO in _internal: [33mPress CTRL+C to quit[0m
[2023-10-21 18:58:06,111] INFO in _internal: * Restarting with stat
[2023-10-21 18:58:06,691] WARNING in _internal: * Debugger is active!
[2023-10-21 18:58:06,707] INFO in _internal: * Debugger PIN: 691-655-964
[2023-10-21 19:01:45,844] INFO in _internal: 127.0.0.1 - - [21/Oct/2023 19:01:45] "GET / HTTP/1.1" 200 -
[2023-10-21 19:01:45,917] INFO in _internal: 127.0.0.1 - - [21/Oct/2023 19:01:45] "[36mGET /static/media/Ericsson_logo.png HTTP/1.1[0m" 304 -
[2023-10-21 19:01:46,068] INFO in _internal: 127.0.0.1 - - [21/Oct/2023 19:01:46] "[36mGET /static/jqueryAdd.js HTTP/1.1[0m" 304 -
这是我在服务器中的配置:
import os
import logging
import logging.handlers
from flask import Flask, redirect, url_for, render_template, request, session, flash
from datetime import timedelta
from logging.config import dictConfig
dictConfig({
'version': 1,
'handlers': {
'file.handler': {
'class': 'logging.handlers.RotatingFileHandler',
'filename': 'server_werkzeug.log',
'maxBytes': 10000000,
'backupCount': 5,
'level': 'DEBUG',
},
},
'loggers': {
'werkzeug': {
'level': 'INFO',
'handlers': ['file.handler'],
},
},
})
#logging.basicConfig(filename='flask_record.log', level=logging.INFO, format=f'%(asctime)s %(levelname)s %(name)s %(threadName)s : %(message)s')
app = Flask(__name__)```
如果仍然相关:
werkzeug(Flask 使用的 WSGI Web 应用程序库)有一个在开发期间运行的开发服务器。该服务器生成访问日志。使用(例如 Gunicorn)部署到生产时,不使用此开发服务器,并且不会生成任何日志。
我认为你必须自己实现请求日志记录。也许这些链接会有所帮助: