How does one set up traceback logging in py-shiny which runs on uvinicorn for python?

Question:

I’m having trouble getting the logger to write any un-caught traceback errors to the log file. Here is my minimum working example in python to reproduce my issue. The idea is that I will have a file that I can reference an end-user to locate and send to me if the application crashes suddenly.

from shiny import App, ui, reactive
import logging


logging.basicConfig(filename='myapp.log', level=logging.INFO)

def ui_card(title, *args):
    return (
        ui.div(
            {"class": "card mb-4"},
            ui.div(title, class_="card-header"),
            ui.div({"class": "card-body"}, *args),
        ),
    )

app_ui = ui.page_fluid(
    ui_card(
        "This button will attempt a divide by zero",
        ui.input_action_button("btn", "Push to Crash"),
    ),
)

def server(input, output, session):

    @reactive.Effect
    @reactive.event(input.btn)
    def _():
        12/0

def create_app():
    return App(app_ui, server)

app = create_app()
app.run()

I’m expecting this to produce the traceback error within the myapp.log file but I’m only seeing this:

INFO:uvicorn.error:Started server process [4684]
INFO:uvicorn.error:Waiting for application startup.
INFO:uvicorn.error:Application startup complete.
INFO:uvicorn.error:Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:uvicorn.error:('127.0.0.1', 62631) - "WebSocket /websocket/" [accepted]
INFO:uvicorn.error:connection open
INFO:uvicorn.error:connection closed
INFO:uvicorn.error:Shutting down
INFO:uvicorn.error:Waiting for application shutdown.
INFO:uvicorn.error:Application shutdown complete.
INFO:uvicorn.error:Finished server process [4684]

The console output is this:

←[32mINFO←[0m:     Started server process [←[36m4684←[0m]
←[32mINFO←[0m:     Waiting for application startup.
←[32mINFO←[0m:     Application startup complete.
←[32mINFO←[0m:     Uvicorn running on ←[1mhttp://127.0.0.1:8000←[0m (Press CTRL+C to quit)
←[32mINFO←[0m:     127.0.0.1:62628 - "←[1mGET / HTTP/1.1←[0m" ←[32m200 OK←[0m
←[32mINFO←[0m:     ('127.0.0.1', 62631) - "WebSocket /websocket/" [accepted]
←[32mINFO←[0m:     connection open
Traceback (most recent call last):
  File "C:Userschandanaconda3envstlibsite-packagesshinyreactive_reactives.py", line 533, in _run
    await self._fn()
  File "C:Userschandanaconda3envstlibsite-packagesshiny_utils.py", line 132, in fn_async
    return fn()
  File "C:Userschandanaconda3envstlibsite-packagesshinyreactive_reactives.py", line 815, in new_user_fn
    return user_fn()
  File "stack_overflow_app.py", line 28, in _
    12/0
ZeroDivisionError: division by zero
C:Userschandanaconda3envstlibsite-packagesshinyreactive_reactives.py:540: ReactiveWarning: Error in Effect: division by zero
  warnings.warn("Error in Effect: " + str(e), ReactiveWarning)
Unhandled error: division by zero
←[32mINFO←[0m:     connection closed
←[32mINFO←[0m:     Shutting down
←[32mINFO←[0m:     Waiting for application shutdown.
←[32mINFO←[0m:     Application shutdown complete.
←[32mINFO←[0m:     Finished server process [←[36m4684←[0m]
Asked By: Chandler Tayek

||

Answers:

@jcheng5 from shiny’s team stated that logging isn’t configured as of this post and to get around this you can redirect stdout and stderr to a file like this:

import sys
logfile = open('myapp.log', 'a', 1)
sys.stdout = logfile
sys.stderr = logfile

Link to actual post: https://github.com/rstudio/py-shiny/issues/305#issuecomment-1214673651

Answered By: Chandler Tayek
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.