Adding custom logging handler to uvicorn.error
does not work when running with fastapi run
#44
-
First Check
Commit to Help
Example Codeimport logging
from fastapi import FastAPI
class CustomHandler(logging.StreamHandler):
def emit(self, record):
print('No way')
my_handler = CustomHandler()
my_handler.setLevel(logging.ERROR)
uvi_err_logger = logging.getLogger('uvicorn.error')
uvi_err_logger.addHandler(my_handler)
app = FastAPI()
@app.get('/test')
async def test():
raise ValueError('ohoh') # This should trigger the error message and print out 'No way' in the terminal DescriptionWhen I am using the custom logging handler, inserting the handler under Operating SystemLinux Operating System DetailsThis problem happens on every platform. Python Version3.11.2 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Try removing the existing log handlers before adding your custom one uvi_err_logger = logging.getLogger("uvicorn.access")
if uahandlers := uvi_err_logger.handlers:
uvi_err_logger.removeHandler(uahandlers[0])
uvi_err_logger.addHandler(my_handler) I do this sort of thing fairly often when I deploy web apps to google cloud in order to reduce the amount of redundant request logging. |
Beta Was this translation helpful? Give feedback.
Also, look at #15 (reply in thread)
That worked for me although I haven't bothered yet to find out why