diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 5cbd5c9..f029b35 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -27,7 +27,6 @@ repos: rev: v8.16.1 hooks: - id: gitleaks - args: ["--config", ".gitleaks.toml"] - repo: 'https://github.com/PyCQA/pydocstyle' rev: 6.3.0 hooks: diff --git a/status_function/run_tests.sh b/status_function/run_tests.sh index d63bc81..f4efcb5 100755 --- a/status_function/run_tests.sh +++ b/status_function/run_tests.sh @@ -34,6 +34,7 @@ echo "Running unit tests..." API_URL="http://some.api.url" \ PRIVATE_KEY="-----BEGIN OPENSSH PRIVATE KEY-----mykey1234-----END OPENSSH PRIVATE KEY-----" \ # gitleaks:allow AZURE_TENANT_ID="00000000-0000-0000-0000-000000000000" \ +CENTRAL_LOGGING_CONNECTION_STRING="InstrumentationKey=00000000-0000-0000-0000-000000000000" \ python -m coverage run \ --omit=".venv/*","tests/*" \ -m unittest discover \ diff --git a/status_function/status/logutils.py b/status_function/status/logutils.py index 1082694..13e228a 100644 --- a/status_function/status/logutils.py +++ b/status_function/status/logutils.py @@ -54,6 +54,10 @@ def set_log_handler(name: str = "status") -> None: logger = logging.getLogger(name) log_settings = settings.get_settings() if log_settings.CENTRAL_LOGGING_CONNECTION_STRING: + for handler in logger.handlers: + if isinstance(handler, AzureLogHandler): + raise RuntimeError("AzureLogHandler already added to logger.") + custom_dimensions = {"logger_name": f"logger_{name}"} handler = AzureLogHandler( connection_string=log_settings.CENTRAL_LOGGING_CONNECTION_STRING diff --git a/status_function/tests/test_function_app.py b/status_function/tests/test_function_app.py index f414c91..340c8f2 100644 --- a/status_function/tests/test_function_app.py +++ b/status_function/tests/test_function_app.py @@ -611,5 +611,13 @@ def test_bearer_auth(self): self.assertEqual("status-app", username) +class TestLoggingUtils(TestCase): + def test_called_twice(self): + """Adding multiple loggers can cause large storage bills ££££.""" + with self.assertRaises(RuntimeError): + status.logutils.set_log_handler("a") + status.logutils.set_log_handler("a") + + if __name__ == "__main__": main()