Skip to content

Commit

Permalink
Merge pull request #287 from thegridelectric/dev
Browse files Browse the repository at this point in the history
Logging refinements
  • Loading branch information
anschweitzer authored Jan 14, 2025
2 parents b41064a + 2432cec commit e2ccf18
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "gridworks-proactor"
version = "1.1.4"
version = "1.1.5"
description = "Gridworks Proactor"
authors = ["Jessica Millar <[email protected]>"]
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion src/gwproactor/links/reuploads.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def process_ack_for_reupload(self, ack_id: str) -> list[str]:
path_dbg |= 0x00000008
self.stats.complete_reupload()
self._logger.comm_event(
f"Reupload completed. Total started: {self.stats.reupload_counts.started} "
f"Reupload completed. Reuploads started: {self.stats.reupload_counts.started} "
f"completed: {self.stats.reupload_counts.completed}."
)
if self._logger.path_enabled:
Expand Down
33 changes: 23 additions & 10 deletions src/gwproactor/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,18 +214,31 @@ def add_category_logger(
"ERROR. add_category_logger() requires category value "
"unless logger is provided."
)
logger = logging.getLogger(self.category_logger_name(category))
logger.setLevel(level)
logger = self.category_logger(category)
if logger is None:
logger = logging.getLogger(self.category_logger_name(category))
logger.setLevel(level)
self.category_loggers[category] = CategoryLoggerInfo(
logger=logger,
default_level=level,
)
else:
category = logger.name
self_prefix = f"{self.name}."
if category.startswith(self_prefix):
category = category[len(self_prefix) :]
if not category:
category = logger.name
self_prefix = f"{self.name}."
if category.startswith(self_prefix):
category = category[len(self_prefix) :]
if category in self.category_loggers:
raise ValueError(
"ERROR. add_category_logger() got explicit logger "
f"named {logger.name}, categorized as {category}, but "
f"logger for that category is already present."
)
level = logger.getEffectiveLevel()
self.category_loggers[category] = CategoryLoggerInfo(
logger=logger,
default_level=level,
)
self.category_loggers[category] = CategoryLoggerInfo(
logger=logger,
default_level=level,
)
return logger

def reset_default_category_levels(self) -> None:
Expand Down
9 changes: 9 additions & 0 deletions src/gwproactor/persister.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ def pending_ids(self) -> list[str]:
def num_pending(self) -> int:
"""Get number of pending uids"""

@property
@abstractmethod
def curr_bytes(self) -> int:
"""Return number of bytes used to store events, if known."""

@abstractmethod
def __contains__(self, uid: str) -> bool:
"""Check whether a uid is pending"""
Expand Down Expand Up @@ -132,6 +137,10 @@ def pending_ids(self) -> list[str]:
def num_pending(self) -> int:
return 0

@property
def curr_bytes(self) -> int:
return 0

def __contains__(self, uid: str) -> bool:
return False

Expand Down
16 changes: 13 additions & 3 deletions src/gwproactor/proactor_implementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,18 @@ def __init__(
)
self._layout = hardware_layout
self._node = self._layout.node(name)
self._logger = ProactorLogger(**settings.logging.qualified_logger_names())
self._logger = self.make_logger(settings)
self._stats = self.make_stats()
self._event_persister = self.make_event_persister(settings)
self._logger.lifecycle(f"Proactor {self._name} reindexing events")
self._logger.lifecycle(f"Proactor <{self._name}> reindexing events")
reindex_result = self._event_persister.reindex()
self._logger.lifecycle(f"Proactor {self._name} reindexing complete")
self._logger.lifecycle(
f"Proactor <{self._name}> reindexing complete.\n"
f" {self._event_persister.num_pending} events present for upload, "
f"using approximately {int(self._event_persister.curr_bytes / 1024)} KB / "
f"{round(self._event_persister.curr_bytes / 1024 / 1024, 1)} MB "
f"storage space."
)
if reindex_result.is_err():
self._reindex_problems = reindex_result.err()
self._logger.error("ERROR in event persister reindex():")
Expand Down Expand Up @@ -161,6 +167,10 @@ def __init__(
def make_stats(cls) -> ProactorStats:
return ProactorStats()

@classmethod
def make_logger(cls, settings: ProactorSettings) -> ProactorLogger:
return ProactorLogger(**settings.logging.qualified_logger_names())

@classmethod
def make_event_persister(cls, settings: ProactorSettings) -> PersisterInterface: # noqa: ARG003
return StubPersister()
Expand Down
5 changes: 1 addition & 4 deletions src/gwproactor/problems.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ def __str__(self) -> str:
if lst:
s += f"\n{attr_name.capitalize()}:\n"
for i, entry in enumerate(lst):
entry_str = str(entry)
if not entry_str:
entry_str = entry.__class__.__name__
s += f" {i:2d}: {entry_str}\n"
s += f" {i:2d}: <{entry.__class__.__name__}> " f"<{entry}>\n"
return s
return ""

Expand Down
8 changes: 8 additions & 0 deletions src/gwproactor/sync_thread.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
"""Classes providing interaction between synchronous and asynchronous code"""

import asyncio
import logging
import queue
import threading
import time
import traceback
from abc import ABC
from typing import Any, Optional

from gwproactor.logger import LoggerOrAdapter
from gwproactor.message import InternalShutdownMessage, PatInternalWatchdogMessage

DEFAULT_STEP_DURATION = 0.1
Expand Down Expand Up @@ -141,6 +143,7 @@ class SyncAsyncInteractionThread(threading.Thread, ABC):
_responsive_sleep_step_seconds: float
pat_timeout: Optional[float]
_last_pat_time: float
_logger: LoggerOrAdapter

def __init__( # noqa: PLR0913
self,
Expand All @@ -151,6 +154,7 @@ def __init__( # noqa: PLR0913
responsive_sleep_step_seconds: float = SLEEP_STEP_SECONDS,
pat_timeout: Optional[float] = PAT_TIMEOUT,
daemon: bool = True,
logger: Optional[LoggerOrAdapter] = None,
) -> None:
super().__init__(name=name, daemon=daemon)
if channel is None:
Expand All @@ -162,6 +166,10 @@ def __init__( # noqa: PLR0913
self.running = None
self.pat_timeout = pat_timeout
self._last_pat_time = 0.0
if logger is None:
self._logger = logging.getLogger(__package__)
else:
self._logger = logger

def _preiterate(self) -> None:
pass
Expand Down

0 comments on commit e2ccf18

Please sign in to comment.