In short
print() is fine on your laptop, but when a script runs at 3 a.m. on a server, you need a proper record: what happened, when, and how serious it was. The logging module gives you that, and you can turn the detail up or down without touching the code.
The logging module records what your program is doing with levels like DEBUG, INFO, WARNING and ERROR, and can send messages to the console or files. Unlike print(), you can switch detail levels without changing code.

Think of it like this
The railway station logbook
The station master at Itarsi writes every event in a logbook with the time: "06:12 Rajdhani arrived, platform 2" for routine things, a red mark for a signal failure. When something goes wrong, the investigators read the log. Your program's log works the same way, with levels instead of red ink.
Watch it happen
print() vs logging
print()
- No time, no severity, no source
- Always on; you delete lines to silence it
- Goes only to the terminal
logging
- Adds time, level and module name automatically
- Change the level to show more or less detail
- Can write to the console, files, or both
Use print() for a script's real output to the user. Use logging for messages about what the program is doing.
The big ideas
What you need to know
Five levels
DEBUG < INFO < WARNING < ERROR < CRITICAL. Setting the level to INFO hides DEBUG and shows everything above.
One logger per module
logger = logging.getLogger(__name__) at the top of each file, so every message shows where it came from.
Configure once
Call logging.basicConfig(...) once in your entry point, never inside library modules.
logger.exception
Inside an except block it logs the message at ERROR level plus the full traceback.
See it in code
Walk through the code
1import logging2 3logger = logging.getLogger(__name__)4 5 6def parse_amount(raw: str) -> float | None:7 logger.debug("Parsing %r", raw)8 try:9 amount = float(raw)10 except ValueError:11 logger.exception("Could not parse amount %r", raw)12 return None13 if amount > 100_000:14 logger.warning("Unusually large amount: %.2f", amount)15 return amount16 17 18if __name__ == "__main__":19 logging.basicConfig(20 level=logging.INFO,21 format="%(asctime)s %(levelname)-8s %(name)s: %(message)s",22 )23 for value in ["499", "abc", "250000"]:24 parse_amount(value)25 logger.info("Finished processing %d values", 3)1/4A named logger for this module. With __name__, the log shows which file each message came from.
Change level=logging.INFO to logging.DEBUG to see the Parsing lines too, without editing anything else.
Save yourself the pain
Common mistakes
Oops
Calling logging.info(...) directly everywhere.
Do this instead
Use a module logger from logging.getLogger(__name__) so messages show their source and can be configured per module.
Oops
Logging passwords, OTPs or full card numbers.
Do this instead
Never log secrets or personal data. Log IDs and outcomes instead.
Oops
Catching an exception and logging only str(e).
Do this instead
Use logger.exception inside the except block to keep the traceback.
Your mission
Replace prints with logs
Quick check
The level is set to WARNING. Which of these messages will appear?
Go deeper
- 1Replace the print() debugging in a script with logging calls.
- 2Configure logging.basicConfig with a format that includes the time and level.
- 3Use logger.exception inside except blocks to record the full traceback.
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.