Logger only logs error and warning logs
See original GitHub issuefrom fastapi import FastAPI
from fastapi.logger import logger
import logging
app = FastAPI()
@app.get("/")
async def pong():
logger.setLevel(logging.DEBUG)
logger.error("Error log")
logger.warning("Warning log")
logger.info("Info log")
logger.debug("Debug log")
return {"ping": "pong"}
Description
I’m trying to get some info level logs in my FastAPI app. I have tried starting my app with uvicorn app.main:app --reload --log-level debug
and also by setting the level in the code as you can see above. Unfortunately I can only get error and warning logs to display. This might be a uvicorn or logging problem or, more likely, my own misunderstanding. Any help would be greatly appreciated!
Error log
Warning log
Environment
- OS: macOS:
- FastAPI Version 0.61.1
- Uvicorn Version 0.11.8
- Python Version 3.8.5
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Logging, Warnings, and Error Messages · GitBook
logger.setLevel("DEBUG") will print messages from all logger functions · logger.setLevel("ERROR") will only print messages from logger.error · logger.setLevel(" ...
Read more >Only the message from "logger.error" method is displayed in ...
In Eclipse in your project locate and open the /RulesDevelopment/config/properties/logging/log4j.xml file. Find the text as shown in the ...
Read more >django logging - only logs warning and error - Stack Overflow
Try to configure at loggers level too: 'loggers': { 'some_module': { 'handlers': ['primary_log_handler', 'debug_log_handler',], ...
Read more >logging – Report status, error, and informational messages.
The logger, handler, and log message call each specify a level. The log message is only emitted if the handler and logger are...
Read more >Logging HOWTO — Python 3.11.1 documentation
The default level is WARNING , which means that only events of this level and above will be tracked, unless the logging package...
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
You also need
logging.basicConfig(level=logging.INFO)
.It gives very weird results when running under uvicorn but to fix that you will need to go deep into uvicorn logging config. If it becomes very annoying i would advice to use
gunicorn
with the uvicorn worker.The comment above is suggesting to use loguru, but the example uses a middleware which is not mandatory for the use case of intercepting logs.
Here is a working example:
It’s quite long, but with this you simply need to:
setup_logger_from_settings
when your application startsfrom loguru import logger
anywhere in your code and uselogger.debug()
,logger.info()
,logger.warning()
regardless of the location of the code. Those logs will be nicely formatted and printed to stdout by default, and optionally to file as well.All logs produced using
logging
library will be intercepted and formatted as well.Here is an example result: