Skip to content

Commit

Permalink
Only allow AzureLogHandler to be added once to each logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Iain-S committed Feb 5, 2024
1 parent cb0e327 commit c22c93e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 1 deletion.
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions status_function/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
4 changes: 4 additions & 0 deletions status_function/status/logutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions status_function/tests/test_function_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit c22c93e

Please sign in to comment.