question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Logging in code disables Starlette logging when called programatically

See original GitHub issue

So, 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:closed
  • Created 5 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
tomchristiecommented, Oct 29, 2018

Resolved with uvicorn 0.3.17

1reaction
alexbotellocommented, Oct 23, 2018

@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")

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found