You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

logging.py 2.9 kB

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  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. # ============================================================================
  15. """
  16. get logger.
  17. """
  18. import logging
  19. import os
  20. import sys
  21. from datetime import datetime
  22. class LOGGER(logging.Logger):
  23. """
  24. set up logging file.
  25. Args:
  26. logger_name (string): logger name.
  27. log_dir (string): path of logger.
  28. Returns:
  29. string, logger path
  30. """
  31. def __init__(self, logger_name, rank=0):
  32. super(LOGGER, self).__init__(logger_name)
  33. if rank % 8 == 0:
  34. console = logging.StreamHandler(sys.stdout)
  35. console.setLevel(logging.INFO)
  36. formatter = logging.Formatter('%(asctime)s:%(levelname)s:%(message)s')
  37. console.setFormatter(formatter)
  38. self.addHandler(console)
  39. def setup_logging_file(self, log_dir, rank=0):
  40. """set up log file"""
  41. self.rank = rank
  42. if not os.path.exists(log_dir):
  43. os.makedirs(log_dir, exist_ok=True)
  44. log_name = datetime.now().strftime('%Y-%m-%d_time_%H_%M_%S') + '_rank_{}.log'.format(rank)
  45. self.log_fn = os.path.join(log_dir, log_name)
  46. fh = logging.FileHandler(self.log_fn)
  47. fh.setLevel(logging.INFO)
  48. formatter = logging.Formatter('%(asctime)s:%(levelname)s:%(message)s')
  49. fh.setFormatter(formatter)
  50. self.addHandler(fh)
  51. def info(self, msg, *args, **kwargs):
  52. if self.isEnabledFor(logging.INFO):
  53. self._log(logging.INFO, msg, args, **kwargs)
  54. def save_args(self, args):
  55. self.info('Args:')
  56. args_dict = vars(args)
  57. for key in args_dict.keys():
  58. self.info('--> %s: %s', key, args_dict[key])
  59. self.info('')
  60. def important_info(self, msg, *args, **kwargs):
  61. if self.isEnabledFor(logging.INFO) and self.rank == 0:
  62. line_width = 2
  63. important_msg = '\n'
  64. important_msg += ('*'*70 + '\n')*line_width
  65. important_msg += ('*'*line_width + '\n')*2
  66. important_msg += '*'*line_width + ' '*8 + msg + '\n'
  67. important_msg += ('*'*line_width + '\n')*2
  68. important_msg += ('*'*70 + '\n')*line_width
  69. self.info(important_msg, *args, **kwargs)
  70. def get_logger(path, rank):
  71. logger = LOGGER("mindversion", rank)
  72. logger.setup_logging_file(path, rank)
  73. return logger