log_helper.py 1.8 KB

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