fastapi-cache
fastapi-cache copied to clipboard
[Feature Request] Add a minimum logging
There was a difficult time recently when I tried and couldn't figure out why everything seemed to work, but the cache wouldn't write. It turned out that it was because there was no connection to redis. But it was impossible to understand it from the logs. It would be nice if at least on startup it would say that the connection was successful or something like that. And thank you for your work!
Yes, I have also reproduced this issue. I found the code that captures storage engine connection exceptions and logs them, but it didn't output to the console because logging.NullHandler was used.
So, I think this is a bug. Why do we need to use logging.NullHandler? @long2ice
https://github.com/long2ice/fastapi-cache/blob/ee58f979d4ef22a03012d86efb6fdb7b5f5b91c6/fastapi_cache/decorator.py#L139-L141
https://github.com/long2ice/fastapi-cache/blob/ee58f979d4ef22a03012d86efb6fdb7b5f5b91c6/fastapi_cache/decorator.py#L20
So, I think this is a bug. Why do we need to use
logging.NullHandler? @long2ice
No, using NullHandler is the correct approach, see the logging documentation.
Note that the null handler does not prevent logging!. What happens instead is that if you remove it the logging library steps up and sets some default configuration when a log message hasn't been handled. This is not desireable behaviour in a library.
Instead, configure logging yourself. In a uvicorn setup, only the uvicorn logging hierarchy is configured. You need to add additional logger configuration to have other logging show up.
E.g. here is an example using the uvicorn logging formatter for the root logger, so covering everything that hasn't got propagation disabled:
import logging.config
log_config = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"default": {
"()": "uvicorn.logging.DefaultFormatter",
"fmt": "%(levelprefix)s %(asctime)s %(message)s",
"datefmt": "%Y-%m-%d %H:%M:%S",
},
},
"handlers": {
"default": {
"formatter": "default",
"class": "logging.StreamHandler",
"stream": "ext://sys.stderr",
},
},
"root": {"handlers": ["default"], "level": "INFO"},
}
logging.config.dictConfig(log_config)
Personally, I feel this is something FastAPI should include in their documentation. I think that uvicorn previously would configure a root-level handler, meaning that it used to be such that logging was taken care of for all modules. Personally, I prefer the current uvicorn-specific loggers configuration so you can set up your own logging independent of the uvicorn access logging.