HomeInterview QuestionsWhat are the log levels in Python and how would yo…

What are the log levels in Python and how would you use them to log debug, info, warning, error, and critical messages? Have you done versioning of endpoints?

🟡 Medium Conceptual Junior level
1Times asked
Jul 2026Last seen
Jul 2026First seen

💡 Model Answer

Python’s built‑in logging module defines five standard levels: DEBUG (detailed information for diagnosing problems), INFO (general operational events), WARNING (unexpected events that don’t stop execution), ERROR (exceptions that prevent a function from completing), and CRITICAL (severe failures that may halt the program). You set the level on a logger or handler; messages below that level are ignored. For example:

python
import logging
logging.basicConfig(level=logging.INFO)
logging.debug('debug msg')   # ignored
logging.info('info msg')     # printed

In a production API, you might log DEBUG during development, INFO for normal requests, WARNING for rate‑limit hits, ERROR for unhandled exceptions, and CRITICAL for system outages. For endpoint versioning, you can include the version in the log message or use a separate logger per version to isolate traffic. This helps trace issues back to a specific API version.

I have implemented versioning in a microservice by adding a X-API-Version header and logging the header value with each request, which simplifies debugging across versions.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.

Get Assisting AI — Starts at ₹500