所有, 在 uwsgi 后面运行 Flask 应用程序时,我无法记录 INFO 和 DEBUG 消息。不仅没有记录调试和信息消息,而且格式也不起作用。
formatter = logging.Formatter( # pylint: disable=invalid-name
'%(asctime)s %(levelname)s %(process)d ---- %(threadName)s '
'%(module)s : %(funcName)s {%(pathname)s:%(lineno)d} %
(message)s','%Y-%m-%dT%H:%M:%SZ')
handler = StreamHandler()
handler.setLevel(logging.DEBUG)
handler.setFormatter(formatter)
application.logger.addHandler(handler)
logging.debug('Debug Message')
logging.info('Info Message')
logging.warning('Warning Message')
logging.error('Error Message')
logging.critical('Critical ')
uwsgi日志文件中的输出是:
*** Operational MODE: preforking ***
WARNING:root:Warning Message
ERROR:root:Error Message
CRITICAL:root:Critical
WSGI app 0 (mountpoint='') ready in 2 seconds on interpreter 0x26f39b0 pid: 9574 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 9574)
从上面可以看到,仅记录错误、警告和严重消息。并且格式化不起作用。 (在 werkzeug 本地运行时一切正常)。
uwsgi配置文件如下:
[uwsgi]
project = myproject
uid = nginx
group = nginx
base = /srv
req-logger = file:/var/log/uwsgi/access.log
logger = file:/var/log/uwsgi/error.log
chdir = %(base)/www
home = %(base)/env
module = application
callable = application
master = true
processes = 4
socket = /var/run/uwsgi/%(project).sock
chown-socket = %(uid):%(group)
chmod-socket = 664
vacuum = true
我检查了 StackOverflow 上的所有解决方案,但没有一个有效。
非常感谢任何帮助。
通过访问
application.logger.addHandler(handler)
,您初始化了 Flask 的记录器。但是,在您使用 root
记录器和 logging.debug('Debug Message')
,而不是应用程序记录器和 application.logger.debug('Debug Message')
之后的行中。
还有,
app.logger.setLevel(logging.DEBUG)
。
完整示例:
from flask import Flask
app = Flask(__name__)
formatter = logging.Formatter( # pylint: disable=invalid-name
'%(asctime)s %(levelname)s %(process)d ---- %(threadName)s '
'%(module)s : %(funcName)s {%(pathname)s:%(lineno)d} %(message)s','%Y-%m-%dT%H:%M:%SZ')
handler = StreamHandler()
handler.setFormatter(formatter)
app.logger.setLevel(logging.DEBUG)
app.logger.addHandler(handler)
app.logger.removeHandler(default_handler)
app.logger.debug('Debug Message')
app.logger.info('Info Message')
app.logger.warning('Warning Message')
app.logger.error('Error Message')
app.logger.critical('Critical ')
打印:
...
2020-10-26T14:13:27Z DEBUG 3197 ---- MainThread test_log : <module> {test_log.py:77} Debug Message
2020-10-26T14:13:27Z INFO 3197 ---- MainThread test_log : <module> {test_log.py:78} Info Message
2020-10-26T14:13:27Z WARNING 3197 ---- MainThread test_log : <module> {test_log.py:79} Warning Message
2020-10-26T14:13:27Z ERROR 3197 ---- MainThread test_log : <module> {test_log.py:80} Error Message
2020-10-26T14:13:27Z CRITICAL 3197 ---- MainThread test_log : <module> {test_log.py:81} Critical
...
对我来说有效:
应用程序创建内部:
gunicorn_error_logger = logging.getLogger('gunicorn.error')
app.logger.handlers.extend(gunicorn_error_logger.handlers)
app.logger.setLevel(logging.DEBUG)
app.logger.debug('initialize gunicorn app logger')
在路线内:
from flask import current_app
@main.route("/")
def home():
current_app.logger.debug("THIS WILL BE IN THE GUNICORN LOG")
请将您的 Flask 版本更新至 1.0.2。这是一个使用 uwsgi-emperor 和 nginx 背后的 app.logger 的小例子:
def create_app(**kwargs):
app = Flask(__name__)
app.logger.setLevel(logging.DEBUG)
@app.route('/ping/', methods=['GET'])
def ping_pong():
app.logger.critical('Healthcheck called')
app.logger.error('Healthcheck called')
app.logger.warning('Healthcheck called')
app.logger.info('Healthcheck called')
app.logger.debug('Healthcheck called')
return jsonify({
'status': 'Epic success',
'message': 'pong!'
})
所有日志消息均显示在系统日志中:
[2018-05-14 08:43:33,778] CRITICAL in __init__: Healthcheck called
[2018-05-14 08:43:33,779] ERROR in __init__: Healthcheck called
[2018-05-14 08:43:33,779] WARNING in __init__: Healthcheck called
[2018-05-14 08:43:33,779] INFO in __init__: Healthcheck called
[2018-05-14 08:43:33,779] DEBUG in __init__: Healthcheck called
希望有帮助。