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.

Setting `log_level="debug"` doesn't output debug log messages on console

See original GitHub issue

Checklist

  • The bug is reproducible against the latest release and/or master.
  • There are no similar issues or pull requests to fix it yet.

Describe the bug

Using the latest uvicorn version 0.13.3 and fastapi version 0.63.0.

Bare minimum application to use logging:

import logging
from fastapi import FastAPI

app = FastAPI()
logger = logging.getLogger("foo")

@app.get("/")
def read_root():
    logger.debug("hello")
    return {"Hello": "World"}

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, port=8080, log_level="debug")

Output of running this file with python 3.8, and executing curl localhost:8080 in a separate terminal:

Press ENTER or type command to continue
INFO:     Started server process [19618]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8080 (Press CTRL+C to quit)
INFO:     127.0.0.1:48340 - "GET / HTTP/1.1" 200 OK
^CINFO:     Shutting down
INFO:     Waiting for application shutdown.
INFO:     Application shutdown complete.
INFO:     Finished server process [19618]

To reproduce

Run the code above.

Expected behavior

I expect to see debug log messages on the console.

Actual behavior

Debug log messages do not show up. logger.error() messages do, but they are unformatted.

@app.get("/")
def read_root():
    logger.error("hello")
    return {"Hello": "World"}

produces:

INFO:     Started server process [19731]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8080 (Press CTRL+C to quit)
hello
INFO:     127.0.0.1:48342 - "GET / HTTP/1.1" 200 OK

Debugging material

Environment

Python 3.8.2, Ubuntu 16.04, uvicorn version 0.13.3 and fastapi version 0.63.0.

Additional context

None

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

17reactions
uSpikecommented, Jan 29, 2021

That behavior seems wrong to me. I’d expect that at least passing debug=True / --debug would enable more verbose logging for all loggers. Requiring a user to provide a log configuration file when they’re trying to get a simple application running is a big ask.

3reactions
daidai21commented, Apr 14, 2021

config log of logging level.

Setting the logging level of uvicorn.run() is different from that of logging module.

import logging
from fastapi import FastAPI

app = FastAPI()
logging.basicConfig(level=logging.DEBUG)   # add this line
logger = logging.getLogger("foo")

@app.get("/")
def read_root():
    logger.debug("hello")
    return {"Hello": "World"}

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, port=8080, log_level="debug")

This is execuate result:

➜  fastapi uvicorn log_lvl_conf:app
INFO:     Started server process [82468]
INFO:uvicorn.error:Started server process [82468]
INFO:     Waiting for application startup.
INFO:uvicorn.error:Waiting for application startup.
INFO:     Application startup complete.
INFO:uvicorn.error:Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:uvicorn.error:Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
DEBUG:foo:hello
INFO:     127.0.0.1:64384 - "GET / HTTP/1.1" 200 OK
Read more comments on GitHub >

github_iconTop Results From Across the Web

Logging HOWTO — Python 3.11.1 documentation
Display console output for ordinary usage of a command line script or program ... DEBUG:root:This message should go to the log file INFO:root:So...
Read more >
c# - Why isn't Serilog writing Debug messages even when the ...
I was having this problem with a writer to MSSqlServer. The key in the answer is the ".MinimumLevel.Debug()" at the logger level in...
Read more >
How to use Debug Log in Unity (without affecting performance)
The simplest way to print a message in the console in Unity is by using the Debug Log function, which takes a string...
Read more >
Node.js Logging Tutorial - Stackify
Debug level can contain things like stack traces, input and output parameters, or special messages for developers.
Read more >
console.debug() - Web APIs - MDN Web Docs
The console.debug() method outputs a message to the web console at the "debug" log level. The message is only displayed to the user...
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