Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Python's logging module categorizes diagnostic and operational messages using a standardized severity hierarchy. By assigning appropriate severity levels to log messages, you can control the volume of logging output without altering your source code—viewing granular debug details during local development while filtering down to warnings and errors in production.
Python defines five standard levels, each associated with an integer severity weight:
| Level Name | Numeric Value | When It Should Be Used |
|---|---|---|
DEBUG | 10 | Detailed diagnostic information, variable state inspection, and function parameter tracking for developers. |
INFO | 20 | Routine confirmation that operations and milestones are proceeding as expected (e.g. server booted, user signed in). |
WARNING | 30 | Indication that something unexpected happened or a potential issue is emerging (e.g., deprecated API usage, low disk space), but execution continues normally. |
ERROR | 40 | Due to a more serious problem, the software was unable to complete a specific task or transaction. |
CRITICAL | 50 | A fatal event has occurred indicating the application or server itself may crash or be unable to continue executing. |
logger.exception()When catching runtime exceptions, standard logging.error() records only your text message. To automatically include the complete Python traceback without manually formatting it, use logger.exception() (or pass exc_info=True to logger.error()):
In real-world applications, you configure the log level dynamically using environment variables or command-line flags:
logging.basicConfig() Multiple Timeslogging.basicConfig() only does work the first time it is invoked. Subsequent calls are silently ignored unless force=True is set (Python 3.8+).
DEBUG for fine-grained diagnostic details.INFO for general operational confirmation.WARNING for unexpected but recoverable events.ERROR for failures affecting a specific operation.CRITICAL for application-wide fatal crashes.Create a logger that prints INFO and higher messages to the console while simultaneously recording detailed DEBUG and higher messages to a persistent app.log file.
logging.basicConfig()?ERROR level message along with the full exception traceback?logging.ERROR, which of the following statements will be emitted?DEBUG level?In the next lesson, we will continue your intermediate Python journey with Basic Debugging with pdb (9: Debugging and Logging).
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Using print vs logging | Basic Debugging with pdb |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.