Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

warnings.warn() vs. logging.warning()

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

like image 281
hekevintran Avatar asked Mar 07 '12 02:03

hekevintran


People also ask

What does logger warning do?

The warning() method of a Logger class used to Log a WARNING message. This method is used to pass WARNING types logs to all the registered output Handler objects.

How do I 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 .

How do I ignore warnings in Python?

Use the filterwarnings() Function to Suppress Warnings in Python. The warnings module handles warnings in Python. We can show warnings raised by the user with the warn() function. We can use the filterwarnings() function to perform actions on specific warnings.

Which method is used to display a warning message in Python?

Warning messages are displayed by warn() function defined in 'warning' module of Python's standard library.


1 Answers

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

like image 61
cxrodgers Avatar answered Oct 15 '22 18:10

cxrodgers