From 977e76e74fba4300698277f8e71ae9b35291f9c9 Mon Sep 17 00:00:00 2001 From: Daniel Sanche Date: Fri, 13 Jan 2023 17:11:21 -0800 Subject: [PATCH 1/3] fix: send instrumentation log using an explicit logger --- google/cloud/logging_v2/handlers/structured_log.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/google/cloud/logging_v2/handlers/structured_log.py b/google/cloud/logging_v2/handlers/structured_log.py index 55ed9c2d0..5b2e41ffb 100644 --- a/google/cloud/logging_v2/handlers/structured_log.py +++ b/google/cloud/logging_v2/handlers/structured_log.py @@ -131,4 +131,8 @@ def emit(self, record): def emit_instrumentation_info(self): google.cloud.logging_v2._instrumentation_emitted = True diagnostic_object = _create_diagnostic_entry() - logging.info(diagnostic_object.payload) + struct_logger = logging.getLogger("logging.googleapis.com/diagnostic") + struct_logger.addHandler(self) + struct_logger.setLevel(logging.INFO) + struct_logger.info(diagnostic_object.payload) + struct_logger.handlers.clear() From 2ab368d4a92caadc43789cc60832577fc50ae14c Mon Sep 17 00:00:00 2001 From: Daniel Sanche Date: Tue, 17 Jan 2023 13:34:23 -0800 Subject: [PATCH 2/3] use __name__ for logger name, as suggested in logging docs --- google/cloud/logging_v2/handlers/structured_log.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google/cloud/logging_v2/handlers/structured_log.py b/google/cloud/logging_v2/handlers/structured_log.py index 5b2e41ffb..fac9b26b3 100644 --- a/google/cloud/logging_v2/handlers/structured_log.py +++ b/google/cloud/logging_v2/handlers/structured_log.py @@ -131,7 +131,7 @@ def emit(self, record): def emit_instrumentation_info(self): google.cloud.logging_v2._instrumentation_emitted = True diagnostic_object = _create_diagnostic_entry() - struct_logger = logging.getLogger("logging.googleapis.com/diagnostic") + struct_logger = logging.getLogger(__name__) struct_logger.addHandler(self) struct_logger.setLevel(logging.INFO) struct_logger.info(diagnostic_object.payload) From 6e444cd54dd28248e6f4e34a755d5eebdf57d764 Mon Sep 17 00:00:00 2001 From: Daniel Sanche Date: Tue, 17 Jan 2023 14:13:05 -0800 Subject: [PATCH 3/3] updated test --- tests/unit/handlers/test_structured_log.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/unit/handlers/test_structured_log.py b/tests/unit/handlers/test_structured_log.py index d930da763..353530ed1 100644 --- a/tests/unit/handlers/test_structured_log.py +++ b/tests/unit/handlers/test_structured_log.py @@ -624,7 +624,8 @@ def test_valid_instrumentation_info(self): import mock import json - with mock.patch.object(logging, "info") as mock_log: + logger = logging.getLogger("google.cloud.logging_v2.handlers.structured_log") + with mock.patch.object(logger, "info") as mock_log: handler = self._make_one() handler.emit_instrumentation_info() mock_log.assert_called_once()