logging.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. import sys
  16. import logging
  17. import functools
  18. import paddle.distributed as dist
  19. logger_initialized = {}
  20. @functools.lru_cache()
  21. def get_logger(name='srnet', log_file=None, log_level=logging.INFO):
  22. """Initialize and get a logger by name.
  23. If the logger has not been initialized, this method will initialize the
  24. logger by adding one or two handlers, otherwise the initialized logger will
  25. be directly returned. During initialization, a StreamHandler will always be
  26. added. If `log_file` is specified a FileHandler will also be added.
  27. Args:
  28. name (str): Logger name.
  29. log_file (str | None): The log filename. If specified, a FileHandler
  30. will be added to the logger.
  31. log_level (int): The logger level. Note that only the process of
  32. rank 0 is affected, and other processes will set the level to
  33. "Error" thus be silent most of the time.
  34. Returns:
  35. logging.Logger: The expected logger.
  36. """
  37. logger = logging.getLogger(name)
  38. if name in logger_initialized:
  39. return logger
  40. for logger_name in logger_initialized:
  41. if name.startswith(logger_name):
  42. return logger
  43. formatter = logging.Formatter(
  44. '[%(asctime)s] %(name)s %(levelname)s: %(message)s',
  45. datefmt="%Y/%m/%d %H:%M:%S")
  46. stream_handler = logging.StreamHandler(stream=sys.stdout)
  47. stream_handler.setFormatter(formatter)
  48. logger.addHandler(stream_handler)
  49. if log_file is not None and dist.get_rank() == 0:
  50. log_file_folder = os.path.split(log_file)[0]
  51. os.makedirs(log_file_folder, exist_ok=True)
  52. file_handler = logging.FileHandler(log_file, 'a')
  53. file_handler.setFormatter(formatter)
  54. logger.addHandler(file_handler)
  55. if dist.get_rank() == 0:
  56. logger.setLevel(log_level)
  57. else:
  58. logger.setLevel(logging.ERROR)
  59. logger_initialized[name] = True
  60. return logger