Logging Events
Hacker’s Black Box Recorder
Imagine you’re a hacker flying a stealth drone. You want to know every move it makes and when it takes off, when it lands, when it encounters turbulence. You don’t want to guess; you want a black box recorder that tracks events in real time. In Python, that recorder is the logging system.
Logging is about recording events, errors, and system states so you can monitor, debug, and analyze your programs. It’s the difference between flying blind and having a dashboard of insights.
Why Logging Matters
- Visibility: Logs show what your program is doing at any moment.
- Debugging: Easier to trace errors and understand failures.
- Monitoring: Track performance, usage, and anomalies in production.
- Persistence: Logs can be stored in files for later analysis.
- Real‑World Analogy: Like CCTV cameras which always records, ready to replay when needed.
Basic Logging Setup
import logging
logging.basicConfig(level=logging.INFO)
logging.info("System initialized.")
logging.warning("Low disk space.")
logging.error("Connection failed.")
- Why? Logs provide structured messages with severity levels.
Logging Levels
- DEBUG: Detailed information for developers.
- INFO: General events confirming things work as expected.
- WARNING: Something unexpected happened, but the program continues.
- ERROR: A serious problem occurred.
- CRITICAL: Severe error - program may not continue.
Logging to a File
import logging
logging.basicConfig(filename="system.log",
level=logging.DEBUG,
format="%(asctime)s - %(levelname)s - %(message)s")
logging.info("System started.")
logging.error("Database connection failed.")
- Why? Logs stored in files allow long‑term monitoring and analysis.
Advanced Logging
import logging
logger = logging.getLogger("hacker")
# Console handler
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# File handler
file = logging.FileHandler("hacker.log")
file.setLevel(logging.ERROR)
# Formatter
formatter = logging.Formatter("%(name)s - %(levelname)s - %(message)s")
console.setFormatter(formatter)
file.setFormatter(formatter)
logger.addHandler(console)
logger.addHandler(file)
logger.info("Mission started.")
logger.error("Critical failure detected.")
- Why? Different handlers let you send logs to multiple destinations (console, file, monitoring system).
Real‑World Example
import logging
logging.basicConfig(filename="webapp.log",
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s")
def login(user):
logging.info(f"User {user} attempted login.")
if user == "admin":
logging.info("Admin logged in successfully.")
else:
logging.warning(f"Unauthorized login attempt by {user}.")
- Why? Logs track user activity, helping detect suspicious behavior or system misuse.
The Hacker’s Notebook
- Logging records events, making systems observable and debuggable. Severity levels (
DEBUG,INFO,WARNING,ERROR,CRITICAL) classify importance. - Logs can be directed to console, files, or monitoring systems. Handlers and formatters provide flexible, structured logging.
Hacker’s Mindset: treat logging as your black box recorder. It tracks every move, ensures accountability, and gives you visibility into hidden system states.

Updated on Jan 3, 2026