By WebNest Studio Engineering Team
Python Tutorial
Logging in Python
print() is fine while learning, but real applications need logs that have timestamps and severity levels, can be switched on and off per module, go to files or monitoring systems, and never crash the program. Python's built-in logging module provides all of this and is used by virtually every framework, including FastAPI, Django and Flask.
This lesson covers log levels, basic configuration, named loggers, formatting, handlers for files and rotation, logging exceptions, structured JSON logs, and configuration with dictConfig.
Why it matters
When something goes wrong on a server at night, the logs are the only record of what happened. print statements cannot be filtered by importance, switched off, or sent to a file without editing the code; logging does all of that through configuration. Every production Python application uses it.
Log Levels
Five standard levels in increasing severity: DEBUG (detailed diagnostics), INFO (normal events: "order placed"), WARNING (unexpected but handled: "retrying payment"), ERROR (an operation failed) and CRITICAL (the application cannot continue). A logger only outputs messages at or above its configured level; the default is WARNING.
Loggers, Handlers and Formatters
Create a logger per module with logging.getLogger(__name__); names form a hierarchy (shop.payments is a child of shop), so you can tune levels per package. Handlers decide where records go (StreamHandler for the console, FileHandler, RotatingFileHandler, TimedRotatingFileHandler), and formatters decide how they look (%(asctime)s %(levelname)s %(name)s %(message)s).
Good Logging Practice
Configure logging once, at the application's entry point, never in library modules. Pass values as arguments (log.info("user %s logged in", user)) so formatting only happens when the message is emitted. Use log.exception() inside except blocks to include the traceback. Never log passwords, tokens or full card numbers. In production, JSON (structured) logs are easier to search in tools like Elasticsearch, Loki or CloudWatch.
Examples
Levels and basic configuration
import logging
import sys
logging.basicConfig(level=logging.INFO, stream=sys.stdout,
format="%(levelname)-8s %(name)s: %(message)s",
force=True) # replace handlers left over from an earlier run
log = logging.getLogger("shop")
log.debug("cart contents: %s", ["pen"]) # below INFO: not shown
log.info("order %s placed by %s", "WN-101", "asha")
log.warning("payment slow, retrying (attempt %d)", 2)
log.error("payment failed for order %s", "WN-102")
log.critical("database unreachable")
INFO shop: order WN-101 placed by asha
WARNING shop: payment slow, retrying (attempt 2)
ERROR shop: payment failed for order WN-102
CRITICAL shop: database unreachable
Named loggers, per-module levels and logging exceptions
import logging
import sys
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(logging.Formatter("[%(levelname)s] %(name)s - %(message)s"))
root = logging.getLogger()
root.addHandler(handler)
root.setLevel(logging.WARNING)
logging.getLogger("shop.payments").setLevel(logging.DEBUG) # verbose for one package
logging.getLogger("shop.catalog").info("catalog loaded") # suppressed (WARNING level)
pay_log = logging.getLogger("shop.payments")
pay_log.debug("calling gateway")
try:
1 / 0
except ZeroDivisionError:
pay_log.exception("fee calculation failed")
[DEBUG] shop.payments - calling gateway
[ERROR] shop.payments - fee calculation failed
Traceback (most recent call last):
File "main.py", line 16, in <module>
1 / 0
~~^~~
ZeroDivisionError: division by zero
Rotating log files and dictConfig
import logging
import logging.config
logging.config.dictConfig({
"version": 1,
"formatters": {
"detailed": {"format": "%(asctime)s %(levelname)s %(name)s: %(message)s"},
},
"handlers": {
"console": {"class": "logging.StreamHandler", "formatter": "detailed", "level": "INFO"},
"file": {
"class": "logging.handlers.RotatingFileHandler",
"filename": "app.log",
"maxBytes": 1_000_000, # rotate at ~1 MB
"backupCount": 5, # keep app.log.1 ... app.log.5
"formatter": "detailed",
"level": "DEBUG",
},
},
"root": {"handlers": ["console", "file"], "level": "DEBUG"},
})
log = logging.getLogger("webnest")
log.info("application started")
log.debug("this detail goes only to app.log")
2026-09-27 10:15:02,118 INFO webnest: application started
(app.log contains both the INFO and the DEBUG line)
Structured JSON logs with a custom formatter
import json
import logging
import sys
class JsonFormatter(logging.Formatter):
def format(self, record):
entry = {"level": record.levelname, "logger": record.name, "message": record.getMessage()}
entry.update(getattr(record, "context", {}))
return json.dumps(entry)
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(JsonFormatter())
log = logging.getLogger("api")
log.handlers.clear() # avoid duplicate lines if this code runs twice
log.addHandler(handler)
log.setLevel(logging.INFO)
log.propagate = False
log.info("order placed", extra={"context": {"order_id": "WN-101", "amount": 2999}})
{"level": "INFO", "logger": "api", "message": "order placed", "order_id": "WN-101", "amount": 2999}
Common Mistakes
- Using print() for diagnostics in applications instead of logging.
- Calling logging.basicConfig inside library modules.
- Formatting messages with f-strings in hot paths; pass arguments instead.
- Logging secrets or personal data.
- Catching exceptions and logging only str(e), losing the traceback — use log.exception().
Exercise
Configure logging to write to standard output, at level INFO, in the format LEVEL:message. Then log one message at each of the levels debug, info and warning. The debug message must not appear.
Expected output: INFO:started then WARNING:disk almost full
import logging
import sys
# TODO: call logging.basicConfig with stream, level and format
logging.debug("details for developers")
logging.info("started")
logging.warning("disk almost full")
Show hints
- The arguments are
stream=sys.stdout,level=logging.INFOandformat="%(levelname)s:%(message)s". - Add
force=Trueso that the configuration replaces any that already exists.
Show solution
import logging
import sys
logging.basicConfig(
stream=sys.stdout,
level=logging.INFO,
format="%(levelname)s:%(message)s",
force=True,
)
logging.debug("details for developers")
logging.info("started")
logging.warning("disk almost full")
Quick quiz
1. With no configuration, which is the lowest level that is displayed?
- A. WARNING
- B. INFO
- C. DEBUG
- D. ERROR
Show answer
A. The default level is WARNING, so debug and info messages are not shown.
2. Which is the correct order of the levels, from least to most severe?
- A. INFO, DEBUG, WARNING, ERROR, CRITICAL
- B. DEBUG, INFO, WARNING, ERROR, CRITICAL
- C. DEBUG, WARNING, INFO, ERROR, CRITICAL
- D. WARNING, INFO, DEBUG, CRITICAL, ERROR
Show answer
B. A logger shows messages at its own level and above.
3. What does logger.exception("failed") add to the log, when called in an except block?
- A. Nothing extra
- B. A new exception
- C. The time only
- D. The full traceback of the current exception
Show answer
D. It logs at ERROR level and includes the traceback.
Interview questions
Why use logging instead of print?
Show answer
Log messages have a level, so detailed messages can be turned on during development and off in production without changing the code. Output can go to the console, to files that rotate, or to a monitoring system, and each line can include the time, the module and the level automatically. print offers none of this and has to be removed or edited by hand.
What are loggers, handlers and formatters?
Show answer
A logger is the object code calls to record a message; each module usually gets its own with logging.getLogger(__name__). A handler decides where records go, such as the console or a file. A formatter decides how each record is laid out as text. A logger can have several handlers, each with its own level and format.
Key Points to Remember
- Levels: DEBUG < INFO < WARNING < ERROR < CRITICAL; default level is WARNING.
- Use logging.getLogger(__name__) per module; configure once at startup.
- Handlers send records to console/files (with rotation); formatters shape output.
- log.exception() records tracebacks; JSON formatters give structured logs.
- dictConfig centralises logging configuration.
Practice in the playground
Write and run your own code for this topic, or start from the exercise or an example. Runs Python in your browser. Your code for each topic is kept in this browser.
Run the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
Written by
WebNest Studio Engineering Team
Practical lessons from developers working with Java, Python, React, databases and production systems.
Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering