log_utils.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # log_utils.py -- Logging utilities for Dulwich
  2. # Copyright (C) 2010 Google, Inc.
  3. #
  4. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  5. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  6. # General Public License as public by the Free Software Foundation; version 2.0
  7. # or (at your option) any later version. You can redistribute it and/or
  8. # modify it under the terms of either of these two licenses.
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # You should have received a copy of the licenses; if not, see
  17. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  18. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  19. # License, Version 2.0.
  20. #
  21. """Logging utilities for Dulwich.
  22. Any module that uses logging needs to do compile-time initialization to set up
  23. the logging environment. Since Dulwich is also used as a library, clients may
  24. not want to see any logging output. In that case, we need to use a special
  25. handler to suppress spurious warnings like "No handlers could be found for
  26. logger dulwich.foo".
  27. For details on the _NullHandler approach, see:
  28. http://docs.python.org/library/logging.html#configuring-logging-for-a-library
  29. For many modules, the only function from the logging module they need is
  30. getLogger; this module exports that function for convenience. If a calling
  31. module needs something else, it can import the standard logging module
  32. directly.
  33. """
  34. import logging
  35. import sys
  36. getLogger = logging.getLogger
  37. class _NullHandler(logging.Handler):
  38. """No-op logging handler to avoid unexpected logging warnings."""
  39. def emit(self, record) -> None:
  40. pass
  41. _NULL_HANDLER = _NullHandler()
  42. _DULWICH_LOGGER = getLogger("dulwich")
  43. _DULWICH_LOGGER.addHandler(_NULL_HANDLER)
  44. def default_logging_config() -> None:
  45. """Set up the default Dulwich loggers."""
  46. remove_null_handler()
  47. logging.basicConfig(
  48. level=logging.INFO,
  49. stream=sys.stderr,
  50. format="%(asctime)s %(levelname)s: %(message)s",
  51. )
  52. def remove_null_handler() -> None:
  53. """Remove the null handler from the Dulwich loggers.
  54. If a caller wants to set up logging using something other than
  55. default_logging_config, calling this function first is a minor optimization
  56. to avoid the overhead of using the _NullHandler.
  57. """
  58. _DULWICH_LOGGER.removeHandler(_NULL_HANDLER)