コード例 #1
0
  def test_log_file(self):
    handler = logging.PythonHandler()
    self.assertEqual(sys.stderr, handler.stream)

    stream = mock.Mock()
    handler = logging.PythonHandler(stream)
    self.assertEqual(stream, handler.stream)
コード例 #2
0
 def test_emit_fatal_non_absl(self):
   stream = _StreamIO()
   handler = logging.PythonHandler(stream)
   record = std_logging.LogRecord(
       'name', std_logging.FATAL, 'path', 12, 'logging_msg', [], False)
   with mock.patch.object(os, 'abort') as mock_abort:
     handler.emit(record)
     mock_abort.assert_not_called()
コード例 #3
0
 def test_emit(self):
   stream = _StreamIO()
   handler = logging.PythonHandler(stream)
   handler.stderr_threshold = std_logging.FATAL
   record = std_logging.LogRecord(
       'name', std_logging.INFO, 'path', 12, 'logging_msg', [], False)
   handler.emit(record)
   self.assertEqual(1, stream.getvalue().count('logging_msg'))
コード例 #4
0
 def test_emit_on_stderr(self):
   mock_stderr = _StreamIO()
   with mock.patch.object(sys, 'stderr', new=mock_stderr) as mock_stderr:
     handler = logging.PythonHandler()
     handler.stderr_threshold = std_logging.INFO
     record = std_logging.LogRecord(
         'name', std_logging.INFO, 'path', 12, 'logging_msg', [], False)
     handler.emit(record)
     self.assertEqual(1, mock_stderr.getvalue().count('logging_msg'))
コード例 #5
0
 def test_close_afile(self):
   stream = mock.Mock()
   stream.isatty.return_value = False
   stream.close.side_effect = ValueError
   handler = logging.PythonHandler(stream)
   with mock.patch.object(handler, 'flush') as mock_flush:
     with mock.patch.object(std_logging.StreamHandler, 'close') as super_close:
       handler.close()
       mock_flush.assert_called_once()
       super_close.assert_called_once()
コード例 #6
0
 def test_close(self):
   stream = mock.Mock()
   stream.isatty.return_value = True
   handler = logging.PythonHandler(stream)
   with mock.patch.object(handler, 'flush') as mock_flush:
     with mock.patch.object(std_logging.StreamHandler, 'close') as super_close:
       handler.close()
       mock_flush.assert_called_once()
       super_close.assert_called_once()
       stream.close.assert_not_called()
コード例 #7
0
def wrap_environment(env: environments.Environment,
                     pretty_print: bool = True,
                     log_every: bool = False,
                     log_by_step: bool = False) -> dm_env.Environment:
  """Returns a wrapped environment that logs to terminal."""
  # Set logging up to show up in STDERR.
  std_logging.getLogger().addHandler(logging.PythonHandler())
  logger = Logger(pretty_print, absl_logging=True)
  return wrappers.Logging(
      env, logger, log_by_step=log_by_step, log_every=log_every)
コード例 #8
0
 def test_emit_fatal_absl(self):
   stream = _StreamIO()
   handler = logging.PythonHandler(stream)
   record = std_logging.LogRecord(
       'name', std_logging.FATAL, 'path', 12, 'logging_msg', [], False)
   record.__dict__[logging._ABSL_LOG_FATAL] = True
   with mock.patch.object(handler, 'flush') as mock_flush:
     with mock.patch.object(os, 'abort') as mock_abort:
       handler.emit(record)
       mock_abort.assert_called_once()
       mock_flush.assert_called()  # flush is also called by super class.
コード例 #9
0
def config_logging():
    """Overrides logging to go through TQDM.

  TODO use this call to kill then restore:
  https://github.com/tqdm/tqdm#redirecting-writing

  """
    h = logging.get_absl_handler()
    old = h.python_handler
    h._python_handler = logging.PythonHandler(stream=TqdmFile(sys.stderr))
    logging.use_python_logging()
コード例 #10
0
    def test_close_fake_file(self):
        class FakeFile(object):
            """A file-like object that does not implement "isatty"."""
            def __init__(self):
                self.closed = False

            def close(self):
                self.closed = True

            def flush(self):
                pass

        fake_file = FakeFile()
        handler = logging.PythonHandler(fake_file)
        handler.close()
        self.assertTrue(fake_file.closed)
コード例 #11
0
def tqdm_logging():
    """Overrides logging to go through TQDM.

  https://github.com/tqdm/tqdm#redirecting-writing

  """
    handler = logging.get_absl_handler()
    orig = handler.python_handler

    try:
        handler._python_handler = logging.PythonHandler(
            stream=TqdmFile(sys.stderr))

        # The changes won't take effect if this hasn't been called. Defensively
        # call it again here.
        logging.use_python_logging()
        yield orig.stream
    except Exception as exc:
        raise exc
    finally:
        handler._python_handler = orig
コード例 #12
0
 def test_flush_with_environment_error(self):
   stream = mock.Mock()
   stream.flush.side_effect = EnvironmentError
   handler = logging.PythonHandler(stream)
   handler.flush()
   stream.flush.assert_called_once()
コード例 #13
0
 def test_flush(self):
   stream = mock.Mock()
   handler = logging.PythonHandler(stream)
   handler.flush()
   stream.flush.assert_called_once()
コード例 #14
0
 def setUp(self):
   (year, month, day, hour, minute, sec,
    dunno, dayofyear, dst_flag) = (1979, 10, 21, 18, 17, 16, 3, 15, 0)
   self.now_tuple = (year, month, day, hour, minute, sec,
                     dunno, dayofyear, dst_flag)
   self.python_handler = logging.PythonHandler()
コード例 #15
0
 def test_close_stderr(self):
   with mock.patch.object(sys, 'stderr') as mock_stderr:
     mock_stderr.isatty.return_value = False
     handler = logging.PythonHandler(sys.stderr)
     handler.close()
     mock_stderr.close.assert_not_called()
コード例 #16
0
 def test_close_original_stdout(self):
   with mock.patch.object(sys, '__stdout__') as mock_original_stdout:
     mock_original_stdout.isatty.return_value = False
     handler = logging.PythonHandler(sys.__stdout__)
     handler.close()
     mock_original_stdout.close.assert_not_called()
コード例 #17
0
 def test_flush_with_assertion_error(self):
   stream = mock.Mock()
   stream.flush.side_effect = AssertionError
   handler = logging.PythonHandler(stream)
   with self.assertRaises(AssertionError):
     handler.flush()