Python Logging
Also Known As
Python logger
logging module
TL;DR
Python's built-in logging module provides a structured, configurable way to emit log messages at different severity levels — replacing print() for production code.
Explanation
The logging hierarchy (DEBUG, INFO, WARNING, ERROR, CRITICAL) lets you filter verbosity by environment. Loggers are named (logging.getLogger(__name__)) creating a hierarchy matching the module structure. Handlers direct output to files, streams, or external services. Formatters define the output format. Never use print() for operational logging — it has no severity, no filtering, no structured output, and cannot be silenced in production without code changes.
Common Misconception
✗ print() is fine for quick debugging and can be removed later — print() accumulates in codebases and has no off switch; logging.debug() can be suppressed at the handler level.
Why It Matters
Structured logging with the logging module integrates with log aggregation tools (ELK, Loki, Datadog) and enables filtering by severity in production — print() cannot.
Common Mistakes
- Using the root logger (logging.info()) instead of a named logger — root logger pollution breaks third-party library logging.
- Configuring logging in library code — libraries should add NullHandler only; applications configure logging.
- f-string interpolation in log messages: logger.debug(f'Value: {x}') — evaluated even when DEBUG is disabled; use % formatting.
- Not setting log level for third-party libraries — they default to WARNING; chatty libraries need explicit suppression.
Code Examples
✗ Vulnerable
# print() for logging — no severity, no filtering:
def process_order(order_id):
print(f'Processing order {order_id}') # No level, no off switch
result = do_processing()
print(f'Result: {result}') # Pollutes production output
✓ Fixed
# Named logger with structured output:
import logging
logger = logging.getLogger(__name__) # Named after module
def process_order(order_id: int) -> None:
logger.info('Processing order', extra={'order_id': order_id})
try:
result = do_processing()
logger.debug('Processing result', extra={'order_id': order_id, 'result': result})
except ProcessingError:
logger.error('Order processing failed', extra={'order_id': order_id}, exc_info=True)
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
15 Mar 2026
Edited
22 Mar 2026
Views
36
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 2
Amazonbot 1
Perplexity 1
No pings yesterday
Amazonbot 8
Perplexity 7
Google 4
Ahrefs 2
Unknown AI 1
Also referenced
How they use it
crawler 21
crawler_json 1
Related categories
⚡
DEV INTEL
Tools & Severity
🟡 Medium
⚙ Fix effort: Low
⚡ Quick Fix
Use Python's logging module with a JSON formatter for structured logs — never use print() for application logging; configure log level per module not globally
📦 Applies To
python 3.0
web
cli
🔍 Detection Hints
print() for application logging; no log level configuration; no structured JSON logging; root logger configured affecting all libraries
Auto-detectable:
✓ Yes
pylint
ruff
⚠ Related Problems
🤖 AI Agent
Confidence: Medium
False Positives: Medium
✗ Manual fix
Fix: Low
Context: File
CWE-532
CWE-312