Repository: mkdocs/mkdocs

Issue:
Log excluded links only if referring page is included
I have several excluded folders in my site (via the `exclude_docs` setting). The excluded folders contain pages that refer to images and other files in the same folder. Would you consider not logging INFO messages for excluded targets when the referring page is also excluded?

For example, the three "Doc file ... contains a link" INFO lines below are repeated in the log every time I save a file and the site is rebuilt. These log entries are irrelevant, because the doc files are also excluded.

```
INFO    -  Building documentation...
INFO    -  Cleaning site directory
INFO    -  Doc file 'hw/hw4_draft.md' contains a link to 'hw/hw4_draft/rhody.py' which is excluded from the built site.
INFO    -  Doc file 'quiz/1a/written_v1.md' contains a link to 'quiz/1a/q1_code.svg' which is excluded from the built site.
INFO    -  Doc file 'quiz/1a/written_v2.md' contains a link to 'quiz/1a/q1_code.svg' which is excluded from the built site.
INFO    -  The following pages are being built only for the preview but will be excluded from `mkdocs build` per `exclude_docs`:
             - http://127.0.0.1:8000/cs149/hw/hw4_draft/
             - http://127.0.0.1:8000/cs149/quiz/1a/written_v1/
             - http://127.0.0.1:8000/cs149/quiz/1a/written_v2/
```


Discussion:

Resolving patch:
diff --git a/mkdocs/structure/pages.py b/mkdocs/structure/pages.py
--- a/mkdocs/structure/pages.py
+++ b/mkdocs/structure/pages.py
@@ -392,6 +392,9 @@ def path_to_url(self, url: str) -> str:
                 )
 
         if warning:
+            if self.file.inclusion.is_excluded():
+                warning_level = min(logging.INFO, warning_level)
+
             # There was no match, so try to guess what other file could've been intended.
             if warning_level > logging.DEBUG:
                 suggest_url = ''
@@ -414,7 +417,10 @@ def path_to_url(self, url: str) -> str:
         assert target_uri is not None
         assert target_file is not None
         if target_file.inclusion.is_excluded():
-            warning_level = min(logging.INFO, self.config.validation.links.not_found)
+            if self.file.inclusion.is_excluded():
+                warning_level = logging.DEBUG
+            else:
+                warning_level = min(logging.INFO, self.config.validation.links.not_found)
             warning = (
                 f"Doc file '{self.file.src_uri}' contains a link to "
                 f"'{target_uri}' which is excluded from the built site."


Test patch:
diff --git a/mkdocs/tests/build_tests.py b/mkdocs/tests/build_tests.py
--- a/mkdocs/tests/build_tests.py
+++ b/mkdocs/tests/build_tests.py
@@ -582,7 +582,7 @@ def _assert_build_logs(self, expected):
     @tempdir(
         files={
             'test/foo.md': 'page1 content, [bar](bar.md)',
-            'test/bar.md': 'page2 content, [baz](baz.md)',
+            'test/bar.md': 'page2 content, [baz](baz.md), [nonexistent](nonexistent.md)',
             'test/baz.md': 'page3 content, [foo](foo.md)',
             '.zoo.md': 'page4 content',
         }
@@ -609,7 +609,7 @@ def test_exclude_pages_with_invalid_links(self, site_dir, docs_dir):
         server = testing_server(site_dir, mount_path='/documentation/')
         with self.subTest(live_server=server):
             expected_logs = '''
-                INFO:Doc file 'test/bar.md' contains a link to 'test/baz.md' which is excluded from the built site.
+                INFO:Doc file 'test/bar.md' contains a relative link 'nonexistent.md', but the target 'test/nonexistent.md' is not found among documentation files.
                 INFO:Doc file 'test/foo.md' contains a link to 'test/bar.md' which is excluded from the built site.
                 INFO:The following pages are being built only for the preview but will be excluded from `mkdocs build` per `exclude_docs`:
                   - http://localhost:123/documentation/.zoo.html