Introduction π
The logging module in Python provides a flexible and powerful system for tracking events in your applications. Logging is essential for debugging, monitoring, diagnostics, and production-level error tracking.
Note
print() for debuggingπ‘ Supports levels, handlers, formatting, and external log files
π‘ Used in real applications, APIs, automation, ML pipelines, and more
1. Why Use Logging Instead of print()? π€
- β You can control verbosity (DEBUG, INFO, WARNING, ERRORβ¦)
- β Logs can be written to files, console, network, email
- β Can enable/disable logs without removing code
- β Timestamp, filename, and line numbers can be added automatically
2. Basic Logging Usage π§±
basic_logging.py
import logging
logging.basicConfig(level=logging.INFO)
logging.info("This is an info message")
logging.warning("This is a warning")
logging.error("This is an error")β Quick setup β prints log messages to console
3. Logging Levels π
| Level | Value | Usage |
|---|---|---|
| DEBUG | 10 | Detailed debugging information |
| INFO | 20 | General information |
| WARNING | 30 | Something unexpected happened |
| ERROR | 40 | Function failed |
| CRITICAL | 50 | Application crash |
4. Customizing log format π¨
format_logging.py
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s - %(levelname)s - %(message)s"
)
logging.debug("Debugging app...")β `%(asctime)s` β timestamp
β `%(levelname)s` β log level
β `%(message)s` β your message
5. Writing Logs to a File π
file_logging.py
logging.basicConfig(
filename="app.log",
level=logging.ERROR,
format="%(asctime)s - %(levelname)s - %(message)s"
)
logging.error("Something went very wrong!")β Logs stored permanently for debugging or audits
6. Loggers, Handlers & Formatters (Recommended Structure) π§©
For larger apps, you should configure logging using:
- πΉ Logger β the object you call
.info(),.error(), etc. - πΉ Handler β decides where logs go (file, console, network)
- πΉ Formatter β controls message formatting
logger_structure.py
import logging
logger = logging.getLogger("myapp")
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler("log.txt")
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(levelname)s - %(message)s")
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.debug("Debug message")
logger.info("Info message")β Modular
β Scalable
7. Logging Exceptions (Important!) β οΈ
logging_exception.py
import logging
try:
x = 10 / 0
except Exception:
logging.exception("An error occurred")β Automatically prints traceback
8. Disable Logging Completely π«
disable_logging.py
logging.disable(logging.CRITICAL)
logging.error("This will NOT appear")β Useful in production builds
9. Logging from Multiple Modules π¦
module_logging.py
# file: module1.py
import logging
logger = logging.getLogger(__name__)
logger.info("Module 1 started")β Automatically names logger after module
10. Rotating Log Files π
For long-running apps, use rotating logs to prevent giant log files.
rotating_logs.py
from logging.handlers import RotatingFileHandler
import logging
handler = RotatingFileHandler(
"app.log", maxBytes=2000, backupCount=3
)
logger = logging.getLogger("rotator")
logger.addHandler(handler)
logger.warning("Test rotating logs")β Automatically rotates files when size limit reached
11. Timed Rotating Logs β±οΈ
timed_rotating_logs.py
from logging.handlers import TimedRotatingFileHandler
import logging
handler = TimedRotatingFileHandler(
"timed.log", when="midnight", interval=1
)
logger = logging.getLogger("timer")
logger.addHandler(handler)
logger.info("Daily log started")β Useful for daily, hourly or weekly logs
12. JSON Logging (for APIs & ML pipelines) π¦
json_logging.py
import logging, json
class JSONFormatter(logging.Formatter):
def format(self, record):
return json.dumps({
"level": record.levelname,
"message": record.msg,
"time": self.formatTime(record)
})
logger = logging.getLogger("json")
handler = logging.StreamHandler()
handler.setFormatter(JSONFormatter())
logger.addHandler(handler)
logger.error("API failed")β Ideal for structured logs
Logging Cheat Sheet π
| Method / Level | Usage |
|---|---|
| logging.debug() | Detailed developer logs |
| logging.info() | Status updates |
| logging.warning() | Potential problem |
| logging.error() | Recoverable error |
| logging.critical() | Severe failure |
| logging.exception() | Log traceback automatically |
| FileHandler | Write logs to file |
| StreamHandler | Console logging |
| RotatingFileHandler | Auto-rotate logs |
| TimedRotatingFileHandler | Rotate at intervals |
Best Practices π‘
- β Never use print() for debugging in production
- β Use different log levels appropriately
- β Add timestamps & module names for clarity
- β Rotate logs to avoid huge files
- β Use structured logging (JSON) for APIs
- β Avoid logging sensitive information (passwords, tokens)
Conclusion π
You now fully understand the Logging module in Python! Want the next topic? Try Debugging, Error Handling, Traceback, or Unit Testing. Just tell me! π