From 7049c7391fff858c21402c80cd49e6b729edebf7 Mon Sep 17 00:00:00 2001
From: Anderson Bravalheri <andersonbravalheri@gmail.com>
Date: Wed, 23 Nov 2022 20:13:15 +0000
Subject: [PATCH 1/4] Add simple regression test for logging patches

---
 setuptools/tests/test_logging.py | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/setuptools/tests/test_logging.py b/setuptools/tests/test_logging.py
index a5ddd56df0..e9256395b2 100644
--- a/setuptools/tests/test_logging.py
+++ b/setuptools/tests/test_logging.py
@@ -1,4 +1,6 @@
+import inspect
 import logging
+import os
 
 import pytest
 
@@ -34,3 +36,18 @@ def test_verbosity_level(tmp_path, monkeypatch, flag, expected_level):
     log_level = logger.getEffectiveLevel()
     log_level_name = logging.getLevelName(log_level)
     assert log_level_name == expected_level
+
+
+def test_patching_does_not_cause_problems():
+    # Ensure `dist.log` is only patched if necessary
+
+    import setuptools.logging
+    from distutils import dist  # <- load distutils after all the patches take place
+
+    setuptools.logging.configure()
+
+    if os.getenv("SETUPTOOLS_USE_DISTUTILS", "local").lower() == "local":
+        # Modern logging infra, no problematic patching.
+        assert isinstance(dist.log, logging.Logger)
+    else:
+        assert inspect.ismodule(dist.log)

From 4c267c78a09128dc80be821654dd60b174d53d41 Mon Sep 17 00:00:00 2001
From: Anderson Bravalheri <andersonbravalheri@gmail.com>
Date: Wed, 23 Nov 2022 19:43:18 +0000
Subject: [PATCH 2/4] Replace condition to patch distutils.dist.log

As `distutils.log.Log` was backfilled for compatibility we no longer can
use this as a condition.
---
 setuptools/logging.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/setuptools/logging.py b/setuptools/logging.py
index e99c1b9d50..0653878fc0 100644
--- a/setuptools/logging.py
+++ b/setuptools/logging.py
@@ -1,4 +1,5 @@
 import sys
+import inspect
 import logging
 import distutils.log
 from . import monkey
@@ -22,7 +23,7 @@ def configure():
     handlers = err_handler, out_handler
     logging.basicConfig(
         format="{message}", style='{', handlers=handlers, level=logging.DEBUG)
-    if hasattr(distutils.log, 'Log'):
+    if inspect.ismodule(distutils.dist.log):
         monkey.patch_func(set_threshold, distutils.log, 'set_threshold')
         # For some reason `distutils.log` module is getting cached in `distutils.dist`
         # and then loaded again when patched,

From 580175342bc0ffc27ee114d0f4f8363b572d0910 Mon Sep 17 00:00:00 2001
From: Anderson Bravalheri <andersonbravalheri@gmail.com>
Date: Wed, 23 Nov 2022 19:47:42 +0000
Subject: [PATCH 3/4] Add news fragment

---
 changelog.d/3709.misc.rst | 2 ++
 1 file changed, 2 insertions(+)
 create mode 100644 changelog.d/3709.misc.rst

diff --git a/changelog.d/3709.misc.rst b/changelog.d/3709.misc.rst
new file mode 100644
index 0000000000..bf0aefe9df
--- /dev/null
+++ b/changelog.d/3709.misc.rst
@@ -0,0 +1,2 @@
+Fix condition to patch ``distutils.dist.log`` to only apply when using
+``distutils`` from the stdlib.

From a4db65ff4c5b3edd4739b0864f4e1641b37b3b87 Mon Sep 17 00:00:00 2001
From: Anderson Bravalheri <andersonbravalheri@gmail.com>
Date: Wed, 23 Nov 2022 20:25:01 +0000
Subject: [PATCH 4/4] Remove wrong comment

---
 setuptools/tests/test_logging.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/setuptools/tests/test_logging.py b/setuptools/tests/test_logging.py
index e9256395b2..aa2b502b6c 100644
--- a/setuptools/tests/test_logging.py
+++ b/setuptools/tests/test_logging.py
@@ -42,7 +42,7 @@ def test_patching_does_not_cause_problems():
     # Ensure `dist.log` is only patched if necessary
 
     import setuptools.logging
-    from distutils import dist  # <- load distutils after all the patches take place
+    from distutils import dist
 
     setuptools.logging.configure()