Skip to content

Commit

Permalink
Worker: Adds override_logging option to use your own logging setup
Browse files Browse the repository at this point in the history
(Closes #46)
  • Loading branch information
ask committed Sep 4, 2019
1 parent 3dab5bb commit 71e11f3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
5 changes: 4 additions & 1 deletion mode/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ def __init__(
loghandlers: List[StreamHandler] = None,
blocking_timeout: Seconds = 10.0,
loop: asyncio.AbstractEventLoop = None,
override_logging: bool = True,
daemon: bool = True,
**kwargs: Any) -> None:
self.services = services
Expand All @@ -131,6 +132,7 @@ def __init__(
self.redirect_stdouts = redirect_stdouts
self.redirect_stdouts_level = logging.level_number(
redirect_stdouts_level or 'WARN')
self.override_logging = override_logging
if stdout is None:
stdout = sys.stdout
self.stdout = stdout
Expand Down Expand Up @@ -172,7 +174,8 @@ async def on_first_start(self) -> None:
await self.default_on_first_start()

async def default_on_first_start(self) -> None:
self._setup_logging()
if self.override_logging:
self._setup_logging()
await self.on_execute()
if self.debug:
await self._add_monitor()
Expand Down
19 changes: 15 additions & 4 deletions t/unit/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def test__say(self, worker):
worker._say('msg', file=file, foo=1)
print.assert_called_once_with('msg', file=file, foo=1, end='\n')

def tesT__say__default_file(self, worker):
def test__say__default_file(self, worker):
worker.quiet = False
with patch('builtins.print') as print:
worker._say('msg', file=None, end='.')
Expand All @@ -77,14 +77,17 @@ def test_on_init_dependencies(self, worker):
worker.services = workers
assert worker.on_init_dependencies() == workers

@pytest.mark.asyncio
async def test_on_first_start(self, worker):
worker.debug = False
def _setup_for_on_first_start(self, worker):
worker._setup_logging = Mock()
worker.on_execute = AsyncMock()
worker._add_monitor = AsyncMock()
worker.install_signal_handlers = Mock()

@pytest.mark.asyncio
async def test_on_first_start(self, worker):
self._setup_for_on_first_start(worker)
worker.debug = False

await worker.on_first_start()
worker._setup_logging.assert_called_once_with()
worker.on_execute.coro.assert_called_once_with()
Expand All @@ -95,6 +98,14 @@ async def test_on_first_start(self, worker):
await worker.on_first_start()
worker._add_monitor.coro.assert_called_once_with()

@pytest.mark.asyncio
async def test_on_first_start__override_logging(self):
worker = Worker(override_logging=False)
self._setup_for_on_first_start(worker)
await worker.on_first_start()

worker._setup_logging.assert_not_called()

@pytest.mark.asyncio
async def test_on_execute(self, worker):
await worker.on_execute()
Expand Down

0 comments on commit 71e11f3

Please sign in to comment.