warnings.warn() vs. logging.warning()
Asked Answered
I

3

147

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

Infantry answered 7/3, 2012 at 2:30 Comment(1)
From this answer: "logging.warn has been deprecated since Python 3.3 and you should use logging.warning."Familist
E
153

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

Educate answered 7/2, 2013 at 22:18 Comment(0)
T
109

logging.warning just logs something at the WARNING level, in the same way that logging.info logs at the INFO level and logging.error logs at the ERROR level. It has no special behaviour.

warnings.warn emits a Warning, which may be printed to stderr, ignored completely, or thrown like a normal Exception (potentially crashing your application) depending upon the precise Warning subclass emitted and how you've configured your Warnings Filter. By default, warnings will be printed to stderr or ignored.

Warnings emitted by warnings.warn are often useful to know about, but easy to miss (especially if you're running a Python program in a background process and not capturing stderr). For that reason, it can be helpful to have them logged.

Python provides a built-in integration between the logging module and the warnings module to let you do this; just call logging.captureWarnings(True) at the start of your script and all warnings emitted by the warnings module will automatically be logged at level WARNING.

Tripody answered 22/6, 2016 at 23:14 Comment(0)
E
49

Besides the canonical explanation in official documentation

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

It is also worth noting that, by default warnings.warn("same message") will show up only once. That is a major noticeable difference. Quoted from official doc

Repetitions of a particular warning for the same source location are typically suppressed.

>>> import warnings
>>> warnings.warn("foo")
__main__:1: UserWarning: foo
>>> warnings.warn("foo")
>>> warnings.warn("foo")
>>>
>>> import logging
>>> logging.warn("bar")
WARNING:root:bar
>>> logging.warn("bar")
WARNING:root:bar
>>> logging.warn("bar")
WARNING:root:bar
>>>
>>>
>>> warnings.warn("fur")
__main__:1: UserWarning: fur
>>> warnings.warn("fur")
>>> warnings.warn("fur")
>>>
Ecdysis answered 18/11, 2016 at 1:3 Comment(2)
Note that "show up only once" is the intended default behaviour, but warning filters can change this.Goren
the answer clearly says this is the default behaviorEnrollee

© 2022 - 2024 — McMap. All rights reserved.