【发布时间】:2017-07-14 15:03:12
【问题描述】:
我有一个脚本,它执行数据库操作以及 alembic API 调用以将新创建的数据库升级到 head。我在使用模块级记录器将日志写入文件的 python 记录器实例中遇到问题。
然后脚本调用alembic.config.main(argv=alembic_args) 来运行迁移。但是,alembic 调用之后的每个日志语句,使用原始记录器实例,都不会写入预期的日志文件。
这是一个重现该行为的示例脚本。
#!/usr/bin/env python3
import logging
import os
import alembic.config
from .utilities import get_migration_dir
logging.basicConfig(filename='test.log',
level=logging.DEBUG)
CUR_DIR = os.path.dirname(__file__)
LOG = logging.getLogger('so_log')
LOG.info('Some stuff')
LOG.info('More stuff')
alembic_config = (
'--raiseerr',
'upgrade', 'head'
)
os.chdir(get_migration_dir())
alembic.config.main(argv=alembic_config)
os.chdir(CUR_DIR)
LOG.debug('logging after alembic call.')
LOG.debug('more logging after alembic call.')
print('Code still running after alembic')
日志文件输出
INFO:so_log:Some stuff
INFO:so_log:More stuff
标准输出
INFO [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO [alembic.runtime.migration] Will assume transactional DDL.
print statement before alembic
Code still running after alembic
在调用 alembic API 后,记录器实例 LOG 似乎正在丢失上下文或被定向到其他地方。
另外,我尝试在一个单独的线程中运行 alembic 调用,结果相同。我期望发生的应该是日志语句在使用 alembic 进行迁移后继续写入指定的文件,但这并没有发生。而且,它实际上破坏了随后调用的任何代码的LOG 实例;除非我只是在这里遗漏了一些东西。
【问题讨论】:
标签: python logging python-3.5 alembic