Force all API loggers to be structured #4331
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes
Related to https://github.com/WordPress/openverse-infrastructure/issues/895
Description
The API logging after #4263 was a mix of structured and unstructured logs. This makes it basically impossible to reliably parse multi-line logs, because there was a mix of log-line start indicators (either a datestamp or an opening curly brace).
This PR forces all logging to be structured. There are a couple leaks during startup, but I don't think there's a good way to fix those that isn't convoluted.
In doing this, I read through a lot of the structlog documentation and refamiliarised myself with the Python logging config docs as well. We were doing a lot of unnecessary configuration and I was able to remove a significant amount of duplicative or unused configuration. I've entirely removed the plain "console" logger configuration.
This PR is critical because without it, our production logs are next to useless when queried. This will not fix the issue, as we also need to make a configuration change to tell CloudWatch to split logs on the opening curly brace instead, but that will be in a PR in the infrastructure repository.
cc @dhruvkb as the original implementer of the structlog in the API. We'll need to keep this in mind when adding structured logging to other apps as well.
Testing Instructions
Set
LOG_PROCESSOR=json
inapi/.env
. Then run the API with the new environment variable usingjust down web && just api/up
and then view the logs withjust logs web
.Make requests and confirm the following:
ValueError
in theget_queryset
of the base media view:Checklist
Update index.md
).main
) or a parent feature branch.just catalog/generate-docs
for catalogPRs) or the media properties generator (
just catalog/generate-docs media-props
for the catalog or
just api/generate-docs
for the API) where applicable.Developer Certificate of Origin
Developer Certificate of Origin