我正在运行Flask项目。我试图将所有日志从调试级别记录到控制台。这是我的代码:
#!flask/bin/python
import logging
from app import app
logging.basicConfig(level=logging.DEBUG, format = '%(asctime)s [%(process)d %(name)s] %(levelname)s %(message)s')
logging.debug('test')
log = logging.getLogger('werkzeug')
log.setLevel(logging.DEBUG)
log.debug('test')
if __name__ == '__main__':
print 'crazy'
log.debug('test')
app.run(host = '0.0.0.0', port = 8100, debug = True, processes = 8)
这是结果(其中一个奇怪的事情是有两个'疯狂'打印):
(flask) [felixc@diamond flask]$ ./run.py
crazy
crazy
我在控制台上找不到任何东西。控制台上甚至没有显示“测试”。那么我的日志在哪里? 我的Python是CentOS 6.9上的v2.7.13。
使用basicConfig更新日志记录:
(flask) [felixc@diamond flask]$ ipython
Python 2.7.13 (default, Jun 22 2017, 13:33:06)
Type "copyright", "credits" or "license" for more information.
IPython 5.5.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: edit
IPython will make a temporary file named:
/tmp/ipython_edit_pUodOs/ipython_edit_WWATn8.py
Editing... done. Executing edited code...
Out[1]: "import logging\nlogging.basicConfig(level=logging.DEBUG, format = '%(asctime)s [%(process)d %(name)s] %(levelname)s %(message)s')\n"
In [2]: logging.debug('test')
2017-12-12 14:51:06,042 [14863 root] DEBUG test
答案 0 :(得分:0)
我刚刚找到了根本原因。我试图在我的项目中使用Ansible,但似乎任何导入都会导致日志记录无效。我没有窥探Ansible的源代码,但我使用了测试代码然后发现了它。
#!flask/bin/python
import logging
from collections import namedtuple
from ansible.parsing.dataloader import DataLoader
#from ansible.vars.manager import VariableManager
#from ansible.inventory.manager import InventoryManager
#from ansible.executor.playbook_executor import PlaybookExecutor
logging.basicConfig(level=logging.DEBUG, format = '%(asctime)s [%(process)d %(name)s] %(levelname)s %(message)s')
logging.debug('test')
logging.info('test2')
logging.error('test3')
来自Ansible模块的任何导入都会导致日志记录消失。我会试着找到原因。我的Ansible版本是2.4.2.0