Python Logging Not Showing Any Output Fix
In this tutorial, you'll learn about Python Logging Not Showing Any Output Fix. We cover key concepts, practical examples, and best practices.
Python's logging module produces no output even though print() works. This happens because logging has a default level of WARNING and no handler is configured. Log messages below WARNING level are silently discarded.
The Problem
import logging
logging.debug("Debug message")
logging.info("Info message")
logging.warning("Warning message")
Output:
WARNING:root:Warning message
The debug and info messages are missing.
Wrong Code
import logging
# WRONG — no configuration at all
logging.info("Application started")
logging.debug("Processing data")
# Nothing shows up
Right Code
import logging
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logging.debug("Debug message")
logging.info("Info message")
Expected output:
2026-06-24 10:30:00,000 - root - DEBUG - Debug message
2026-06-24 10:30:00,000 - root - INFO - Info message
Step-by-Step Fix
Step 1: Configure logging with basicConfig
import logging
logging.basicConfig(level=logging.DEBUG)
logging.info("Now this shows up")
Step 2: Set the correct log level
# Levels in order: DEBUG < INFO < WARNING < ERROR < CRITICAL
logging.basicConfig(level=logging.INFO)
# DEBUG messages will NOT show (DEBUG < INFO)
# INFO, WARNING, ERROR, CRITICAL will show
Step 3: Add a custom format
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
Step 4: Create a named logger
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler()
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.info("Named logger works")
Step 5: Log to a file
import logging
logging.basicConfig(
level=logging.DEBUG,
filename='app.log',
filemode='a',
format='%(asctime)s - %(message)s'
)
logging.info("Logged to file")
Step 6: Check for handler conflicts
import logging
# basicConfig does nothing if handlers already exist
# Check with:
if not logging.getLogger().hasHandlers():
logging.basicConfig(level=logging.DEBUG)
Prevention Tips
- Always call
logging.basicConfig()at the start of your application - Set level to DEBUG during development, INFO in production
- Use named loggers with
logging.getLogger(__name__) - Add file and console handlers for different environments
- Configure logging in a central location, not scattered across modules
Common Mistakes with logging not showing
- Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
These mistakes appear frequently in real-world PYTHON code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro