Tallmad
Tallmad

Reputation: 2181

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

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

Upvotes: 124

Views: 51311

Answers (2)

jamylak
jamylak

Reputation: 133634

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

Upvotes: 45

siebz0r
siebz0r

Reputation: 20359

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.

Upvotes: 165

Related Questions