Loggers are never instantiated directly, but always through the module-level function logging.getLogger(name). Multiple calls to getLogger() with the same name will always return a reference to the same Logger object.
import logging
import other_modules
from config_log_ziwen import setup_logging
setup_logging(app_name="my_app")
logger = logging.getLogger(__name__)
if __name__ == "__main__":
logger.info("Hello, world!")
import logging
logger = logging.getLogger(__name__)
logger.info("Hello, world!")
def setup_logging(
app_name: str = "my_app",
log_dir: str = "logs",
disable_existing_loggers: bool = False,
default_level: int = logging.DEBUG,
file_level: int = logging.DEBUG,
console_level: int = logging.DEBUG,
max_bytes: int = 10 * 1024 * 1024, # 10MB
backup_count: int = 10,
) -> None: