Mathew K Analytics

Lesson 13 · Python standard library deep dive

Python logging Explained: Loggers, Handlers & Formatters | Standard Library #13

Logging, Python's built-in, production-grade diagnostics system. Levels, loggers, formatters, handlers, exceptions, and the logger hierarchy. Let's get into…

What you'll learn

Data

No separate download needed — the notebook creates or downloads everything it uses.

📓 Full notebook

Download .ipynb

Python Standard Library Deep-Dive, Video 13: logging#

  • Video thirteen of the twenty-five-part series: logging, Python's built-in, production-grade diagnostics system.
  • Levels, loggers, formatters, handlers, exceptions, and the logger hierarchy.
  • Let's get into it.

Part 1: What logging Offers, vs print()#

import logging
logging.warning('This is a warning, using the root logger directly')
logging.info('This info message genuinely will not show by default')
WARNING:root:This is a warning, using the root logger directly

Part 2: Log Levels#

print(logging.DEBUG)
print(logging.INFO)
print(logging.WARNING)
print(logging.ERROR)
print(logging.CRITICAL)
print(logging.DEBUG < logging.INFO < logging.WARNING < logging.ERROR < logging.CRITICAL)
10
20
30
40
50
True

Part 3: basicConfig()#

import importlib
importlib.reload(logging)
logging.basicConfig(
    level=logging.DEBUG,
    format='%(levelname)s: %(message)s'
)
logging.debug('Now debug messages genuinely show up')
logging.info('So do info messages')
logging.warning('And warnings still show, formatted the same way')
DEBUG: Now debug messages genuinely show up
INFO: So do info messages
WARNING: And warnings still show, formatted the same way

Part 4: getLogger() and name#

logger = logging.getLogger('my_app.database')
print(logger.name)
same_logger = logging.getLogger('my_app.database')
print(logger is same_logger)
logger.warning('A warning from the database module specifically')
WARNING: A warning from the database module specifically
my_app.database
True

Part 5: Formatters#

formatter = logging.Formatter('%(asctime)s | %(name)s | %(levelname)s | %(message)s')
handler = logging.StreamHandler()
handler.setFormatter(formatter)
custom_logger = logging.getLogger('demo.formatter')
custom_logger.addHandler(handler)
custom_logger.setLevel(logging.INFO)
custom_logger.propagate = False
custom_logger.info('A fully custom-formatted message')
2026-08-23 18:07:23,802 | demo.formatter | INFO | A fully custom-formatted message

Part 6: Handlers: StreamHandler and FileHandler#

file_logger = logging.getLogger('demo.file')
file_logger.propagate = False
file_handler = logging.FileHandler('demo_app.log')
file_handler.setFormatter(logging.Formatter('%(levelname)s - %(message)s'))
file_logger.addHandler(file_handler)
file_logger.setLevel(logging.INFO)
file_logger.info('This genuinely goes to a real file, not the console')
file_handler.close()
with open('demo_app.log') as f:
    print(f.read())
INFO - This genuinely goes to a real file, not the console

Part 7: Multiple Handlers on One Logger#

multi_logger = logging.getLogger('demo.multi')
multi_logger.propagate = False
multi_logger.setLevel(logging.DEBUG)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.WARNING)
console_handler.setFormatter(logging.Formatter('%(levelname)s: %(message)s'))
file_handler2 = logging.FileHandler('demo_detailed.log')
file_handler2.setLevel(logging.DEBUG)
file_handler2.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(message)s'))
multi_logger.addHandler(console_handler)
multi_logger.addHandler(file_handler2)
multi_logger.debug('Only genuinely goes to the file')
multi_logger.error('Goes to both the console and the file')
file_handler2.close()
ERROR: Goes to both the console and the file

Part 8: Logging Exceptions#

error_logger = logging.getLogger('demo.errors')
error_logger.propagate = False
error_logger.addHandler(logging.StreamHandler())
error_logger.setLevel(logging.ERROR)
try:
    result = 10 / 0
except ZeroDivisionError:
    error_logger.exception('A calculation genuinely failed')
A calculation genuinely failed
Traceback (most recent call last):
  File "C:\Users\makmw\AppData\Local\Temp\ipykernel_25540\346908998.py", line 6, in <module>
    result = 10 / 0
             ~~~^~~
ZeroDivisionError: division by zero

Part 9: Logger Hierarchy and Propagation#

parent = logging.getLogger('myapp')
parent.propagate = False
parent.addHandler(logging.StreamHandler())
parent.setLevel(logging.INFO)
child = logging.getLogger('myapp.submodule')
print(child.parent is parent)
child.info('This message genuinely propagates up to the parent handler')
This message genuinely propagates up to the parent handler
True

Part 10: Common Patterns#

def process_order(order_id, amount):
    module_logger = logging.getLogger('orders')
    module_logger.propagate = False
    if not module_logger.handlers:
        module_logger.addHandler(logging.StreamHandler())
        module_logger.setLevel(logging.INFO)
    module_logger.info(f'Processing order {order_id} for ${amount}')
    if amount <= 0:
        module_logger.warning(f'Order {order_id} has a genuinely suspicious amount')
        return False
    module_logger.info(f'Order {order_id} processed successfully')
    return True
process_order(1001, 49.99)
process_order(1002, -5.00)
Processing order 1001 for $49.99
Order 1001 processed successfully
Processing order 1002 for $-5.0
Order 1002 has a genuinely suspicious amount
False

Wrap-Up: What You Learned#

  • logging adds severity, timestamps, and structure that print() simply can't offer.
  • Five levels: DEBUG, INFO, WARNING, ERROR, CRITICAL, in increasing severity.
  • basicConfig for one-time root logger setup; getLogger(name) for real, per-module loggers.
  • Formatters control exact message layout; handlers control where messages actually go.
  • StreamHandler for console output, FileHandler for real files, and multiple handlers per logger.
  • logger.exception automatically attaches the full traceback inside an except block.
  • The dotted logger hierarchy and propagate, letting one parent's handlers cover every submodule.
  • The real production pattern: configure once at the entry point, getLogger(name) everywhere else.
  • That wraps up logging. Next up: argparse, for building real command-line interfaces.

🧠 Test yourself

Pick an answer to check it. Your best score is saved in this browser.

  1. With no configuration at all, which of these calls prints something?

  2. What does logger.exception('failed') add compared to logger.error('failed')?

  3. What does this print?

    import logging
    print(logging.getLogger('a') is logging.getLogger('a'))
    
  4. A logger is set to DEBUG. Its console handler is set to WARNING and its file handler to DEBUG. Where does a DEBUG message go?

  5. What does this print?

    import logging
    parent = logging.getLogger('myapp')
    child = logging.getLogger('myapp.sub')
    print(child.parent is parent, child.name)
    

💻 Practice exercises

Try each one in the notebook or in your own editor before opening the solution.

  1. Configure basicConfig to show INFO and above in the format 'LEVEL: message' on stdout. Log a debug, an info and a warning message and check which appear.

    💡 Hint

    Pass level, format and stream=sys.stdout to logging.basicConfig.

    ✅ Show solution
    import logging
    import sys
    
    logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s", stream=sys.stdout)
    logging.debug("hidden")        # below INFO, not shown
    logging.info("server started")
    logging.warning("disk space low")
    
  2. Create a logger named 'demo.file' that writes INFO messages to a log file in a temp folder using the format '%(levelname)s - %(message)s'. Log two messages, close the handler, and print the file contents.

    💡 Hint

    Use FileHandler with setFormatter, set propagate to False, and close the handler before reading.

    ✅ Show solution
    import logging
    import tempfile
    from pathlib import Path
    
    with tempfile.TemporaryDirectory() as tmp:
        log_path = Path(tmp) / "app.log"
        logger = logging.getLogger("demo.file")
        logger.setLevel(logging.INFO)
        logger.propagate = False
        handler = logging.FileHandler(log_path)
        handler.setFormatter(logging.Formatter("%(levelname)s - %(message)s"))
        logger.addHandler(handler)
    
        logger.info("first")
        logger.warning("second")
        handler.close()
        logger.removeHandler(handler)
    
        print(log_path.read_text())
        assert log_path.read_text().splitlines() == ["INFO - first", "WARNING - second"]
    
  3. Build a logger with two handlers: a console handler (WARNING and up) and a file handler (DEBUG and up). Log a debug message and an exception from a division by zero, then confirm the file has both and the console only the error.

    💡 Hint

    Set levels on the logger and on each handler; use logger.exception inside except.

    ✅ Show solution
    import io
    import logging
    import tempfile
    from pathlib import Path
    
    with tempfile.TemporaryDirectory() as tmp:
        log_path = Path(tmp) / "detailed.log"
        logger = logging.getLogger("demo.multi")
        logger.setLevel(logging.DEBUG)
        logger.propagate = False
    
        console_stream = io.StringIO()               # stands in for the console
        console = logging.StreamHandler(console_stream)
        console.setLevel(logging.WARNING)
        file_handler = logging.FileHandler(log_path)
        file_handler.setLevel(logging.DEBUG)
        logger.addHandler(console)
        logger.addHandler(file_handler)
    
        logger.debug("only in the file")
        try:
            1 / 0
        except ZeroDivisionError:
            logger.exception("calculation failed")   # includes the traceback
    
        file_handler.close()
        file_text = log_path.read_text()
        assert "only in the file" in file_text and "calculation failed" in file_text
        assert "only in the file" not in console_stream.getvalue()
        assert "ZeroDivisionError" in console_stream.getvalue()
        print("console got:", console_stream.getvalue().splitlines()[0])
    

Found this useful?

All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.