From 35d26be21976ce1a63f24d41c969996776ee16bf Mon Sep 17 00:00:00 2001 From: Stella Laurenzo Date: Mon, 5 Dec 2022 17:42:38 -0800 Subject: [PATCH] Don't use root logger at import time At import time, these calls to `logging.debug()` implicitly call `logging.basicConfig` (https://docs.python.org/3/library/logging.html#logging.basicConfig), setting logging config for the whole project which cannot then be overwritten later. For instance, consider the following test script: ``` import logging import jax logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) logger.info('info') ``` This should log out `'info'`, but because when `import jax` is called, this `_mlir_lib/__init__.py` file is run and a `logging.debug` is called, calling `logging.basicConfig`, my `logging.basicConfig(level=logging.INFO)` does nothing. Fix: instead of using root logger, use a module level logger. Found in this issue: https://github.com/google/jax/issues/12526 Reviewed By: stellaraccident Differential Revision: https://reviews.llvm.org/D134812 --- mlir/python/mlir/_mlir_libs/__init__.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/mlir/python/mlir/_mlir_libs/__init__.py b/mlir/python/mlir/_mlir_libs/__init__.py index b140ad6..9ceeef8 100644 --- a/mlir/python/mlir/_mlir_libs/__init__.py +++ b/mlir/python/mlir/_mlir_libs/__init__.py @@ -54,6 +54,7 @@ def _site_initialize(): import itertools import logging from ._mlir import ir + logger = logging.getLogger(__name__) registry = ir.DialectRegistry() post_init_hooks = [] @@ -66,14 +67,14 @@ def _site_initialize(): message = (f"Error importing mlir initializer {module_name}. This may " "happen in unclean incremental builds but is likely a real bug if " "encountered otherwise and the MLIR Python API may not function.") - logging.warning(message, exc_info=True) + logger.warning(message, exc_info=True) - logging.debug("Initializing MLIR with module: %s", module_name) + logger.debug("Initializing MLIR with module: %s", module_name) if hasattr(m, "register_dialects"): - logging.debug("Registering dialects from initializer %r", m) + logger.debug("Registering dialects from initializer %r", m) m.register_dialects(registry) if hasattr(m, "context_init_hook"): - logging.debug("Adding context init hook from %r", m) + logger.debug("Adding context init hook from %r", m) post_init_hooks.append(m.context_init_hook) return True -- 2.7.4