Repository: jamesoff/simplemonitor

Issue:
Add group support to loggers
As discussed in #78 it would be nice to have the recently-added group feature work for loggers as well as alerters. 

Discussion:

Patch:
diff --git a/docs/logging.md b/docs/logging.md
index 7ddeef3..a627fb4 100644
--- a/docs/logging.md
+++ b/docs/logging.md
@@ -24,6 +24,7 @@ The section name should be the name of your logger. This is the name you should
 |---|---|---|---|
 |type|the type of logger to create. Choose one of the five in the list above.|yes| |
 |depend|lists (comma-separated, no spaces) the names of the monitors this logger depends on. Use this if the database file lives over the network. If a monitor it depends on fails, no attempt will be made to update the database.| no | |
+|groups|comma-separated list of monitor groups this logger should operate for|no|"default"|
 
 ### <a name="db"></a><a name="dbstatus"></a>db and dbstatus loggers
 
diff --git a/simplemonitor/Loggers/logger.py b/simplemonitor/Loggers/logger.py
index 2de2024..5299388 100644
--- a/simplemonitor/Loggers/logger.py
+++ b/simplemonitor/Loggers/logger.py
@@ -24,6 +24,10 @@ class Logger:
             List[str],
             self.get_config_option("depend", required_type="[str]", default=[]),
         )
+        # only log for Monitors with one of these groups
+        self._groups = self.get_config_option(
+            "groups", required_type="[str]", default=["default"]
+        )
         if self.batch_data is None:
             self.batch_data = {}
         self._global_info = None  # type: Optional[dict]
diff --git a/simplemonitor/simplemonitor.py b/simplemonitor/simplemonitor.py
index fbdb3dd..c27d92e 100644
--- a/simplemonitor/simplemonitor.py
+++ b/simplemonitor/simplemonitor.py
@@ -181,7 +181,15 @@ class SimpleMonitor:
         logger.check_dependencies(self.failed + self.still_failing + self.skipped)
         logger.start_batch()
         for key in list(self.monitors.keys()):
-            logger.save_result2(key, self.monitors[key])
+            if self.monitors[key].group in logger._groups:
+                logger.save_result2(key, self.monitors[key])
+            else:
+                module_logger.debug(
+                    "not logging for %s due to group mismatch (monitor in group %s, logger has groups %s",
+                    key,
+                    self.monitors[key].group,
+                    logger._groups,
+                )
         try:
             for key in list(self.remote_monitors.keys()):
                 logger.save_result2(key, self.remote_monitors[key])
@@ -201,13 +209,6 @@ class SimpleMonitor:
                     "skipping alert for monitor %s as it wants remote alerting", key
                 )
                 continue
-            module_logger.debug(
-                "considering alert for monitor %s (group: %s) with alerter %s (groups: %s)",
-                this_monitor.name,
-                this_monitor.group,
-                alerter.name,
-                alerter.groups,
-            )
             try:
                 if this_monitor.group in alerter.groups:
                     # Only notifications for services that have it enabled
@@ -220,7 +221,10 @@ class SimpleMonitor:
                         )
                 else:
                     module_logger.info(
-                        "skipping alerter %s as monitor is not in group", alerter.name
+                        "skipping alerter %s as monitor %s is not in group %s",
+                        alerter.name,
+                        this_monitor.name,
+                        alerter.groups,
                     )
             except Exception:  # pragma: no cover
                 module_logger.exception("exception caught while alerting for %s", key)


Test patch:
diff --git a/tests/test_logger.py b/tests/test_logger.py
index 9d5d928..eafa56d 100644
--- a/tests/test_logger.py
+++ b/tests/test_logger.py
@@ -1,7 +1,10 @@
 # type: ignore
 import unittest
+from unittest.mock import patch
 
 from simplemonitor.Loggers import logger
+from simplemonitor.Monitors.monitor import MonitorNull
+from simplemonitor.simplemonitor import SimpleMonitor
 
 
 class TestLogger(unittest.TestCase):
@@ -39,3 +42,18 @@ class TestLogger(unittest.TestCase):
         self.assertEqual(
             test_logger.connected, False, "logger thought it was connected"
         )
+
+    def test_groups(self):
+        with patch.object(logger.Logger, "save_result2") as mock_method:
+            this_logger = logger.Logger({"groups": "nondefault"})
+            s = SimpleMonitor()
+            s.add_monitor("test", MonitorNull())
+            s.log_result(this_logger)
+        mock_method.assert_not_called()
+
+        with patch.object(logger.Logger, "save_result2") as mock_method:
+            this_logger = logger.Logger({"groups": "nondefault"})
+            s = SimpleMonitor()
+            s.add_monitor("test", MonitorNull("unnamed", {"group": "nondefault"}))
+            s.log_result(this_logger)
+        mock_method.assert_called_once()