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

feat: improve logging of reactive variable changes #517

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions solara/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,10 @@ def open_browser():

if log_level is not None:
LOGGING_CONFIG["loggers"]["solara"]["level"] = log_level.upper()
settings.main.log_level = log_level.upper()
# LOGGING_CONFIG["loggers"]["reacton"]["level"] = log_level.upper()
else:
settings.main.log_level = "ERROR"

log_level = log_level_uvicorn
del log_level_uvicorn
Expand Down
1 change: 1 addition & 0 deletions solara/server/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ class MainSettings(BaseSettings):
platform: str = sys.platform
host: str = HOST_DEFAULT
experimental_performance: bool = False
log_level: str = "INFO"

class Config:
env_prefix = "solara_"
Expand Down
42 changes: 41 additions & 1 deletion solara/toestand.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@

import solara
from solara import _using_solara_server
from solara.server import settings

T = TypeVar("T")
TS = TypeVar("TS")
S = TypeVar("S") # used for state
logger = logging.getLogger("solara.toestand")
solara_logger = logging.getLogger("solara")

_DEBUG = False

Expand Down Expand Up @@ -86,6 +88,41 @@ def __init__(self, merge: Callable = merge_state):
self.merge = merge
self.listeners: Dict[str, Set[Tuple[Callable[[T], None], Optional[ContextManager]]]] = defaultdict(set)
self.listeners2: Dict[str, Set[Tuple[Callable[[T, T], None], Optional[ContextManager]]]] = defaultdict(set)
if settings.main.log_level in ["DEBUG", "INFO"]:
import inspect

# All subclasses of ValueBase have at least two calls within this file
frame = sys._getframe(2)
while frame:
file = frame.f_code.co_filename
if not (
file.endswith("solara/toestand.py")
or file.endswith("solara/reactive.py")
or file.endswith("solara/hooks/use_reactive.py")
or file.endswith("reacton/core.py")
or file.endswith("components/markdown.py")
):
frame_info = inspect.getframeinfo(frame)
if frame_info.code_context is None or "=" not in frame_info.code_context[0]:
# For some reason MyPy complains about types even though both are FrameType | None
frame = frame.f_back # type: ignore
elif any(op in frame_info.code_context[0].split("=")[1].lower() for op in ["reactive", "use_memo", "computed", "singleton"]):
declaration = frame_info.code_context[0].split("=")[0].strip()
if ":" in declaration:
declaration = declaration.split(":")[0].strip()
self._varname: Optional[str] = declaration
logger.info("found varname: " + declaration)
break
# Markdown case is special, because the stacktrace ends at
# https://github.com/widgetti/solara/blob/604d2e54146308d64a209334d0314d2baba75108/solara/components/markdown.py#L368
elif file.endswith("components/markdown.py"):
self._varname = "markdown_content"
break
else:
frame = frame.f_back # type: ignore
if not hasattr(self, "_varname"):
logger.info("No varname found")
self._varname = None

@property
def lock(self):
Expand Down Expand Up @@ -130,7 +167,10 @@ def cleanup():
return cleanup

def fire(self, new: T, old: T):
logger.info("value change from %s to %s, will fire events", old, new)
if settings.main.log_level in ["DEBUG", "INFO"] and self._varname is not None:
logger.info(f"value of {self._varname if self._varname is not None else ''} changed from %s to %s, will fire events", old, new)
else:
logger.info("value changed from %s to %s, will fire events", old, new)
scope_id = self._get_scope_key()
scopes = set()
for listener, scope in self.listeners[scope_id].copy():
Expand Down
Loading