Python

Logging & Debugging

Learn professional logging and debugging techniques. Master the logging module and pdb debugger.

By TechCoder TeamLast updated: 2026-06-02
In a Nutshell

Learn professional logging and debugging techniques. Master the logging module and pdb debugger. This hands-on tutorial focuses on practical implementation of logging & debugging concepts.

Logging & Debugging

Professional applications use logging instead of print statements. Learn to track application behavior and debug effectively.

The logging Module

The logging module is a powerful standard library that tracks events in your software.

Why not just use print?

  • Levels: You can categorize messages (Error vs. Info).
  • Destinations: Send logs to files, emails, or external services.
  • Formatting: Automatically add timestamps and line numbers.

Basic Configuration

import logging

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(message)s'
)
PYTHON PLAYGROUND
⏳ Loading editor…

Log Levels

Setting the Level

Setting a level ignores all messages below that level.

logger.setLevel(logging.WARNING)
# Now DEBUG and INFO messages are ignored
LevelValueWhen to Use
DEBUG10Detailed information for diagnosing problems
INFO20General informational messages
WARNING30Warning messages (potential issues)
ERROR40Error messages (serious problems)
CRITICAL50Critical errors (program may crash)

Logging to File

Logging to File

In production, you want logs saved to a file for later analysis. You use FileHandler for this.

Example

import logging

logging.basicConfig(
    filename='app.log', # Log to this file
    level=logging.INFO
)

logging.info("This is saved to the file")
PYTHON PLAYGROUND
⏳ Loading editor…

Application Logger Example

Application Logger Example

When building larger applications, avoid using the root logger (logging.<method>). Instead, create a named logger for each module.

logger = logging.getLogger(__name__)
PYTHON PLAYGROUND
⏳ Loading editor…

AI Mentor

Confused about "Python logging debugging log levels"? Ask our AI mentor for a simplified explanation.

Quiz

Quiz

Question 1 of 3

Which log level is most severe?

ERROR
CRITICAL
WARNING
DEBUG

Key Takeaways

Logging is better than print for production code.
Log levels control verbosity (DEBUG to CRITICAL).
Formatters customize log message appearance.
Handlers control where logs go (console, file, etc.).

Keep coding! 🚀