mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
associate app with logger
This commit is contained in:
@@ -8,6 +8,7 @@ Exposes some commonly used symbols.
|
||||
from __future__ import annotations
|
||||
|
||||
import inspect
|
||||
import weakref
|
||||
from typing import TYPE_CHECKING, Callable
|
||||
|
||||
import rich.repr
|
||||
@@ -35,6 +36,8 @@ LogCallable: TypeAlias = "Callable"
|
||||
if TYPE_CHECKING:
|
||||
from importlib.metadata import version
|
||||
|
||||
from textual.app import App
|
||||
|
||||
__version__ = version("textual")
|
||||
"""The version of Textual."""
|
||||
|
||||
@@ -62,10 +65,17 @@ class Logger:
|
||||
log_callable: LogCallable | None,
|
||||
group: LogGroup = LogGroup.INFO,
|
||||
verbosity: LogVerbosity = LogVerbosity.NORMAL,
|
||||
app: App | None = None,
|
||||
) -> None:
|
||||
self._log = log_callable
|
||||
self._group = group
|
||||
self._verbosity = verbosity
|
||||
self._app = None if app is None else weakref.ref(app)
|
||||
|
||||
@property
|
||||
def app(self) -> App | None:
|
||||
"""The associated application, or `None` if there isn't one."""
|
||||
return None if self._app is None else self._app()
|
||||
|
||||
def __rich_repr__(self) -> rich.repr.Result:
|
||||
yield self._group, LogGroup.INFO
|
||||
@@ -82,16 +92,19 @@ class Logger:
|
||||
|
||||
with open(constants.LOG_FILE, "a") as log_file:
|
||||
print(output, file=log_file)
|
||||
try:
|
||||
app = active_app.get()
|
||||
except LookupError:
|
||||
if constants.DEBUG:
|
||||
print_args = (
|
||||
*args,
|
||||
*[f"{key}={value!r}" for key, value in kwargs.items()],
|
||||
)
|
||||
print(*print_args)
|
||||
return
|
||||
|
||||
app = self.app
|
||||
if app is None:
|
||||
try:
|
||||
app = active_app.get()
|
||||
except LookupError:
|
||||
if constants.DEBUG:
|
||||
print_args = (
|
||||
*args,
|
||||
*[f"{key}={value!r}" for key, value in kwargs.items()],
|
||||
)
|
||||
print(*print_args)
|
||||
return
|
||||
if app.devtools is None or not app.devtools.is_connected:
|
||||
return
|
||||
|
||||
@@ -129,52 +142,52 @@ class Logger:
|
||||
New logger.
|
||||
"""
|
||||
verbosity = LogVerbosity.HIGH if verbose else LogVerbosity.NORMAL
|
||||
return Logger(self._log, self._group, verbosity)
|
||||
return Logger(self._log, self._group, verbosity, app=self.app)
|
||||
|
||||
@property
|
||||
def verbose(self) -> Logger:
|
||||
"""A verbose logger."""
|
||||
return Logger(self._log, self._group, LogVerbosity.HIGH)
|
||||
return Logger(self._log, self._group, LogVerbosity.HIGH, app=self.app)
|
||||
|
||||
@property
|
||||
def event(self) -> Logger:
|
||||
"""Logs events."""
|
||||
return Logger(self._log, LogGroup.EVENT)
|
||||
return Logger(self._log, LogGroup.EVENT, app=self.app)
|
||||
|
||||
@property
|
||||
def debug(self) -> Logger:
|
||||
"""Logs debug messages."""
|
||||
return Logger(self._log, LogGroup.DEBUG)
|
||||
return Logger(self._log, LogGroup.DEBUG, app=self.app)
|
||||
|
||||
@property
|
||||
def info(self) -> Logger:
|
||||
"""Logs information."""
|
||||
return Logger(self._log, LogGroup.INFO)
|
||||
return Logger(self._log, LogGroup.INFO, app=self.app)
|
||||
|
||||
@property
|
||||
def warning(self) -> Logger:
|
||||
"""Logs warnings."""
|
||||
return Logger(self._log, LogGroup.WARNING)
|
||||
return Logger(self._log, LogGroup.WARNING, app=self.app)
|
||||
|
||||
@property
|
||||
def error(self) -> Logger:
|
||||
"""Logs errors."""
|
||||
return Logger(self._log, LogGroup.ERROR)
|
||||
return Logger(self._log, LogGroup.ERROR, app=self.app)
|
||||
|
||||
@property
|
||||
def system(self) -> Logger:
|
||||
"""Logs system information."""
|
||||
return Logger(self._log, LogGroup.SYSTEM)
|
||||
return Logger(self._log, LogGroup.SYSTEM, app=self.app)
|
||||
|
||||
@property
|
||||
def logging(self) -> Logger:
|
||||
"""Logs from stdlib logging module."""
|
||||
return Logger(self._log, LogGroup.LOGGING)
|
||||
return Logger(self._log, LogGroup.LOGGING, app=self.app)
|
||||
|
||||
@property
|
||||
def worker(self) -> Logger:
|
||||
"""Logs worker information."""
|
||||
return Logger(self._log, LogGroup.WORKER)
|
||||
return Logger(self._log, LogGroup.WORKER, app=self.app)
|
||||
|
||||
|
||||
log = Logger(None)
|
||||
|
||||
@@ -682,7 +682,7 @@ class App(Generic[ReturnType], DOMNode):
|
||||
will be ignored.
|
||||
"""
|
||||
|
||||
self._logger = Logger(self._log)
|
||||
self._logger = Logger(self._log, app=self)
|
||||
|
||||
self._css_has_errors = False
|
||||
|
||||
|
||||
Reference in New Issue
Block a user