Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 更改unittest中的日志级别_Python_Logging_Python Unittest - Fatal编程技术网

Python 更改unittest中的日志级别

Python 更改unittest中的日志级别,python,logging,python-unittest,Python,Logging,Python Unittest,我的印象是(但没有找到相关文档),unittest为所有记录器将日志记录级别设置为WARNING。我想: 能够从命令行(运行测试时)或测试模块本身为所有记录器指定日志记录级别 避免unittest与应用程序日志记录级别混淆:在运行测试时,我希望具有与运行应用程序时相同的日志记录输出(相同的级别) 如何实现这一点?请参见下面的Python登录示例。您还可以使用“设置级别”方法更改日志级别 import os import logging logging.basicConfig() logge

我的印象是(但没有找到相关文档),unittest为所有记录器将日志记录级别设置为
WARNING
。我想:

  • 能够从命令行(运行测试时)或测试模块本身为所有记录器指定日志记录级别
  • 避免unittest与应用程序日志记录级别混淆:在运行测试时,我希望具有与运行应用程序时相同的日志记录输出(相同的级别)

如何实现这一点?

请参见下面的Python登录示例。您还可以使用“设置级别”方法更改日志级别

import os
import logging

logging.basicConfig()
logger = logging.getLogger(__name__)

# Change logging level here.
logger.setLevel(os.environ.get('LOG_LEVEL', logging.INFO))

logger.info('For INFO message')
logger.debug('For DEBUG message')
logger.warn('For WARNING message')
logger.error('For ERROR message')
logger.fatal('For CRITICAL message')

我不相信
unittest
本身对日志记录有任何作用,除非您使用它定义的
\u CapturingHandler
类。这个简单的程序演示了:

import logging
import unittest

logger = logging.getLogger(__name__)

class MyTestCase(unittest.TestCase):
    def test_something(self):
        logger.debug('logged from test_something')


if __name__ == '__main__':
    # DEBUG for demonstration purposes, but you could set the level from
    # cmdline args to whatever you like
    logging.basicConfig(level=logging.DEBUG, format='%(name)s %(levelname)s %(message)s')
    unittest.main()
运行时,它会打印

__main__ DEBUG logged from test_something
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

显示它正在以
DEBUG
级别记录事件,如预期的那样。因此,问题可能与其他内容有关,例如,测试中的代码,或其他一些测试运行程序,它们更改日志记录配置或重定向
sys.stdout
sys.stderr
。您可能需要提供有关测试环境的更多信息,或者最好提供一个演示问题的最小程序(正如我上面的示例所示,
unittest
本身并不会导致您所描述的问题)。

这是对@Vinay上述答案的补充。它没有回答原来的问题。我想包括用于修改日志级别的命令行选项。其目的是仅当我从命令行传递某个参数时才能获得详细的日志记录。我就是这样解决的:

import sys
import unittest
import logging
from histogram import Histogram
class TestHistogram(unittest.TestCase):

def test_case2(self):
    h = Histogram([2,1,2])
    self.assertEqual(h.calculateMaxAreaON(), 3)

if __name__ == '__main__':
    argv = len(sys.argv) > 1 and sys.argv[1]
    loglevel = logging.INFO if argv == '-v' else logging.WARNING
    logging.basicConfig(level=loglevel)
    unittest.main() 

目的是获得更详细的日志记录。我知道它不能回答这个问题,但我会把它放在这里,以防有人来寻找类似的需求。

像往常一样,你能给出一个最小但完整的示例,以及预期和实际输出吗?你现在有没有可能找到答案?你愿意和我分享吗?在我的情况下,只显示
INFO
日志消息,我还想看到
DEBUG
消息。@dan-j您能提供一个再现您的问题的最小示例吗?不,
unittest
不会自动更改日志级别。更有可能的是,您的生产代码运行一段配置日志的代码,而您的单元测试没有运行。缺少配置意味着默认值适用。这与unittest改变配置不同,python日志框架就是这样设置的。但OP暗示,当涉及unittest时,这不起作用。unittest显然把事情搞砸了,所以为什么、如何以及怎么做呢?logger.warn()已被弃用。现在应该使用logger.warning()