From 93c3567abeb7d19818767d3d20977e5e9ee15ca3 Mon Sep 17 00:00:00 2001 From: Stu Hood Date: Wed, 12 Feb 2020 21:07:29 -0800 Subject: [PATCH] Fix warnings filtering under pantsd. (#9121) ### Problem The warnings-filtering facility provided by `--ignore-pants-warnings` was not being enabled in the pantsd subprocess, meaning that warnings were not filtered while pantsd was in use. ### Solution Move warnings filtering into `setup_logging` to ensure that it is executed in all relevant contexts. ### Result Warnings are properly filtered with/without pantsd. --- src/python/pants/bin/pants_runner.py | 4 ---- src/python/pants/init/logging.py | 25 +++++++++++++++++++++++-- src/python/pants/pantsd/pants_daemon.py | 8 +++++++- src/python/pants/subsystem/subsystem.py | 2 +- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/python/pants/bin/pants_runner.py b/src/python/pants/bin/pants_runner.py index bf70f5f26af..0978ad1c846 100644 --- a/src/python/pants/bin/pants_runner.py +++ b/src/python/pants/bin/pants_runner.py @@ -4,7 +4,6 @@ import logging import os import sys -import warnings from typing import List, Mapping, Optional from pants.base.exception_sink import ExceptionSink @@ -90,9 +89,6 @@ def run(self): ExceptionSink.reset_should_print_backtrace_to_terminal(global_bootstrap_options.print_exception_stacktrace) ExceptionSink.reset_log_location(global_bootstrap_options.pants_workdir) - for message_regexp in global_bootstrap_options.ignore_pants_warnings: - warnings.filterwarnings(action='ignore', message=message_regexp) - # TODO https://github.com/pantsbuild/pants/issues/7205 if self._should_run_with_pantsd(global_bootstrap_options): try: diff --git a/src/python/pants/init/logging.py b/src/python/pants/init/logging.py index 792a568ae8d..f845f6e7150 100644 --- a/src/python/pants/init/logging.py +++ b/src/python/pants/init/logging.py @@ -6,6 +6,7 @@ import logging import os import sys +import warnings from collections import namedtuple from contextlib import contextmanager from logging import StreamHandler @@ -64,7 +65,13 @@ def setup_logging_from_options(bootstrap_options): # N.B. quiet help says 'Squelches all console output apart from errors'. level = 'ERROR' if bootstrap_options.quiet else bootstrap_options.level.upper() native = Native() - return setup_logging(level, console_stream=sys.stderr, log_dir=bootstrap_options.logdir, native=native) + return setup_logging( + level, + console_stream=sys.stderr, + log_dir=bootstrap_options.logdir, + native=native, + warnings_filter_regexes=bootstrap_options.ignore_pants_warnings + ) class NativeHandler(StreamHandler): @@ -130,7 +137,16 @@ def encapsulated_global_logger(): global_logger.addHandler(handler) -def setup_logging(level, console_stream=None, log_dir=None, scope=None, log_name=None, native=None): +def setup_logging( + level, + *, + console_stream=None, + log_dir=None, + scope=None, + log_name=None, + native=None, + warnings_filter_regexes=None, +): """Configures logging for a given scope, by default the global scope. :param str level: The logging level to enable, must be one of the level names listed here: @@ -146,6 +162,8 @@ def setup_logging(level, console_stream=None, log_dir=None, scope=None, log_name configured. :param str log_name: The base name of the log file (defaults to 'pants.log'). :param Native native: An instance of the Native FFI lib, to register rust logging. + :param list warnings_filter_regexes: A series of regexes to ignore warnings for, typically from + the `ignore_pants_warnings` option. :returns: The full path to the main log file if file logging is configured or else `None`. :rtype: str """ @@ -190,6 +208,9 @@ def trace(self, message, *args, **kwargs): # This routes warnings through our loggers instead of straight to raw stderr. logging.captureWarnings(True) + for message_regexp in (warnings_filter_regexes or ()): + warnings.filterwarnings(action='ignore', message=message_regexp) + _maybe_configure_extended_logging(logger) return LoggingSetupResult(log_filename, file_handler) diff --git a/src/python/pants/pantsd/pants_daemon.py b/src/python/pants/pantsd/pants_daemon.py index 8d4aaf27e70..49e4d0ecb80 100644 --- a/src/python/pants/pantsd/pants_daemon.py +++ b/src/python/pants/pantsd/pants_daemon.py @@ -316,7 +316,13 @@ def _pantsd_logging(self): with stdio_as(stdin_fd=-1, stdout_fd=-1, stderr_fd=-1): # Reinitialize logging for the daemon context. init_rust_logger(self._log_level, self._log_show_rust_3rdparty) - result = setup_logging(self._log_level, log_dir=self._log_dir, log_name=self.LOG_NAME, native=self._native) + result = setup_logging( + self._log_level, + log_dir=self._log_dir, + log_name=self.LOG_NAME, + native=self._native, + warnings_filter_regexes=self._bootstrap_options.for_global_scope(), + ) self._native.override_thread_logging_destination_to_just_pantsd() # Do a python-level redirect of stdout/stderr, which will not disturb `0,1,2`. diff --git a/src/python/pants/subsystem/subsystem.py b/src/python/pants/subsystem/subsystem.py index 8e803756f57..b1aafdad9bd 100644 --- a/src/python/pants/subsystem/subsystem.py +++ b/src/python/pants/subsystem/subsystem.py @@ -50,7 +50,7 @@ class Subsystem(SubsystemClientMixin, Optionable): class UninitializedSubsystemError(SubsystemError): def __init__(self, class_name, scope): super().__init__( - f'Subsystem "{class_name}" not initialized for scope "{scope}". Is subsystem missing' + f'Subsystem "{class_name}" not initialized for scope "{scope}". Is subsystem missing ' 'from subsystem_dependencies() in a task? ' )