Redirect Logging to File / Directory

When running a dash app, all the HTTP responses and errors are shown in the CLI by default. Is there a way in Dash to send this logging to a file or a file in a folder instead for debugging at a later time? Or any other ways to do this?

Thanks.

2 Likes

Googling “Flask file logger”, I found this: https://gist.github.com/ibeex/3257877

Adapting this to Dash would look something like:

import logging
from logging.handlers import RotatingFileHandler

import dash

app = dash.Dash()

if __name__ == '__main__':
    handler = RotatingFileHandler('foo.log', maxBytes=10000, backupCount=1)
    handler.setLevel(logging.INFO)
    app.server.logger.addHandler(handler)
    app.run_server()

I haven’t tested this, but I’d recommend searching for something along these lines. Be sure to let us know what you end up with!

2 Likes

@tbone did chris’s solution work?
My file comes out empty.
Could you please share what you did
Cheers!

Try using this, I just tested it and it works
import logging
from logging.handlers import RotatingFileHandler

import dash
import dash_html_components as html

logger = logging.getLogger(‘my_logger’)
handler = RotatingFileHandler(‘ttt.log’, maxBytes=10000, backupCount=1)
logger.addHandler(handler)

logger.warning(‘start application’)
app = dash.Dash()
app.layout = html.Div([
html.H3(children=“Hello From the Dash app”)
])
logger.warning(‘end application’)

if name == ‘main’:
app.server.logger.addHandler(handler)
app.run_server()