导入fbprophet之后整理烧瓶重复日志

时间:2019-06-05 13:37:36

标签: python logging flask facebook-prophet

我想在Flask应用中使用登录功能,只需在需要登录的每个文件中调用<!DOCTYPE html> <html> <head> <style> html,body, #Chart { height: 100%; min-height: 100%; width: 100%; min-width: 100%; } #Chart { display: inline-block; } #redmonest { font-size: 1.2em !important; font-weight: normal !important; color: white; background-color: #899E8E; text-align: center; } </style> <title>dex.js : Clustered Force</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.theme.min.css"> <link rel="stylesheet" href="https://dexjs.net/js/dex-jquery.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css"/> <link rel="stylesheet" href="https://dexjs.net/js/dex-bootstrap.css"> <link rel="stylesheet" href="/wp-content/themes/redmonest/css/dex.css"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> <script src="https://dexjs.net/js/dex-jquery.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script> <script src="https://dexjs.net/js/dex-bootstrap.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script> <script src="https://dexjs.net/js/dex-libs.js"></script> <script src="/wp-content/themes/redmonest/js/dex-redmonest-bb.js"></script> </head> <body> <div class="ui-layout-center"> <div id="Chart"></div> </div> <div class="ui-layout-west"> <div id="ConfigurationPane"></div> <br> <a href="http://redmonest.eu"><div class="redmonest" id="redmonest">Redmonest</div></a> <br> </div> <script> d3.csv("/wp-content/themes/redmonest/csv/databeton.csv", function (error, data) { var databeton = new dex.csv(data); $(document).ready(function () { $('body').layout({ applyDemoStyles: false, west: { size: 335 }, onresize: function () { chart.resize(); } }); $('body').layout({ applyDemoStyles: false, west: { size: 335 }, onresize: function () { chart.resize(); } }); var chart = dex.charts.d3.Sunburst({ 'parent': '#Chart', 'csv': databeton.include([0, 1, 2, 3]) }).render(); var configPane = dex.ui.ConfigurationPane({ "parent": "#ConfigurationPane", "csv": databeton.include([0, 1, 2, 3]), "components": [chart] }).render(); }); }); </script> </body> </html>

应该为该“全局”应用程序记录器定义处理程序和格式的单一位置。这行得通,但是Flask还在以默认格式连续记录自己的日志。这些日志仅在导入库logging.getLogger('myapi')时存在。我想防止Flask记录这些多余的,未格式化的重复日志。

(Flask也有一个fbprophet记录器,很好,可以保留。)

代码

werkzeug

输出:

我希望看到以import sys import logging import fbprophet from flask import Flask, jsonify from werkzeug.serving import run_simple # Set up my custom global logger log = logging.getLogger('myapi') handler = logging.StreamHandler(sys.stdout) handler.setFormatter(logging.Formatter('*** %(asctime)s %(levelname)s %(message)s')) log.addHandler(handler) log.setLevel(logging.DEBUG) def create_app(config={}): ''' Application factory to create and configure the app ''' app = Flask('myapi', instance_relative_config=False) app.config.from_mapping(config) log.info('TEST INFO') log.debug('TEST DEBUG') @app.route('/health') def health(): log.info('Health OK') return 'OK' return app if __name__ == '__main__': dev_config = {'SECRET_KEY': 'dev', 'DEBUG': False} app = create_app(dev_config) run_simple('localhost', 5000, app) 为前缀的日志。以***开头的那些仅在我导入Facebook先知时出现。

LEVEL

更多说明:

我也尝试设置应用程序的记录器,但是我不想从其他模块调用* Serving Flask app "main.py" (lazy loading) * Environment: development * Debug mode: on * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! *** 2019-06-05 14:17:56,702 INFO TEST INFO # good log INFO:myapi:TEST INFO # bad dupe log *** 2019-06-05 14:17:56,702 DEBUG TEST DEBUG DEBUG:myapi:TEST DEBUG *** 2019-06-05 14:17:56,703 INFO TEST INFO INFO:myapi:TEST INFO *** 2019-06-05 14:17:56,703 DEBUG TEST DEBUG DEBUG:myapi:TEST DEBUG *** 2019-06-05 14:18:10,405 INFO Health OK INFO:myapi:Health OK 127.0.0.1 - - [05/Jun/2019 14:18:10] "GET /health HTTP/1.1" 200 - # good werkzeug api log INFO:werkzeug:127.0.0.1 - - [05/Jun/2019 14:18:10] "GET /health HTTP/1.1" 200 - # bad dupe log

我尝试用current_app.logger禁用Flask的记录器,但这还是行不通的。

导入logging.getLogger('flask.app').handlers.clear()时,出现以下控制台错误(来自先知):

fbprophet

我认为这可能是导致问题的原因,因此我更正了错误following this。但是Flask仍在记录多余的日志。

ERROR:fbprophet:Importing matplotlib failed. Plotting will not work.
ERROR:fbprophet:Importing matplotlib failed. Plotting will not work.
*** 2019-06-05 14:29:06,488 INFO TEST INFO
INFO:myapi:TEST INFO

摘要:

在Flask中查找格式化的全局日志,没有重复的日志。我只需要全局import plotly import matplotlib as mpl mpl.use('TkAgg') import fbprophet logging.getLogger('myapi') API日志。

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,花了数小时来解决。实际上,它甚至与Flask都不相关(几个小时后我就知道了)。

即使在简单的脚本中,处理程序中也会有重复的日志。

唯一可行的解​​决方案似乎是在您自己的处理程序中添加logger.propagate = False。这样可以防止将日志传递给高级(祖先)记录器的处理程序,即由先知创建的记录器(即使我看不到该层次结构的确切位置)。

在以下答案中找到:https://stackoverflow.com/a/55877763/3615718

答案 1 :(得分:0)

您可以搜索所有已注册的记录器,并根据需要更改其设置。听起来fbprophet可能正在设置自己的logger实例,所以希望执行以下操作时它将被设置为所需的级别:

for logger_name in logging.root.manager.loggerDict:
    print(f"found a logger:{logger_name}")
    logger = logging.getLogger(logger_name)
    logger.setLevel(logging.ERROR)

    if logger_name == 'myapi':
        logger.setLevel(logging.INFO)