【问题标题】:Plotly Dash: Use SMTP logging handler when debug is falsePlotly Dash:当调试为 false 时使用 SMTP 日志记录处理程序
【发布时间】:2022-01-10 08:29:10
【问题描述】:

在 Plotly Dash 中,我正在尝试

  1. 确定我是否在调试模式下运行,并且
  2. 仅当应用程序未在调试模式下运行时,才将日志处理程序更改为 SMTPHandler

我尝试了什么:

import dash

app = dash.Dash(__name__)

if app.server.debug is False:
    print("Not in Debug mode")
    # app.logger.addHandler(mail_handler)

if __name__ == '__main__':
    app.run_server(debug=True, use_reloader=True)
    print(f"app.server.debug is {app.server.debug}")  # This code only executes after the server is shut down

我试过app.server.debug(和app.server.config["DEBUG"]),但都返回False。所以我无法确定应用程序是否实际上处于调试模式。

这是我的控制台输出:

Not in Debug mode
Dash is running on http://127.0.0.1:8050/

 * Serving Flask app 'example_code' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
Not in Debug mode

我认为这种混淆是因为未设置 FLASK_DEBUG 环境变量,但即便如此,它确实会显示 * Debug mode: on 那么如何在运行时识别它?

最后,我应该在哪里添加此调试模式检查并更改处理程序 - 调试在 app.run_server() 中设置,但之后立即添加任何代码仅在服务器关闭后执行。

【问题讨论】:

    标签: python flask plotly plotly-dash python-logging


    【解决方案1】:

    也许这种方法对您有用?只需将回调设置为每次程序执行仅运行一次。 (抱歉使用了全局变量,想不出更简洁的方法)。

    import dash
    import dash_core_components as dcc
    import dash_html_components as html
    from dash.dependencies import Input, Output, State
    import logging
    
    app = dash.Dash(__name__)
    app.layout = html.Div([dcc.Store(id='dummy_store')])
    logger = logging.getLogger(__name__)
    LOGGER_CONFIGURED = False
    
    
    @app.callback(
        Output('dummy_store', 'data'),
        Input('dummy_store', 'data'),
    )
    def configure_logger(dummy_store):
        global LOGGER_CONFIGURED
        if LOGGER_CONFIGURED is False:
            print(f'configured logger with debug mode set to {app.server.debug}')
            # do something with the logger here
            LOGGER_CONFIGURED = True
    
    
    if __name__ == '__main__':
        app.run_server(debug=True, use_reloader=True)
    

    【讨论】:

    • 有趣的方法!
    猜你喜欢
    • 2020-05-22
    • 1970-01-01
    • 2011-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多