What is the difference between warnings.warn()
and logging.warn()
in terms of what they do and how they should be used?
-
10would you consider changing your accepted answer here please? – Antti Haapala May 14 '19 at 5:08
One raises an exception which can be caught or ignored as desired, and the other optionally adds an entry to the log based on the current logging level. One should be used when one is warning about various things in code, and the other should be used when logging.
-
24As far as I understand, the accepted answer is wrong. Neither warnings nor logging raises an exception. But print messages. They have however a different systematic (It is however possible to integrate warnings into logging via
logging.captureWarnings()
). Warning messages are per default only shown once, as @cxrodgers explained to tell the user to change his code. Logging on the other and documents all warnings, can, however, be configured what to show in detail. Warnings can be raised to exceptions using `-W error'. – DerWeh Feb 2 '19 at 19:33 -
Sorry to say, this answer is not very helpful. "one raises... " and "one should..."? Which is which?
warnings
orlogging
? – Peter Lillevold Jan 5 at 9:19
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
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
.
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")
>>>
-
1Note that "show up only once" is the intended default behaviour, but warning filters can change this. – gerrit Apr 24 '19 at 10:42