def log(level, msg, *args, **kwargs):
    """Logs 'msg % args' at absl logging level 'level'.

  If no args are given just print msg, ignoring any interpolation specifiers.

  Args:
    level: int, the absl logging level at which to log the message
        (logging.DEBUG|INFO|WARNING|ERROR|FATAL). While some C++ verbose logging
        level constants are also supported, callers should prefer explicit
        logging.vlog() calls for such purpose.

    msg: str, the message to be logged.
    *args: The args to be substituted into the msg.
    **kwargs: May contain exc_info to add exception traceback to message.
  """
    if level > converter.ABSL_DEBUG:
        # Even though this function supports level that is greater than 1, users
        # should use logging.vlog instead for such cases.
        # Treat this as vlog, 1 is equivalent to DEBUG.
        standard_level = converter.STANDARD_DEBUG - (level - 1)
    else:
        if level < converter.ABSL_FATAL:
            level = converter.ABSL_FATAL
        standard_level = converter.absl_to_standard(level)

    # Match standard logging's behavior. Before use_absl_handler() and
    # logging is configured, there is no handler attached on _absl_logger nor
    # logging.root. So logs go no where.
    if not logging.root.handlers:
        logging.basicConfig()

    _absl_logger.log(standard_level, msg, *args, **kwargs)
def log(level, msg, *args, **kwargs):
  """Logs 'msg % args' at absl logging level 'level'.

  If no args are given just print msg, ignoring any interpolation specifiers.

  Args:
    level: int, the absl logging level at which to log the message
        (logging.DEBUG|INFO|WARNING|ERROR|FATAL). While some C++ verbose logging
        level constants are also supported, callers should prefer explicit
        logging.vlog() calls for such purpose.

    msg: str, the message to be logged.
    *args: The args to be substitued into the msg.
    **kwargs: May contain exc_info to add exception traceback to message.
  """
  if level > converter.ABSL_DEBUG:
    # Even though this function supports level that is greater than 1, users
    # should use logging.vlog instead for such cases.
    # Treat this as vlog, 1 is equivalent to DEBUG.
    standard_level = converter.STANDARD_DEBUG - (level - 1)
  else:
    if level < converter.ABSL_FATAL:
      level = converter.ABSL_FATAL
    standard_level = converter.absl_to_standard(level)

  _absl_logger.log(standard_level, msg, *args, **kwargs)
    def _update_logging_levels(self):
        """Updates absl logging levels to the current verbosity."""
        if not _absl_logger:
            return

        if self._value <= converter.ABSL_DEBUG:
            standard_verbosity = converter.absl_to_standard(self._value)
        else:
            # --verbosity is set to higher than 1 for vlog.
            standard_verbosity = logging.DEBUG - (self._value - 1)

        # Also update root level when absl_handler is used.
        if _absl_handler in logging.root.handlers:
            logging.root.setLevel(standard_verbosity)
  def _update_logging_levels(self):
    """Updates absl logging levels to the current verbosity."""
    if not _absl_logger:
      return

    if self._value <= converter.ABSL_DEBUG:
      standard_verbosity = converter.absl_to_standard(self._value)
    else:
      # --verbosity is set to higher than 1 for vlog.
      standard_verbosity = logging.DEBUG - (self._value - 1)

    # Also update root level when absl_handler is used.
    if _absl_handler in logging.root.handlers:
      logging.root.setLevel(standard_verbosity)
示例#5
0
    def _update_logging_levels(self):
        """Updates absl logging levels to the current verbosity."""
        if not _absl_logger:
            return

        if self._value <= converter.ABSL_DEBUG:
            standard_verbosity = converter.absl_to_standard(self._value)
        else:
            # --verbosity is set to higher than 1 for vlog.
            standard_verbosity = logging.DEBUG - (self._value - 1)

        # Also update root level when absl_handler is used.
        if _absl_handler in logging.root.handlers:
            # Make absl logger inherit from the root logger. absl logger might have
            # a non-NOTSET value if logging.set_verbosity() is called at import time.
            _absl_logger.setLevel(logging.NOTSET)
            logging.root.setLevel(standard_verbosity)
        else:
            _absl_logger.setLevel(standard_verbosity)
示例#6
0
def vlog_is_on(level):
    """Checks if vlog is enabled for the given level in caller's source file.

  Args:
    level: int, the C++ verbose logging level at which to log the message, e.g.
      1, 2, 3, 4... While absl level constants are also supported, callers
      should prefer level_debug|level_info|... calls for checking those.

  Returns:
    True if logging is turned on for that level.
  """

    if level > converter.ABSL_DEBUG:
        # Even though this function supports level that is greater than 1, users
        # should use logging.vlog instead for such cases.
        # Treat this as vlog, 1 is equivalent to DEBUG.
        standard_level = converter.STANDARD_DEBUG - (level - 1)
    else:
        if level < converter.ABSL_FATAL:
            level = converter.ABSL_FATAL
        standard_level = converter.absl_to_standard(level)
    return _absl_logger.isEnabledFor(standard_level)
    def test_absl_to_standard(self):
        self.assertEqual(logging.DEBUG,
                         converter.absl_to_standard(absl_logging.DEBUG))
        self.assertEqual(logging.INFO,
                         converter.absl_to_standard(absl_logging.INFO))
        self.assertEqual(logging.WARNING,
                         converter.absl_to_standard(absl_logging.WARN))
        self.assertEqual(logging.WARN,
                         converter.absl_to_standard(absl_logging.WARN))
        self.assertEqual(logging.ERROR,
                         converter.absl_to_standard(absl_logging.ERROR))
        self.assertEqual(logging.FATAL,
                         converter.absl_to_standard(absl_logging.FATAL))
        self.assertEqual(logging.CRITICAL,
                         converter.absl_to_standard(absl_logging.FATAL))
        # vlog levels.
        self.assertEqual(9, converter.absl_to_standard(2))
        self.assertEqual(8, converter.absl_to_standard(3))

        with self.assertRaises(TypeError):
            converter.absl_to_standard('')