logging.basicConfig() vs logging.getLogger()
logging.basicConfig() and logging.getLogger() are both Python Logging Module APIs, and choosing between them is a common point of confusion. This page compares their syntax, parameters, return values, and typical use cases so you can pick the right one. logging.basicConfig(): Configures the root logger with a basic configuration. Its syntax is logging.basicConfig(*, filename=None, filemode="a", format=None, datefmt=None, level=None, stream=None, style="%", handlers=None), and it returns none logging.getLogger(): Returns a logger with the specified name or the root logger if name is None. Its syntax is logging.getLogger(name=None), and it returns logger object Example of logging.basicConfig(): import logging
logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
logging.info("Server started"). Example of logging.getLogger(): logger = logging.getLogger(__name__)
logger.info("This is a log message"). Read the full references for logging.basicConfig() and logging.getLogger() to see parameters, edge cases, and example code side by side. Other Logging Module APIs worth comparing: logger.debug(), logger.info(), logger.warning(), logger.error(), logger.critical(), logger.exception(). Both belong to the Logging Module category in the Python Reference documentation.