Skip to content

Commit

Permalink
maybe a better fix to the logging filter
Browse files Browse the repository at this point in the history
  • Loading branch information
MAKOMO committed Jan 20, 2025
1 parent bd61636 commit 6e3f550
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
34 changes: 18 additions & 16 deletions src/artisanlib/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
import zlib
import logging.config
from yaml import safe_load as yaml_load
from typing import Final, Optional, Mapping, List, Dict, Tuple, Union, cast, Any, Callable, TYPE_CHECKING #for Python >= 3.9: can remove 'List' since type hints can now use the generic 'list'
from typing import Final, Optional, List, Dict, Tuple, Union, cast, Any, Callable, TYPE_CHECKING #for Python >= 3.9: can remove 'List' since type hints can now use the generic 'list'

from functools import reduce as freduce

Expand Down Expand Up @@ -672,32 +672,34 @@ def permissionUpdated(permission:'QPermission') -> None:
except Exception: # pylint: disable=broad-except
pass

class FilteredLogger(logging.Logger):

def __init__(self, name:str, level:Any=logging.NOTSET) -> None:
super().__init__(name, level)
# returns False if message is duplicate and should be suppressed from log output
class DuplicateFilter(logging.Filter):
def __init__(self) -> None:
super().__init__()
self._message_lockup: Dict[int,int] = {}

def _log(self, level:int, msg:Any, args:Any, exc_info:Any=None, extra:Optional[Mapping[str, object]]=None,
stack_info:bool=False, stacklevel: int = 1) -> None:
def filter(self, record:logging.LogRecord) -> bool:
try:
log_interval = 10
message_Id = zlib.crc32(str(msg).encode('utf-8'))
log_interval:int = 10
message_Id = zlib.crc32(str(record.getMessage()).encode('utf-8'))
if message_Id not in self._message_lockup:
self._message_lockup[message_Id] = 0
super()._log(level, msg, args, exc_info, extra, stack_info, stacklevel)
elif self._message_lockup[message_Id] % log_interval == 0:
self._message_lockup[message_Id] = 0
msg += f' -- Suppressed {log_interval} equal messages'
super()._log(level, msg, args, exc_info, extra, stack_info, stacklevel)
return True
self._message_lockup[message_Id] += 1
if self._message_lockup[message_Id] % log_interval == 0:
self._message_lockup[message_Id] = 0
return True
return False
except Exception: # pylint: disable=broad-except
super()._log(level, msg, args, exc_info, extra, stack_info, stacklevel)
logging.setLoggerClass(FilteredLogger)
return True
for handler in logging.root.handlers:
handler.addFilter(DuplicateFilter())


_log: Final[logging.Logger] = logging.getLogger(__name__)



if multiprocessing.current_process().name == 'MainProcess':
_log.info(
'%s v%s (%s, %s)',
Expand Down
2 changes: 1 addition & 1 deletion src/artisanlib/phidgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def __init__(self) -> None:
self.manager.setOnAttachHandler(self.attachHandler)
self.manager.setOnDetachHandler(self.detachHandler)
self.manager.open()
_log.debug('PhidgetManager opened')
_log.debug('phidgetManager opened')

def close(self) -> None:
self.manager.close()
Expand Down

0 comments on commit 6e3f550

Please sign in to comment.