logger.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 logging
  15. import os
  16. import datetime
  17. logging.basicConfig(
  18. level=logging.INFO,
  19. format="%(asctime)s %(levelname)s: %(message)s",
  20. datefmt="%Y-%m-%d %H:%M:%S")
  21. def time_zone(sec, fmt):
  22. real_time = datetime.datetime.now()
  23. return real_time.timetuple()
  24. logging.Formatter.converter = time_zone
  25. _logger = logging.getLogger(__name__)
  26. Color = {
  27. 'RED': '\033[31m',
  28. 'HEADER': '\033[35m', # deep purple
  29. 'PURPLE': '\033[95m', # purple
  30. 'OKBLUE': '\033[94m',
  31. 'OKGREEN': '\033[92m',
  32. 'WARNING': '\033[93m',
  33. 'FAIL': '\033[91m',
  34. 'ENDC': '\033[0m'
  35. }
  36. def coloring(message, color="OKGREEN"):
  37. assert color in Color.keys()
  38. if os.environ.get('PADDLECLAS_COLORING', False):
  39. return Color[color] + str(message) + Color["ENDC"]
  40. else:
  41. return message
  42. def anti_fleet(log):
  43. """
  44. logs will print multi-times when calling Fleet API.
  45. Only display single log and ignore the others.
  46. """
  47. def wrapper(fmt, *args):
  48. if int(os.getenv("PADDLE_TRAINER_ID", 0)) == 0:
  49. log(fmt, *args)
  50. return wrapper
  51. @anti_fleet
  52. def info(fmt, *args):
  53. _logger.info(fmt, *args)
  54. @anti_fleet
  55. def warning(fmt, *args):
  56. _logger.warning(coloring(fmt, "RED"), *args)
  57. @anti_fleet
  58. def error(fmt, *args):
  59. _logger.error(coloring(fmt, "FAIL"), *args)
  60. def scaler(name, value, step, writer):
  61. """
  62. This function will draw a scalar curve generated by the visualdl.
  63. Usage: Install visualdl: pip3 install visualdl==2.0.0b4
  64. and then:
  65. visualdl --logdir ./scalar --host 0.0.0.0 --port 8830
  66. to preview loss corve in real time.
  67. """
  68. writer.add_scalar(tag=name, step=step, value=value)
  69. def advertise():
  70. """
  71. Show the advertising message like the following:
  72. ===========================================================
  73. == PaddleClas is powered by PaddlePaddle ! ==
  74. ===========================================================
  75. == ==
  76. == For more info please go to the following website. ==
  77. == ==
  78. == https://github.com/PaddlePaddle/PaddleClas ==
  79. ===========================================================
  80. """
  81. copyright = "PaddleClas is powered by PaddlePaddle !"
  82. ad = "For more info please go to the following website."
  83. website = "https://github.com/PaddlePaddle/PaddleClas"
  84. AD_LEN = 6 + len(max([copyright, ad, website], key=len))
  85. info(
  86. coloring("\n{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}\n{7}\n".format(
  87. "=" * (AD_LEN + 4),
  88. "=={}==".format(copyright.center(AD_LEN)),
  89. "=" * (AD_LEN + 4),
  90. "=={}==".format(' ' * AD_LEN),
  91. "=={}==".format(ad.center(AD_LEN)),
  92. "=={}==".format(' ' * AD_LEN),
  93. "=={}==".format(website.center(AD_LEN)),
  94. "=" * (AD_LEN + 4), ), "RED"))