DEV Community

Sameer Qaiser
Sameer Qaiser

Posted on

Python Logging: How to Stop Using print () for Debugging

πŸ“Œ Quick Info

  • Topic: Using Python's logging module instead of print()
  • Target Audience: Beginners who debug with print statements
  • Goal: Learn a proper tool that scales

1. Introduction

"For my first few weeks of Python, every bug got solved the same way: add a print statement, run the code, read the output, delete the print. It worked β€” until it didn't. Then I learned about logging."


2. The Problem with print()

def divide(a, b):
    print("Starting divide")
    print(f"a = {a}, b = {b}")
    result = a / b
    print(f"Result: {result}")
    return result
Enter fullscreen mode Exit fullscreen mode

Problems:

  • You have to delete it later β€” or leave messy output in production
  • No timestamps, no severity levels, no file output
  • You can't turn it off without editing code
  • Every debug session becomes a cleanup job

3. The Solution (Logging)

import logging

logging.basicConfig(level=logging.INFO)

def divide(a, b):
    logging.info("Starting divide")
    logging.debug(f"a = {a}, b = {b}")
    result = a / b
    logging.info(f"Result: {result}")
    return result
Enter fullscreen mode Exit fullscreen mode

Output:

INFO:root:Starting divide
INFO:root:Result: 5.0
Enter fullscreen mode Exit fullscreen mode

Notice the debug line didn’t print- because the level is set to INFO. Debug messages are hidden by default.


4. The Five Log Levels

Level When to use
DEBUG Detailed info for diagnosing problem.
INFO Confirmation that things are working.
WARNING Something unexpected, but not breaking.
ERROR Something failed.
CRITICAL The Program cannot continue.

Set the level once, and all messages below it are hidden.


5. Real Example (Logging to a file)

import logging

logging.basicConfig(
    filename="app.log",
    level=logging.INFO,
    format="%(asctime)s - %(levelname)s - %(message)s"
)

logging.info("App started")
logging.warning("Low disk space")
logging.error("Could not connect to database")
Enter fullscreen mode Exit fullscreen mode

What’s in app.log:

2026-10-10 14:32:01,234 - INFO - App started
2026-10-10 14:32:01,235 - WARNING - Low disk space
2026-10-10 14:32:01,236 - ERROR - Could not connect to database
Enter fullscreen mode Exit fullscreen mode

Now you have timestamps, levels, and a permanent record.


6. Try/Except + Logging

The most useful combination:

import logging

logging.basicConfig(level=logging.INFO)

try:
    result = 10 / 0
except ZeroDivisionError:
    logging.exception("Division failed")
Enter fullscreen mode Exit fullscreen mode

logging.exception() automatically includes the full traceback. No more losing the error message.


7. What I learned

  • print() is for output. logging is for diagnostics. They're different tools.
  • Log levels let you control verbosity without changing code
  • logging.basicConfig() sets the format, level, and destination
  • logging.exception() captures tracebacks automatically
  • Log to a file for anything you want to keep

8. Common Beginner Mistakes

Mistake 1: Forgetting to set the level
Without level=logging.INFO, only warnings and above show up.

Mistake 2: Using print() and logging together
Pick one. Mixing them makes output hard to parse.

Mistake 3: Logging sensitive data
Never log passwords, tokens, or personal data β€” even at DEBUG level.

Mistake 4: Not using f-strings carefully

logging.debug(f"User: {user}")  
logging.debug("User: %s", user)  
Enter fullscreen mode Exit fullscreen mode

9. Conclusion

"print() gets you through your first week. logging gets you through the next year. The switch takes 10 minutes, and you'll never go back.”

Top comments (1)

Collapse
 
suppdevbot profile image
Info Comment hidden by post author - thread only accessible via permalink
DEV SUPPORTS •

You need to verify your account.

Enter fullscreen mode Exit fullscreen mode

tr.ee/dev-to

Some comments have been hidden by the post's author - find out more