logger.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 sys
  15. import time
  16. import paddle
  17. levels = {0: 'ERROR', 1: 'WARNING', 2: 'INFO', 3: 'DEBUG'}
  18. log_level = 2
  19. def log(level=2, message=""):
  20. if paddle.distributed.ParallelEnv().local_rank == 0:
  21. current_time = time.time()
  22. time_array = time.localtime(current_time)
  23. current_time = time.strftime("%Y-%m-%d %H:%M:%S", time_array)
  24. if log_level >= level:
  25. print("{} [{}]\t{}".format(current_time, levels[level], message)
  26. .encode("utf-8").decode("latin1"))
  27. sys.stdout.flush()
  28. def debug(message=""):
  29. log(level=3, message=message)
  30. def info(message=""):
  31. log(level=2, message=message)
  32. def warning(message=""):
  33. log(level=1, message=message)
  34. def error(message=""):
  35. log(level=0, message=message)