Skip to content

Logging

Import packages

import logging

Usage

Basic config

logging.basicConfig(
    # filename="app.log",
    # filemode="w",
    level=logging.DEBUG,
    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
    datefmt="%Y-%m-%d %H:%M:%S",
    handlers=[
        logging.StreamHandler(),
        # logging.FileHandler("example.log")
    ],
    force=True,  # Jupyter on VSCode
)

logger = logging.getLogger()

Message

1
2
3
4
5
logger.debug("Debugging")
logger.info("Don't worry, this is just an info message")
logger.warning("I'm warning you!")
logger.error("This was a mistake!")
logger.critical("Well, this was not meant to happen")

2025-09-22 18:55:59 - root - DEBUG - Debugging

2025-09-22 18:55:59 - root - INFO - Don't worry, this is just an info message

2025-09-22 18:55:59 - root - WARNING - I'm warning you!

2025-09-22 18:55:59 - root - ERROR - This was a mistake!

2025-09-22 18:55:59 - root - CRITICAL - Well, this was not meant to happen

Multiple loggers

1
2
3
4
5
debugging_logger = logging.getLogger("debugging_logger")
debugging_logger.setLevel(logging.DEBUG)

warning_logger = logging.getLogger("warning_logger")
warning_logger.setLevel(logging.WARNING)
debugging_logger.debug("Debugging")
warning_logger.warning("Warning")

2025-09-22 19:01:16 - debugging_logger - DEBUG - Debugging

2025-09-22 19:01:16 - warning_logger - WARNING - Warning