Yes, in Python, the standard and most widely used library for logging is the built-in logging
module. You don’t need to install anything — it comes with Python by default.
import logging
import logging
logging.basicConfig(level=logging.INFO)
logging.info("This is an info message")
logging.warning("This is a warning")
logging.error("This is an error message")
logging
ModuleMultiple severity levels: DEBUG
, INFO
, WARNING
, ERROR
, CRITICAL
Logging to console or file
Custom formatting
Log rotation using logging.handlers
import logging
logging.basicConfig(
filename='app.log',
filemode='a', # append mode
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logging.debug("Debug message")
logging.info("Info message")
logging.error("Error message")
If you need more advanced logging features, consider:
loguru
A modern and easier-to-use alternative to logging
.
pip install loguru
from loguru import logger
logger.info("This is a loguru info message")
structlog
(for structured logging, often used in async apps or microservices)