Technical skill practice
Python Log Levels vs Warnings: Capture Two Different Diagnostics
Python logging and warnings serve different diagnostic tasks. A log records an application event at a configured severity. A warning can alert callers about an API or usage issue and can be filtered or promoted to an exception. Setting a logger’s threshold does not configure the warnings system.
Start with a small, explicit task
A fictional label function records an informational event and warns when its old option is used. Capture the log in memory and the warning in a local context. Then lower the logger’s verbosity and show that the compatibility warning is still emitted. The exercise does not modify global warning filters permanently.
Run the complete example
This original example was checked with Python 3.12.14. Save it as python_logging_vs_warnings.py and run it with Python 3.12. It uses only the standard library. The assertions check the stated results.
Expected output
INFO:building label
captured warnings: 2
import io
import logging
import warnings
stream = io.StringIO()
logger = logging.getLogger("label-example")
logger.handlers.clear()
logger.propagate = False
logger.setLevel(logging.INFO)
handler = logging.StreamHandler(stream)
handler.setFormatter(logging.Formatter("%(levelname)s:%(message)s"))
logger.addHandler(handler)
def label(old_option=False):
logger.info("building label")
if old_option:
warnings.warn("old_option is deprecated", DeprecationWarning,
stacklevel=2)
return "label"
with warnings.catch_warnings(record=True) as captured:
warnings.simplefilter("always", DeprecationWarning)
assert label(True) == "label"
logger.setLevel(logging.ERROR)
assert label(True) == "label"
assert stream.getvalue() == "INFO:building label\n"
assert len(captured) == 2
assert all(item.category is DeprecationWarning for item in captured)
print(stream.getvalue().strip())
print("captured warnings:", len(captured))
handler.close()
logger.removeHandler(handler)
Explain the result
The first call passes the INFO threshold and writes one line to the memory stream. The second call runs with an ERROR logger threshold, so its INFO event is not written. Both calls still produce a DeprecationWarning because the local warning filter requests always. The two warning objects and the single log line are checked separately.
The warning’s category describes a compatibility concern, not an operational log level. stacklevel points the warning toward the caller instead of the helper’s internal line. The example uses an explicit local filter because default visibility can vary with execution context; it does not promise that every deprecation warning appears on every console.
Avoid these mistakes
Do not hide all warnings just to reduce log noise. Choose the correct logger and warning policy for the audience. The example also disables propagation for its own logger so the same record is not repeated by a parent handler; that is separate from warning filtering.
Questions, answered
Does logging.ERROR suppress warnings.warn?
No in this script. The logger stops writing INFO records, while the warning capture still receives two warnings.
Is a warning always an exception?
Not by default. A warning filter can change its handling; this example captures warning objects for inspection.
Sources and examples
ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.
Your experience. A complete resume.
Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.
Free account required. Free-plan PDF exports include a watermark.