Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not initialize logging on import #8634

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
except Exception:
pass

from distributed.config import ensure_logging_configured

ensure_logging_configured()

# Make all fixtures available
from distributed.utils_test import * # noqa

Expand Down
12 changes: 11 additions & 1 deletion distributed/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging.config
import os
import sys
import threading
from collections.abc import Callable
from typing import Any

Expand Down Expand Up @@ -212,4 +213,13 @@ def get_loop_factory() -> Callable[[], asyncio.AbstractEventLoop] | None:
)


initialize_logging(dask.config.config)
_LOGGING_CONFIGURED = False
_LOG_CONFIGURE_LOCK = threading.Lock()


def ensure_logging_configured() -> None:
global _LOGGING_CONFIGURED
with _LOG_CONFIGURE_LOCK:
if not _LOGGING_CONFIGURED:
_LOGGING_CONFIGURED = True
initialize_logging(dask.config.config)
2 changes: 2 additions & 0 deletions distributed/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
)
from distributed.comm.core import Listener
from distributed.compatibility import PeriodicCallback
from distributed.config import ensure_logging_configured
from distributed.counter import Counter
from distributed.diskutils import WorkDir, WorkSpace
from distributed.metrics import context_meter, time
Expand Down Expand Up @@ -647,6 +648,7 @@ async def start_unsafe(self):

@final
async def start(self):
ensure_logging_configured()
async with self._startup_lock:
if self.status == Status.failed:
assert self.__startup_exc is not None
Expand Down
2 changes: 2 additions & 0 deletions distributed/deploy/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from dask.widgets import get_template

from distributed.compatibility import PeriodicCallback
from distributed.config import ensure_logging_configured
from distributed.core import Status
from distributed.deploy.adaptive import Adaptive
from distributed.metrics import time
Expand Down Expand Up @@ -67,6 +68,7 @@ def __init__(
name=None,
scheduler_sync_interval=1,
):
ensure_logging_configured()
self._loop_runner = LoopRunner(loop=loop, asynchronous=asynchronous)
self.__asynchronous = asynchronous

Expand Down
14 changes: 12 additions & 2 deletions distributed/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ def test_logging_simple_under_distributed():
import logging
import dask

from distributed.config import ensure_logging_configured
ensure_logging_configured()
from distributed.utils_test import captured_handler

d = logging.getLogger('distributed')
Expand Down Expand Up @@ -116,6 +118,8 @@ def test_logging_simple():
import logging
import dask

from distributed.config import ensure_logging_configured
ensure_logging_configured()
from distributed.utils_test import captured_handler

d = logging.getLogger('distributed')
Expand Down Expand Up @@ -172,6 +176,8 @@ def test_logging_extended():
code = """if 1:
import logging

from distributed.config import ensure_logging_configured
ensure_logging_configured()
from distributed.utils_test import captured_handler

root = logging.getLogger()
Expand Down Expand Up @@ -205,6 +211,8 @@ def test_default_logging_does_not_override_basic_config():
import logging
logging.basicConfig()
import distributed
from distributed.config import ensure_logging_configured
ensure_logging_configured()
logging.getLogger("distributed").warning("hello")
"""
)
Expand All @@ -220,7 +228,8 @@ def test_basic_config_does_not_override_default_logging():
"""\
import logging
import distributed

from distributed.config import ensure_logging_configured
ensure_logging_configured()
logging.basicConfig()
logging.getLogger("distributed").warning("hello")
"""
Expand Down Expand Up @@ -287,7 +296,8 @@ def test_logging_file_config():
with new_config_file(dask_config):
code = """if 1:
import logging
from distributed import config
from distributed.config import ensure_logging_configured
ensure_logging_configured()
foo = logging.getLogger('foo')
bar = logging.getLogger('foo.bar')
assert logging.INFO == foo.getEffectiveLevel()
Expand Down
3 changes: 2 additions & 1 deletion distributed/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
import tblib.pickling_support

from distributed.compatibility import asyncio_run
from distributed.config import get_loop_factory
from distributed.config import ensure_logging_configured, get_loop_factory

try:
import resource
Expand Down Expand Up @@ -893,6 +893,7 @@ def silence_logging_cmgr(
"""
Temporarily change all StreamHandlers for the given logger to the given level
"""
ensure_logging_configured()
if isinstance(level, str):
level = getattr(logging, level.upper())

Expand Down
Loading