Introduction
print() is fine for scripts. In production, it’s a liability: no levels, no timestamps, no way to turn it off, and it goes to stdout which may not be captured. Python’s logging module fixes all of this — and it’s built in.
This post is the practical guide: enough logging to be useful, without the complexity.
The basics
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
logger.debug("Detailed info for debugging")
logger.info("Normal operation")
logger.warning("Something unexpected")
logger.error("Something failed")
logger.critical("App is crashing")
Levels in order: DEBUG < INFO < WARNING < ERROR < CRITICAL. Set the level to INFO and DEBUG messages are suppressed.
Why not print()?
print() |
logging |
|
|---|---|---|
| Levels | No | Yes (5 levels) |
| Timestamps | Manual | Automatic |
| Output control | Always stdout | stdout, file, network, etc. |
| Disable globally | No | Yes (level=WARNING) |
| Per-module control | No | Yes (hierarchical loggers) |
| Structured data | No | Yes (extra fields) |
Named loggers (the right way)
# In each module, create a logger named after the module
logger = logging.getLogger(__name__)
# In main.py, configure the root logger
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s | %(name)s | %(levelname)s | %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
__name__ gives you myapp.db, myapp.api, etc. — you can filter logs by module.
Handlers: where logs go
import logging
logger = logging.getLogger("myapp")
logger.setLevel(logging.DEBUG)
# Console handler
console = logging.StreamHandler()
console.setLevel(logging.INFO)
console.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
# File handler
file_handler = logging.FileHandler("app.log")
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s"))
logger.addHandler(console)
logger.addHandler(file_handler)
Now INFO+ goes to console, DEBUG+ goes to file. Different destinations, different levels.
Rotating file handler
from logging.handlers import RotatingFileHandler
handler = RotatingFileHandler("app.log", maxBytes=5_000_000, backupCount=3)
# Rotates at 5MB, keeps 3 backups (app.log.1, app.log.2, app.log.3)
Prevents log files from filling your disk. Essential for production.
Structured logging (JSON)
For log aggregation (Datadog, ELK, CloudWatch), use JSON:
import logging, json
class JSONFormatter(logging.Formatter):
def format(self, record):
return json.dumps({
"time": self.formatTime(record),
"level": record.levelname,
"logger": record.name,
"message": record.getMessage(),
"module": record.module,
"line": record.lineno,
})
handler = logging.StreamHandler()
handler.setFormatter(JSONFormatter())
logger.addHandler(handler)
Now your logs are machine-parseable. Add custom fields with logger.info("msg", extra={"user_id": 42}).
Exception logging
try:
risky_operation()
except Exception:
logger.exception("Operation failed") # logs the full traceback
logger.exception() is logger.error() + traceback. Always use it in except blocks.
Configuration via dictConfig
import logging.config
LOGGING_CONFIG = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"standard": {"format": "%(asctime)s | %(name)s | %(levelname)s | %(message)s"},
},
"handlers": {
"console": {"class": "logging.StreamHandler", "formatter": "standard", "level": "INFO"},
"file": {"class": "logging.handlers.RotatingFileHandler",
"filename": "app.log", "maxBytes": 5000000, "backupCount": 3,
"formatter": "standard", "level": "DEBUG"},
},
"loggers": {
"myapp": {"handlers": ["console", "file"], "level": "DEBUG", "propagate": False},
},
}
logging.config.dictConfig(LOGGING_CONFIG)
logger = logging.getLogger("myapp")
This is the production pattern — configure once, use everywhere.
Common mistakes
print()in production — no levels, no control, goes to stdout.logging.basicConfig()called twice — only the first call takes effect. Configure once at startup.- Root logger everywhere —
logging.info(...)uses the root logger; use named loggers (logging.getLogger(__name__)). - No log rotation —
FileHandlergrows forever. UseRotatingFileHandler. - Logging sensitive data — never log passwords, tokens, or PII. Mask or redact.
- Too verbose in production — set level to
INFOorWARNINGin prod,DEBUGonly when debugging.
Key Concepts
- Levels = DEBUG, INFO, WARNING, ERROR, CRITICAL
- Logger = named, hierarchical (
myapp.db,myapp.api) - Handler = where logs go (console, file, network)
- Formatter = how logs look (text, JSON)
logger.exception()= error + traceback
What to Try Next
- Add logging to your FastAPI backend — FastAPI logs requests automatically.
- Add structured JSON logging to your Streamlit app.
- Send logs to a file and tail them:
tail -f app.log. - Combine with type hints — log typed data.
FAQ
print() vs logging — when is print OK?
Scripts, one-off tasks, debugging during development. The moment code goes to production or runs unattended, switch to logging.
Should I use loguru instead?
loguru is a popular third-party library with better defaults (colored output, rotation, async). It’s great for apps. The standard logging module is better for libraries (zero dependencies). Use loguru for apps, logging for libs.
How do I log in a Streamlit app?
Use logging normally — Streamlit captures stdout/stderr. For user-facing errors, use st.error() in addition to logger.error(). Log the technical detail, show the user a friendly message.