Logging#
Quick Start#
import logging
You will not see any output from the following cell since the default logging level is set to WARNING
.
logging.debug("Hello, World!")
logging.info("Hello?")
Any logging messages with a level of WARNING
or higher will be displayed in the console:
logging.warning("This is a warning!")
logging.error("An error occurred!")
logging.critical("Critical error!!!")
WARNING:root:This is a warning!
ERROR:root:An error occurred!
CRITICAL:root:Critical error!!!
Clean up by removing all handlers from the root logger:
logging.root.handlers = []
Custom Logger#
import logging
# Create a logger
logger = logging.getLogger(__name__)
# Set the logging level
logger.setLevel(logging.DEBUG)
# Create a formatter
formatter = logging.Formatter("%(asctime)s | %(levelname)s | %(name)s | %(message)s")
# Create a stream handler so that logs can be written to the console
stream_handler = logging.StreamHandler()
# Set the logging level
stream_handler.setLevel(logging.DEBUG)
# Set the formatter
stream_handler.setFormatter(formatter)
# Add the handler to the logger
logger.addHandler(stream_handler)
# Log!
logger.debug("Hello, World!")
2024-04-30 18:24:03,767 | DEBUG | __main__ | Hello, World!
Add a file handler to the logger so that logs can be written to a file:
import tempfile
from pathlib import Path
# Create a temporary directory
tmp_dir = tempfile.TemporaryDirectory()
# Temperature log file
tmp_log_path = Path(tmp_dir.name).joinpath("tmp.log")
# Create a file handler
file_handler = logging.FileHandler(tmp_log_path)
# Only keep logs from INFO and above
file_handler.setLevel(logging.INFO)
# Set the formatter
file_handler.setFormatter(formatter)
# Add the handler to the logger
logger.addHandler(file_handler)
# Log
logger.debug("This debug message is not important")
logger.info("This is a useful message")
# Read the log file
with open(tmp_log_path) as f:
content = f.read()
print("Log file content:")
print(content)
# Clean up the temporary directory
tmp_dir.cleanup()
2024-04-30 18:24:03,773 | DEBUG | __main__ | This debug message is not important
2024-04-30 18:24:03,773 | INFO | __main__ | This is a useful message
Log file content:
2024-04-30 18:24:03,773 | INFO | __main__ | This is a useful message
Note that only logs with a level of INFO
or higher will be written to the file in the example above.