Logging in code disables Starlette logging when called programatically
See original GitHub issueSo, here’s the simplest code, using the quickstart example from Starlette’s documentation, in a file test.py
:
from starlette.applications import Starlette
from starlette.responses import JSONResponse
import uvicorn
import logging
app = Starlette()
@app.route('/')
async def homepage(request):
return JSONResponse({'hello': 'world'})
if __name__ == '__main__':
logging.info('Starting server')
uvicorn.run(app, host='0.0.0.0', port=8000, log_level='debug')
If we run this directly, with python -m test
, there is no logging out put in the console. If we either disable the penultimate line (logging.info(...
), or if we run it externally (uvicorn test:app
), we get the regular logging output:
$ uvicorn test:app
INFO: Started server process [18682]
INFO: Waiting for application startup.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
So, in the case of running uvicorn
programatically and logging stuff in our code, the logging fails to appear. The log_level
argument has no effect either way.
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Python Logging - How do I disable a package's Logging?
logging.basicConfig adds a Handler . Then you go and add another. Both directed to the same file. logging.
Read more >tiangolo/fastapi - Gitter
@vjanz I want to use blocking handlers like SMTPhandler, I think about a walkaround: in the main file I'll settings a root logger...
Read more >Configuration - Starlette
Starlette encourages a strict separation of configuration from code, following the twelve-factor pattern. Configuration should be stored in environment ...
Read more >Uvicorn
It allows Django to support WebSockets, background tasks, and long-running connections, with application code still running in a standard threaded context.
Read more >FastAPI Uvicorn logging in Production - Nucu Labs
To keep things as simple as possible I've put all my code in a single Python ... Create a new file and name...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Resolved with uvicorn 0.3.17
@tomchristie it seems the issue is with the
caplog.set_level
. I was able to get the tests working by passing the name of our logger,"uvicorn"
to the function.So
uvicorn/tests/middleware/test_message_logger.py
lines 16, 37, 55 should be:caplog.set_level(logging.DEBUG, logger="uvicorn")