Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 513
Tallmad Avatar asked Mar 21 '13 05:03

Tallmad


People also ask

How do you log a warning in Python?

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 .

What are the five levels of logging in Python?

In Python, the built-in logging module can be used to log events. Log messages can have 5 levels - DEBUG, INGO, WARNING, ERROR and CRITICAL. They can also include traceback information for exceptions. Logs can be especially useful in case of errors to help identify their cause.

What does logging mean in Python?

When you set a logging level in Python using the standard module, you're telling the library you want to handle all events from that level on up. If you set the log level to INFO, it will include INFO, WARNING, ERROR, and CRITICAL messages.

What is logging logger in Python?

Logger : This is the class whose objects will be used in the application code directly to call the functions. LogRecord : Loggers automatically create LogRecord objects that have all the information related to the event being logged, like the name of the logger, the function, the line number, the message, and more.


2 Answers

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.

like image 171
siebz0r Avatar answered Oct 12 '22 14:10

siebz0r


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

>>> import logging >>> logging.warn is logging.warning True 
like image 43
jamylak Avatar answered Oct 12 '22 14:10

jamylak