Using alembic.config.main redirects log output
In case anyone comes across this issue here is what worked for me:
Updating the
fileConfig
call inenv.py
:fileConfig(config.config_file_name, disable_existing_loggers = False)
Updating the level for
[logger_root]
inalembic.ini
to a lower level, since it is set by default toWARN
I just learned that fileConfig takes a keyword argument, disable_existing_loggers, which defaults to True. Simply adding disable_existing_loggers = False
to the call to fileConfig in env.py
e.g:
fileConfig(config.config_file_name, disable_existing_loggers=False)
appears to allow both logging configurations to work without interfering with each other (which may be preferred rather than having to choose one over the other, in some cases)
This is because alembic sets up logging using fileConfig
from alembic.ini
, you can see it in your env.py
script:
# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name)
This effectively overrides your original logger config.
To avoid this, you can simply remove this line from env.py
, however this will result in no logs being produced when running alembic
from console.
A more robust option is to run alembic commands via alembic.command
instead of alembic.config.main
. This way you can override alembic config at runtime:
from alembic.config import Config
import alembic.command
config = Config('alembic.ini')
config.attributes['configure_logger'] = False
alembic.command.upgrade(config, 'head')
Then in env.py
:
if config.attributes.get('configure_logger', True):
fileConfig(config.config_file_name)