Ejemplo n.º 1
0
def logging(request):
    marks = request.node.get_closest_marker("logging")
    options = LoggingMarks(
        **{
            **{
                "logfile": None,
                "loglevel": "info",
                "logging_config": None
            },
            **((marks.kwargs or {}) if marks else {}),
        })
    _logging._acquireLock()
    try:
        prev_state = copy(_logging.Logger.manager.loggerDict)
        prev_handlers = copy(_logging.root.handlers)
    finally:
        _logging._releaseLock()
    try:
        setup_logging(
            logfile=options.logfile,
            loglevel=options.loglevel,
            logging_config=options.logging_config,
        )
        yield
    finally:
        _logging._acquireLock()
        try:
            _logging.Logger.manager.loggerDict = prev_state
            _logging.root.handlers = prev_handlers
        finally:
            _logging._releaseLock()
Ejemplo n.º 2
0
    def test_logfile(self):
        with patch('mode.utils.logging._setup_logging') as _sl:
            setup_logging(loglevel='INFO', logfile='foo.txt')

            _sl.assert_called_once_with(
                level=logging.INFO,
                filename='foo.txt',
                stream=None,
                loghandlers=None,
                logging_config=None,
            )
Ejemplo n.º 3
0
    def test_default(self):
        with patch('mode.utils.logging._setup_logging') as _sl:
            setup_logging(loglevel='INFO', logfile=None)

            _sl.assert_called_once_with(
                level=logging.INFO,
                filename=None,
                stream=sys.stdout,
                loghandlers=None,
                logging_config=None,
            )
Ejemplo n.º 4
0
    def test_logfile(self):
        with patch("mode.utils.logging._setup_logging") as _sl:
            setup_logging(loglevel="INFO", logfile="foo.txt")

            _sl.assert_called_once_with(
                level=logging.INFO,
                filename="foo.txt",
                stream=None,
                loghandlers=None,
                logging_config=None,
            )
Ejemplo n.º 5
0
    def test_io(self):
        logfile = Mock()
        with patch('mode.utils.logging._setup_logging') as _sl:
            setup_logging(loglevel='INFO', logfile=logfile)

            _sl.assert_called_once_with(
                level=logging.INFO,
                filename=None,
                stream=logfile,
                loghandlers=None,
                logging_config=None,
            )
Ejemplo n.º 6
0
    def test_io_no_tty(self):
        logfile = Mock()
        logfile.isatty.side_effect = AttributeError()
        with patch('mode.utils.logging._setup_logging') as _sl:
            setup_logging(loglevel='INFO', logfile=logfile)

            _sl.assert_called_once_with(
                level=logging.INFO,
                filename=None,
                stream=logfile,
                loghandlers=None,
                logging_config=None,
            )
Ejemplo n.º 7
0
 def test_setup_logging(self):
     mock_handler = Mock()
     assert logging.setup_logging(loglevel=5,
                                  loghandlers=[mock_handler]) == 5
     self.logging.basicConfig.assert_called_once_with(
         handlers=[mock_handler,
                   self.colorlog.StreamHandler()], level=5)
Ejemplo n.º 8
0
 def test_setup_logging_no_log_handlers(self):
     assert logging.setup_logging(loghandlers=[]) is None
     self.logging.basicConfig.assert_called_once_with(
         handlers=[self.colorlog.StreamHandler()], level=None)