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