Skip to content

Commit 7a27572

Browse files
author
Oleh Prypin
authored
Isolate strict warning counter to just the ongoing build (#2607)
Refactor to eliminate global state. Fixes the issue with the webserver's warnings being able to abort the build.
1 parent 5754091 commit 7a27572

3 files changed

Lines changed: 14 additions & 11 deletions

File tree

mkdocs/__main__.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,6 @@ def __init__(self, log_name='mkdocs', level=logging.INFO):
7272
self.stream.name = 'MkDocsStreamHandler'
7373
self.logger.addHandler(self.stream)
7474

75-
# Add CountHandler for strict mode
76-
self.counter = utils.log_counter
77-
self.counter.setLevel(logging.WARNING)
78-
self.logger.addHandler(self.counter)
79-
8075

8176
pass_state = click.make_pass_decorator(State, ensure=True)
8277

mkdocs/commands/build.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,16 @@ def _build_page(page, config, doc_files, nav, env, dirty=False):
240240

241241
def build(config, live_server=False, dirty=False):
242242
""" Perform a full site build. """
243-
try:
244243

244+
logger = logging.getLogger('mkdocs')
245+
246+
# Add CountHandler for strict mode
247+
warning_counter = utils.CountHandler()
248+
warning_counter.setLevel(logging.WARNING)
249+
if config['strict']:
250+
logging.getLogger('mkdocs').addHandler(warning_counter)
251+
252+
try:
245253
from time import time
246254
start = time()
247255

@@ -308,8 +316,8 @@ def build(config, live_server=False, dirty=False):
308316
# Run `post_build` plugin events.
309317
config['plugins'].run_event('post_build', config=config)
310318

311-
counts = utils.log_counter.get_counts()
312-
if config['strict'] and len(counts):
319+
counts = warning_counter.get_counts()
320+
if counts:
313321
msg = ', '.join([f'{v} {k.lower()}s' for k, v in counts])
314322
raise Abort(f'\nAborted with {msg} in strict mode!')
315323

@@ -323,6 +331,9 @@ def build(config, live_server=False, dirty=False):
323331
raise Abort('\nAborted with a BuildError!')
324332
raise
325333

334+
finally:
335+
logger.removeHandler(warning_counter)
336+
326337

327338
def site_directory_contains_stale_files(site_directory):
328339
""" Check if the site directory contains stale files from a previous build. """

mkdocs/utils/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,6 @@ def get_counts(self):
442442
return [(logging.getLevelName(k), v) for k, v in sorted(self.counts.items(), reverse=True)]
443443

444444

445-
# A global instance to use throughout package
446-
log_counter = CountHandler()
447-
448445
# For backward compatibility as some plugins import it.
449446
# It is no longer necessary as all messages on the
450447
# `mkdocs` logger get counted automatically.

0 commit comments

Comments
 (0)