logging.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 inspect
  8. import logging
  9. import os
  10. import warnings
  11. from torch.distributed.elastic.utils.log_level import get_log_level
  12. def get_logger(name: str | None = None) -> logging.Logger:
  13. """
  14. Util function to set up a simple logger that writes
  15. into stderr. The loglevel is fetched from the LOGLEVEL
  16. env. variable or WARNING as default. The function will use the
  17. module name of the caller if no name is provided.
  18. Args:
  19. name: Name of the logger. If no name provided, the name will
  20. be derived from the call stack.
  21. """
  22. # Derive the name of the caller, if none provided
  23. # Use depth=2 since this function takes up one level in the call stack
  24. return _setup_logger(name or _derive_module_name(depth=2))
  25. def _setup_logger(name: str | None = None) -> logging.Logger:
  26. logger = logging.getLogger(name)
  27. logger.setLevel(os.environ.get("LOGLEVEL", get_log_level()))
  28. return logger
  29. def _derive_module_name(depth: int = 1) -> str | None:
  30. """
  31. Derives the name of the caller module from the stack frames.
  32. Args:
  33. depth: The position of the frame in the stack.
  34. """
  35. try:
  36. stack = inspect.stack()
  37. assert depth < len(stack)
  38. # FrameInfo is just a named tuple: (frame, filename, lineno, function, code_context, index)
  39. frame_info = stack[depth]
  40. module = inspect.getmodule(frame_info[0])
  41. if module:
  42. module_name = module.__name__
  43. else:
  44. # inspect.getmodule(frame_info[0]) does NOT work (returns None) in
  45. # binaries built with @mode/opt
  46. # return the filename (minus the .py extension) as modulename
  47. filename = frame_info[1]
  48. module_name = os.path.splitext(os.path.basename(filename))[0]
  49. return module_name
  50. except Exception as e:
  51. warnings.warn(
  52. f"Error deriving logger module name, using <None>. Exception: {e}",
  53. RuntimeWarning,
  54. stacklevel=2,
  55. )
  56. return None