Skip to content

Commit

Permalink
Fix warnings filtering under pantsd. (#9121)
Browse files Browse the repository at this point in the history
### 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.
  • Loading branch information
stuhood authored Feb 13, 2020
1 parent 7136d9b commit 93c3567
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
4 changes: 0 additions & 4 deletions src/python/pants/bin/pants_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
25 changes: 23 additions & 2 deletions src/python/pants/init/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import logging
import os
import sys
import warnings
from collections import namedtuple
from contextlib import contextmanager
from logging import StreamHandler
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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
"""
Expand Down Expand Up @@ -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)
8 changes: 7 additions & 1 deletion src/python/pants/pantsd/pants_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/subsystem/subsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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? '
)

Expand Down

0 comments on commit 93c3567

Please sign in to comment.