log_helper.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # Copyright (c) 2022 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. def get_logger(name, level, fmt=None):
  16. """
  17. Get logger from logging with given name, level and format without
  18. setting logging basicConfig. For setting basicConfig in paddle
  19. will disable basicConfig setting after import paddle.
  20. Args:
  21. name (str): The logger name.
  22. level (logging.LEVEL): The base level of the logger
  23. fmt (str): Format of logger output
  24. Returns:
  25. logging.Logger: logging logger with given settings
  26. Examples:
  27. .. code-block:: python
  28. >>> import paddle
  29. >>> import logging
  30. >>> logger = paddle.static.log_helper.get_logger(__name__, logging.INFO,
  31. ... fmt='%(asctime)s-%(levelname)s: %(message)s')
  32. """
  33. logger = logging.getLogger(name)
  34. logger.setLevel(level)
  35. handler = logging.StreamHandler()
  36. if fmt:
  37. formatter = logging.Formatter(fmt=fmt, datefmt='%a %b %d %H:%M:%S')
  38. handler.setFormatter(formatter)
  39. logger.addHandler(handler)
  40. # stop propagate for propagating may print
  41. # log multiple times
  42. logger.propagate = False
  43. return logger