c10d_error_logger.py 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. #!/usr/bin/env python3
  2. # Copyright (c) Facebook, Inc. and its affiliates.
  3. # All rights reserved.
  4. #
  5. # This source code is licensed under the BSD-style license found in the
  6. # LICENSE file in the root directory of this source tree.
  7. import logging
  8. from typing import List, Tuple
  9. from torch.distributed.logging_handlers import _log_handlers
  10. __all__: List[str] = []
  11. def _get_or_create_logger() -> logging.Logger:
  12. logging_handler, log_handler_name = _get_logging_handler()
  13. logger = logging.getLogger(f"c10d-collectives-{log_handler_name}")
  14. logger.setLevel(logging.DEBUG)
  15. formatter = logging.Formatter(
  16. "%(asctime)s %(filename)s:%(lineno)s %(levelname)s p:%(processName)s t:%(threadName)s: %(message)s"
  17. )
  18. logging_handler.setFormatter(formatter)
  19. logger.propagate = False
  20. logger.addHandler(logging_handler)
  21. return logger
  22. def _get_logging_handler(destination: str = "default") -> Tuple[logging.Handler, str]:
  23. log_handler = _log_handlers[destination]
  24. log_handler_name = type(log_handler).__name__
  25. return (log_handler, log_handler_name)