logger.py 2.3 KB

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