warnings.warn() vs. logging.warning()

后端 未结 4 1710
眼角桃花
眼角桃花 2020-12-13 11:38

What is the difference between warnings.warn() and logging.warn() in terms of what they do and how they should be used?

4条回答
  •  悲&欢浪女
    2020-12-13 12:24

    I agree with the other answer -- logging is for logging and warning is for warning -- but I'd like to add more detail.

    Here is a tutorial-style HOWTO taking you through the steps in using the logging module. https://docs.python.org/3/howto/logging.html

    It directly answers your question:

    warnings.warn() in library code if the issue is avoidable and the client application should be modified to eliminate the warning

    logging.warning() if there is nothing the client application can do about the situation, but the event should still be noted

提交回复
热议问题