[Mlir-commits] [mlir] 35d26be - Don't use root logger at import time

Stella Laurenzo llvmlistbot at llvm.org
Mon Dec 5 17:56:24 PST 2022


Author: Stella Laurenzo
Date: 2022-12-05T17:42:38-08:00
New Revision: 35d26be21976ce1a63f24d41c969996776ee16bf

URL: https://github.com/llvm/llvm-project/commit/35d26be21976ce1a63f24d41c969996776ee16bf
DIFF: https://github.com/llvm/llvm-project/commit/35d26be21976ce1a63f24d41c969996776ee16bf.diff

LOG: 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

Added: 
    

Modified: 
    mlir/python/mlir/_mlir_libs/__init__.py

Removed: 
    


################################################################################
diff  --git a/mlir/python/mlir/_mlir_libs/__init__.py b/mlir/python/mlir/_mlir_libs/__init__.py
index b140ad64e64cd..9ceeef81844c2 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 process_initializer_module(module_name):
       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
 


        


More information about the Mlir-commits mailing list