Fix squelch repeated log messages to work with modules loaded via
authorScott Gasch <[email protected]>
Mon, 3 Apr 2023 16:25:25 +0000 (09:25 -0700)
committerScott Gasch <[email protected]>
Mon, 3 Apr 2023 16:25:25 +0000 (09:25 -0700)
"from foo import bar" type syntax.

src/pyutils/function_utils.py
src/pyutils/logging_utils.py

index ba1d469d160c03fc41c8a64f48909edd2f36f7f8..8b2cd3790fa499baa1e010cf570e18ffe1c284f0 100644 (file)
@@ -28,19 +28,19 @@ def function_identifier(f: Callable) -> str:
 
     """
 
-    if f.__module__ == '__main__':
+    if f.__module__ == "__main__":
         from pathlib import Path
 
         import __main__
 
         module = __main__.__file__
         module = Path(module).stem
-        return f'{module}:{f.__name__}'
+        return f"{module}:{f.__name__}"
     else:
-        return f'{f.__module__}:{f.__name__}'
+        return f"{f.__module__}:{f.__name__}"
 
 
-if __name__ == '__main__':
+if __name__ == "__main__":
     import doctest
 
     doctest.testmod()
index e1f3a2221b4e3f3e27441d5ace08e959f3f18257..c99cb4e644fce847bdb0593e8df4368143e5df68 100644 (file)
@@ -51,6 +51,7 @@ import enum
 import io
 import logging
 import os
+import re
 import sys
 from logging.config import fileConfig
 from logging.handlers import RotatingFileHandler, SysLogHandler
@@ -263,6 +264,10 @@ def squelch_repeated_log_messages(squelch_after_n_repeats: int) -> Callable:
         from pyutils import function_utils
 
         identifier = function_utils.function_identifier(f)
+
+        # Get rid of module paths, e.g. pyutils.ansi:bg -> ansi:bg which
+        # is what we're going to need below.
+        identifier = re.sub(r"[^\.]+\.", "", identifier)
         squelched_logging_counts[identifier] = squelch_after_n_repeats
         return f