Repository: scoutapp/scout_apm_python (python)

Issue:
Hide scout message about event draining
When `log_level` is set to `ERROR` and logs are redirected to a log file through `log_file_path`, scout still logs a message like: `Scout draining 1 event for up to 2.0 seconds` on STDOUT, is it possible to remove this message from STDOUT?
Thank you!

Pull request:
Allow disabling the shutdown message
Fixes #498.

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6cccc5b..b09c491 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,14 @@
 # Changelog
 
+## Pending
+
+### Added
+
+- Added config value `shutdown_message_enabled` /
+  `SCOUT_SHUTDOWN_MESSAGE_ENABLED` which can be set to "false" to disable the
+  shutdown message "Scout draining N events..."that was added in version 2.11.0
+  ([Issue #498](https://github.com/scoutapp/scout_apm_python/issues/498)).
+
 ## [2.12.0] 2020-03-03
 
 ### Added
diff --git a/src/scout_apm/core/__init__.py b/src/scout_apm/core/__init__.py
index 656abe2..94022aa 100644
--- a/src/scout_apm/core/__init__.py
+++ b/src/scout_apm/core/__init__.py
@@ -62,17 +62,18 @@ def shutdown():
     timeout_seconds = scout_config.value("shutdown_timeout_seconds")
 
     def callback(queue_size):
-        print(  # noqa: T001
-            (
-                "Scout draining {queue_size} event{s} for up to"
-                + " {timeout_seconds} seconds"
-            ).format(
-                queue_size=queue_size,
-                s=("" if queue_size == 1 else "s"),
-                timeout_seconds=timeout_seconds,
-            ),
-            file=sys.stderr,
-        )
+        if scout_config.value("shutdown_message_enabled"):
+            print(  # noqa: T001
+                (
+                    "Scout draining {queue_size} event{s} for up to"
+                    + " {timeout_seconds} seconds"
+                ).format(
+                    queue_size=queue_size,
+                    s=("" if queue_size == 1 else "s"),
+                    timeout_seconds=timeout_seconds,
+                ),
+                file=sys.stderr,
+            )
 
     CoreAgentSocketThread.wait_until_drained(
         timeout_seconds=timeout_seconds, callback=callback
diff --git a/src/scout_apm/core/config.py b/src/scout_apm/core/config.py
index 69c5321..31f140f 100644
--- a/src/scout_apm/core/config.py
+++ b/src/scout_apm/core/config.py
@@ -73,6 +73,7 @@ class ScoutConfig(object):
             "name",
             "revision_sha",
             "scm_subdirectory",
+            "shutdown_message_enabled",
             "shutdown_timeout_seconds",
             "socket_path",
         ]
@@ -221,6 +222,7 @@ class Defaults(object):
             "name": "Python App",
             "revision_sha": self._git_revision_sha(),
             "scm_subdirectory": "",
+            "shutdown_message_enabled": True,
             "shutdown_timeout_seconds": 2.0,
             "uri_reporting": "filtered_params",
         }
@@ -285,6 +287,7 @@ CONVERSIONS = {
     "disabled_instruments": convert_to_list,
     "ignore": convert_to_list,
     "monitor": convert_to_bool,
+    "shutdown_message_enabled": convert_to_bool,
     "shutdown_timeout_seconds": convert_to_float,
 }
 


Test patch:
diff --git a/tests/unit/test_core.py b/tests/unit/test_core.py
index f1fff08..90eb78e 100644
--- a/tests/unit/test_core.py
+++ b/tests/unit/test_core.py
@@ -57,6 +57,7 @@ def test_install_success(caplog):
 
 
 def test_shutdown(capsys):
+    scout_config
     report_app_metadata()  # queued but thread not running
     try:
         scout_config.set(shutdown_timeout_seconds=0.1)
@@ -67,3 +68,16 @@ def test_shutdown(capsys):
 
     captured = capsys.readouterr()
     assert "Scout draining" in captured.err
+
+
+def test_shutdown_message_disabled(capsys):
+    report_app_metadata()  # queued but thread not running
+    try:
+        scout_config.set(shutdown_timeout_seconds=0.1, shutdown_message_enabled=False)
+
+        shutdown()
+    finally:
+        Config.reset_all()
+
+    captured = capsys.readouterr()
+    assert not captured.err