Question
How can I stop the alembic logger from deactivating my own loggers after using an alembic command in code?
I'm using alembic
in my code to apply database migrations at application start. I'm also using Python's builtin logging
lib to log to the terminal. After applying the migrations (or running any alembic
command that prints to stdout
it seems) my loggers stop working, though.
Code:
import logging
import alembic.command
from alembic.config import Config as AlembicConfig
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger("app")
logger.debug("Applying alembic migrations.")
alembic_config = AlembicConfig("alembic.ini")
alembic_config.attributes["sqlalchemy.url"] = connection_string
alembic.command.upgrade(alembic_config, "head", tag="from_app")
logger.debug("Terminating app.")
Expected output:
DEBUG:app:Applying alembic migrations.
INFO [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO [alembic.runtime.migration] Will assume transactional DDL.
DEBUG:app:Terminating app.
Actual output:
DEBUG:app:Applying alembic migrations.
INFO [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO [alembic.runtime.migration] Will assume transactional DDL.
The last line is missing.
I've tried setting the log level again after applying the migrations (I thought maybe it changed the root logger log level):
...
alembic.command.upgrade(alembic_config, "head", tag="from_app")
logger.setLevel(logging.DEBUG)
logger.debug("Terminating app.")
In fact, even logger.critical("blah")
won't log anything anymore.
I've also tried applying the basic config again and getting the logger again:
...
alembic.command.upgrade(alembic_config, "head", tag="from_app")
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger("app")
logger.debug("Terminating app.")
But to no avail. Even the root logger isn't logging anymore:
...
alembic.command.upgrade(alembic_config, "head", tag="from_app")
logging.basicConfig(level=logging.DEBUG)
logging.debug("Terminating app.")
Is there anything I can do to make sure that my loggers are logging? I'd like to keep using the builtin logging functionality, but I'm also open to using some lib for that.