Python 3.6.4 Pycharm 2017.3
我正在尝试以level = debug和console level = error登录文件,但似乎是在运行控制台中反映了文件日志(没有格式化)以及用于控制台的日志。 / p>
我在配置菜单中将文件设置为别名。记录到文件的行为符合预期。
我从文档中的教程中提取了日志记录配置:
LOGGER = logging.getLogger(__name__)
LOGGER.setLevel(logging.DEBUG)
# create file handler which logs all debug messages
fh = logging.FileHandler('clsfr.log')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s -
%(message)s')
ch.setFormatter(formatter)
fh.setFormatter(formatter)
# add the handlers to logger
LOGGER.addHandler(ch)
LOGGER.addHandler(fh)
# from logger calls
LOGGER.error("Test ERROR message")
LOGGER.debug("about to yield {} {}".format(number, year))
LOGGER.debug('iterator returned {} {}'.format(number, year))
LOGGER.info('Queued file key {} - count is {}'.format(id, count))
控制台的一些输出:(全部为红色)
2018-01-31 15:15:40,594 - __main__ - ERROR - Test ERROR message
ERROR:__main__:Test ERROR message
DEBUG:__main__:about to yield 02680309 2000
DEBUG:__main__:iterator returned 02680309 2000
INFO:__main__:Queued file key 02680309_2000_1 - count is 1
DEBUG:__main__:about to yield 02680309 2001
对应的文件输出:(第一行只有红色)
2018-01-31 15:15:40,594 - __main__ - ERROR - Test ERROR message
2018-01-31 15:15:41,822 - __main__ - DEBUG - about to yield 02680309
2000
2018-01-31 15:15:41,823 - __main__ - DEBUG - iterator returned 02680309
2000
2018-01-31 15:15:41,823 - __main__ - INFO - Queued file key
02680309_2000_1 - count is 1
2018-01-31 15:15:41,823 - __main__ - DEBUG - about to yield 02680309
2001
我做了一些搜索尝试和诊断,这导致我在控制台中运行一些代码行,看看是否有多个StreamHandler但它看起来不是:
logging.getLogger().handlers
Out[6]: []
ch = logging.StreamHandler()
logger = logging.getLogger('temp')
logger.handlers
Out[9]: []
logger.addHandler(ch)
logger.handlers
Out[11]: [<StreamHandler <stderr> (NOTSET)>]
fh = logging.FileHandler('test.log')
fh
Out[13]: <FileHandler /Users/andyspence/Project/ccML_async/test.log
(NOTSET)>
logger.addHandler(fh)
logger.handlers
Out[15]: [<StreamHandler <stderr> (NOTSET)>,
<FileHandler /Users/andyspence/Project/ccML_async/test.log
(NOTSET)>]
Python错误或Pycharm配置错误??
答案 0 :(得分:0)
终端的结果相同。
我查看了我的模块并删除了一个未使用的记录器定义。
然后按预期执行代码