What's the difference between logging.warn and logging.warning in Python?
Asked Answered
M

2

118

The samples at http://docs.python.org/2/howto/logging.html use both warn and warning.

Macintosh answered 21/3, 2013 at 5:6 Comment(2)
Not the same question, but some Googlers may have been looking for: Python warnings.warn() vs. logging.warning()Breadnut
Possible duplicate: warnings.warn() vs. logging.warning().Reinertson
A
160

logging.warn has been deprecated since Python 3.3 and you should use logging.warning.

Prior to Python 3.3, logging.warn and logging.warning were the same function, but logging.warn was not documented, as noted in a closed issue in the Python bug tracker http://bugs.python.org/issue13235:

That's deliberate. The original code (before incorporation into Python) had warn(), which was kept for backward compatibility. The docs refer to warning() because that's what everyone is supposed to use. The method names map to the lower case of the appropriate logging level name.

logging.warn() was kept for backwards compatibility but a deprecation warning was added. logging.warning() is what everyone is supposed to use.

Attitudinize answered 27/3, 2013 at 9:40 Comment(0)
P
45

Prior to Python 3.3, they are the same, however warn is deprecated:

>>> import logging
>>> logging.warn is logging.warning
True

Now they are different, .warn is still deprecated of course though

❯ python3
Python 3.12.2 (main, Feb  6 2024, 20:19:44) [Clang 15.0.0 (clang-1500.1.0.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
>>> logging.warn
<function warn at 0x104d625c0>
>>> logging.warn is logging.warning
False
>>> logging.warning("foo")
WARNING:root:foo
>>> logging.warn("foo")
<stdin>:1: DeprecationWarning: The 'warn' function is deprecated, use 'warning' instead
WARNING:root:foo
Prefiguration answered 21/3, 2013 at 5:20 Comment(2)
Beware, nowadays the same comparison returns False.Photometer
@Bartłomiej I updated the answer to show that they are now different and also display the DeprecationWarningPrefiguration

© 2022 - 2024 — McMap. All rights reserved.