Logging app initialization failure using config.log #218
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.
On exiting due to an unhandled exception during the app initialization (lifespan), the process(es) spawned by
multiprocessing.BaseContext.Process.start()
in_populate()
would have their stack traceback directly printed to stderr byBaseProcess:_bootstrap
.Using the user-configurable
config.log
logger instead makes the log output more consistent.Notes on the changeset:
exc_info
is added and created manually to thelogger.exception()
call, as we're not in anexcept
block and usingexc_info=True
wouldn't help as latersys.exc_info()
wouldn't return anything. This tuple contains what's expected as per the documentation.exit(1)
in order for the "active" loop inhypercorn/run.py:run()
to break and hypercorn to shutdown instead of respawning a process for the app. Exiting the process with an exception was also exiting with code 1 but also printed the stack traceback which is what we want to avoid.Motivation
We're using fastapi/hypercorn in production with structlog and a json formatter so the logs can be ingested by some logs storage and visualization tooling.
We've configured everything so the application outputs JSON logs but in the case the app fails to start, hypercorn was outputting a raw stack traceback to stderr, which isn't consistent with how the logging system was configured.