简体   繁体   English

在带有 Connexion 的 Python Flask 应用程序中使用哪个记录器

[英]Which logger to use in a Python Flask app with Connexion

I'm using both Flask and Connexion for a Python based REST API, and it runs within a Docker container.我将 Flask 和 Connexion 用于基于 Python 的 REST API,并且它在 Docker 容器中运行。 Here is main.py :这是main.py

import connexion
import logging
from app.log import handler

# initiate swagger/connexion
application = connexion.App(__name__, specification_dir='./')
application.add_api('swagger.yml')

# logging
application.app.logger.handlers.clear()
application.app.logger.addHandler(handler)
application.app.logger.setLevel(logging.DEBUG)
application.app.logger.debug('application starting...')

# if we're running in standalone mode, run the application
if __name__ == '__main__':
    application.run(host='0.0.0.0', port=5000, debug=True)

This works fine, and in my syslog server I can see:这工作正常,在我的系统日志服务器中我可以看到:

2020-01-14 11:03:14,951 app main:DEBUG application starting...

However, I'm not sure how to log correctly from files outside of main.py .但是,我不确定如何从main.py之外的文件中正确main.py For example, I have a status.py which has a single route for GET /status and the code looks like:例如,我有一个status.py ,它有一个用于GET /status路由,代码如下所示:

import yaml
from flask import current_app
import logging

def read():

    # LOG TESTING
    current_app.logger.debug('Test using current_app')
    logging.getLogger(__name__).debug('Test using getLogger')
    print('Test using print')

    with open('./swagger.yml', 'r') as f:
        y = yaml.load(f)

    return {
        # .... some data here
    }

In my syslog server, I can see:在我的系统日志服务器中,我可以看到:

Test using print
./status.py:22: YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details.
  y = yaml.load(f)

I would like to use the same logging mechanism that main.py is using in all of my separate files, but I can only get it to work from main.py , and the only thing that works outside of main.py is the print function, however, as can be seen above, errors also seem to get picked up (albeit with no timestamp).我想使用main.py在我的所有单独文件中使用的相同日志记录机制,但我只能让它从main.py工作,并且唯一在main.py之外工作的是打印功能,然而,如上所示,错误似乎也被发现(尽管没有时间戳)。

Please review the docs here.请在此处查看文档。 https://flask.palletsprojects.com/en/1.1.x/logging/ You are changing the logging after calling app.log or is it app.logger (I forget) so the application has already started. https://flask.palletsprojects.com/en/1.1.x/logging/您是在调用app.log后更改日志记录还是app.logger (我忘记了)所以应用程序已经启动。 You need to override the default.您需要覆盖默认值。 The document covers it but here is a gist.该文件涵盖了它,但这里有一个要点。

Before you instantiate the Flask App.在实例化 Flask 应用程序之前。 do this做这个

from logging.config import dictConfig

dictConfig({
    'version': 1,
    'formatters': {'default': {
        'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s',
    }},
    'handlers': {'wsgi': {
        'class': 'logging.StreamHandler',
        'stream': 'ext://flask.logging.wsgi_errors_stream',
        'formatter': 'default'
    }},
    'root': {
        'level': 'INFO',
        'handlers': ['wsgi']
    }
})

app = Flask(__name__) # important!  logging stuff is set before this.

One thing to note is that error from web request get logged differently than the errors outside of web requests (for eg jobs, cli etc).需要注意的一件事是,来自 Web 请求的错误与 Web 请求之外的错误(例如作业、cli 等)的记录方式不同。 The default behavior is to log to standard error which in your case is syslog默认行为是记录到标准错误,在您的情况下是syslog

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM