我有一个脚本,它执行数据库操作以及一个alembic API调用,以将新创建的数据库升级为head.我遇到了一个python logger实例的问题,其中使用模块级记录器将日志写入文件.
然后脚本调用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')
Run Code Online (Sandbox Code Playgroud)
日志文件输出
INFO:so_log:Some stuff
INFO:so_log:More stuff
Run Code Online (Sandbox Code Playgroud)
标准输出
INFO [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO [alembic.runtime.migration] Will assume transactional DDL.
print statement before alembic
Code still running after …Run Code Online (Sandbox Code Playgroud) 我试图解决一个算法问题,但目击奇怪的行为.每当我注释掉第一个cout语句,因为它看起来相对温和,只是出于测试目的,我的脚本不会运行.它将编译没有错误,但当我使用txt文件输入参数运行它崩溃.
但是,如果我在下面的脚本中保留cout语句,它运行正常,没有任何问题.我不确定为什么会这样.在评论看似非必要的cout语句时,脚本再次起作用但不是预期的.我在这里错过了什么?
测试输入文件
5
9 6
4 6 8
0 7 1 5
Run Code Online (Sandbox Code Playgroud)
int main(int argc, char *argv[])
{
ifstream stream(argv[1]);
string line;
while (getline(stream, line))
{
cout<<line<<" String representation."<<endl; // Why do i need to keep this to prevent segfault / AppCrash (windows)?
vector<long int> numbers = string_to_ints(line);
for (int i =0; i < numbers.size(); i++)
{
cout<<numbers.at(i)<<" ";
}
cout<<" number representation."<<endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
下面的string_to_ints()声明
vector<long int> string_to_ints(string input) // takes string input and produces a …Run Code Online (Sandbox Code Playgroud)