logger.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
  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 logging
  15. import os
  16. import sys
  17. import paddle.distributed as dist
  18. __all__ = ['setup_logger']
  19. logger_initialized = []
  20. def setup_logger(name="ppdet", output=None):
  21. """
  22. Initialize logger and set its verbosity level to INFO.
  23. Args:
  24. output (str): a file name or a directory to save log. If None, will not save log file.
  25. If ends with ".txt" or ".log", assumed to be a file name.
  26. Otherwise, logs will be saved to `output/log.txt`.
  27. name (str): the root module name of this logger
  28. Returns:
  29. logging.Logger: a logger
  30. """
  31. logger = logging.getLogger(name)
  32. if name in logger_initialized:
  33. return logger
  34. logger.setLevel(logging.INFO)
  35. logger.propagate = False
  36. formatter = logging.Formatter(
  37. "[%(asctime)s] %(name)s %(levelname)s: %(message)s",
  38. datefmt="%m/%d %H:%M:%S")
  39. # stdout logging: master only
  40. local_rank = dist.get_rank()
  41. if local_rank == 0:
  42. ch = logging.StreamHandler(stream=sys.stdout)
  43. ch.setLevel(logging.DEBUG)
  44. ch.setFormatter(formatter)
  45. logger.addHandler(ch)
  46. # file logging: all workers
  47. if output is not None:
  48. if output.endswith(".txt") or output.endswith(".log"):
  49. filename = output
  50. else:
  51. filename = os.path.join(output, "log.txt")
  52. if local_rank > 0:
  53. filename = filename + ".rank{}".format(local_rank)
  54. os.makedirs(os.path.dirname(filename))
  55. fh = logging.FileHandler(filename, mode='a')
  56. fh.setLevel(logging.DEBUG)
  57. fh.setFormatter(logging.Formatter())
  58. logger.addHandler(fh)
  59. logger_initialized.append(name)
  60. return logger