在将日志记录添加到我的项目之前,我会在终端控制台中看到来自库(如flask(的所有日志。添加以下内容后,我不再在终端中看到这些输出,但它们确实显示在日志文件中。我想让它们同时输出到日志文件和终端,这样我就可以在同一个终端/控制台中看到所有输出(打印或日志(。这可能吗?这是我用来将日志添加到我的应用程序的代码,之后我不再在终端窗口中看到日志:
logging.basicConfig(filename = LOG_DIR+'app.log',
level = logging.WARNING,
format = '%(asctime)s:%(levelname)s:%(name)s:%(message)s')
logger = logging.getLogger("app_logger")
logger.setLevel(logging.INFO)
我的launch.json看起来像这样:
"configurations": [
{
"name": "Python: Flask",
"type": "python",
"request": "launch",
"module": "flask",
"env": {
"FLASK_APP": "src/web/app.py",
"FLASK_ENV": "development"
},
"args": [
"run",
"--no-debugger"
],
"jinja": true,
"justMyCode": true,
"console": "integratedTerminal",
},
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
}
]
创建一个额外的处理程序,并将其添加到记录器中。
logging.basicConfig(filename = LOG_DIR+'app.log',
level = logging.WARNING,
format = '%(asctime)s:%(levelname)s:%(name)s:%(message)s')
logger = logging.getLogger("app_logger")
logger.setLevel(logging.INFO)
# Also log to console.
console = logging.StreamHandler()
logger.addHandler(console)