Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions faust/cli/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ class State:
loglevel: Optional[int] = None
blocking_timeout: Optional[float] = None
console_port: Optional[int] = None
override_logging: Optional[bool] = None


def compat_option(
Expand Down Expand Up @@ -208,6 +209,17 @@ def _callback(
type=params.TCPPort(),
help="when --debug: Port to run debugger console on.",
),
compat_option(
"--override-logging/--no-override-logging",
state_key="override_logging",
default=True,
help=(
"Reconfigure the root logger when the worker starts "
"(clears any existing handlers, e.g. from structlog or "
"logging.basicConfig). Pass --no-override-logging to leave "
"your own logging setup untouched."
),
),
]

core_options: OptionSequence = [
Expand Down Expand Up @@ -509,6 +521,7 @@ class Command(abc.ABC): # noqa: B024
_loglevel: Optional[str]
_blocking_timeout: Optional[float]
_console_port: Optional[int]
_override_logging: Optional[bool]

stdout: IO
stderr: IO
Expand Down Expand Up @@ -592,6 +605,7 @@ def __init__(self, ctx: click.Context, *args: Any, **kwargs: Any) -> None:
self._loglevel = self.state.loglevel
self._blocking_timeout = self.state.blocking_timeout
self._console_port = self.state.console_port
self._override_logging = self.state.override_logging

@no_type_check # Subclasses can omit *args, **kwargs in signature. # noqa: B027
async def run(self, *args: Any, **kwargs: Any) -> Any: # noqa: B027
Expand Down Expand Up @@ -662,6 +676,7 @@ def worker_for_service(
redirect_stdouts_level=self.redirect_stdouts_level,
loop=loop or asyncio.get_event_loop_policy().get_event_loop(),
daemon=self.daemon,
override_logging=self.override_logging,
)

@property
Expand Down Expand Up @@ -772,6 +787,15 @@ def console_port(self) -> int:
def console_port(self, port: int) -> None:
self._console_port = port

@property
def override_logging(self) -> bool:
"""Return whether the worker should reconfigure the root logger."""
return True if self._override_logging is None else self._override_logging

@override_logging.setter
def override_logging(self, override: bool) -> None:
self._override_logging = override


class AppCommand(Command):
"""Command that takes ``-A app`` as argument."""
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/cli/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ def test_worker_for_service(self, *, command):
redirect_stdouts_level=command.redirect_stdouts_level,
loop=loop,
daemon=command.daemon,
override_logging=command.override_logging,
)

def test__Worker(self, *, command):
Expand Down Expand Up @@ -471,6 +472,13 @@ def test_console_port(self, *, command, ctx):
command.console_port = None
assert command.console_port == CONSOLE_PORT

def test_override_logging(self, *, command, ctx):
assert command.override_logging == ctx.ensure_object().override_logging
command.override_logging = False
assert command.override_logging is False
command.override_logging = None
assert command.override_logging is True


class Test_AppCommand:
@pytest.fixture()
Expand Down
Loading