I am using fast API for creating an API and want to use logging to create csv logs. The API code looks like below:
@app.post("/path")
async def return_something(header: header, body: body):
...
logger.info('....')
return something
Pydantic models are defined like this:
class header(BaseModel):
field1: str
field2: list
I am using logger like this
from logger import ApplicationLogger
logger = ApplicationLogger()
The question is how can I use the existing longing to log pydantic errors for field verification?
Depending on you use case, you could utilize below implementations. Both implement a middleware
exception_handler
for the purpose of capturing aValidationError
orRequestValidationError
. You could log whatever you need within your middleware as precised below. For more information you could check these Github issues (https://github.com/tiangolo/fastapi/issues/1486, https://github.com/tiangolo/fastapi/pull/853)or