Skip to content

Commit 7f41d04

Browse files
committed
feat: support configuring logger in dbapi kwargs
Allow the kwargs for dbapi connections to inlcude a logger, and use this as the logger for the database that is used. Also set a default logger that only logs at WARN level for the mock server tests to stop them from spamming the test log with a bunch of "Created multiplexed session." messages that are logged at INFO level.
1 parent aaaff2a commit 7f41d04

File tree

3 files changed

+7
-8
lines changed

3 files changed

+7
-8
lines changed

google/cloud/spanner_dbapi/connection.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,8 +819,9 @@ def connect(
819819
instance = client.instance(instance_id)
820820
database = None
821821
if database_id:
822+
logger = kwargs.get("logger")
822823
database = instance.database(
823-
database_id, pool=pool, database_role=database_role
824+
database_id, pool=pool, database_role=database_role, logger=logger
824825
)
825826
conn = Connection(instance, database, **kwargs)
826827
if pool is not None:

tests/mockserver_tests/mock_server_test_base.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
14+
import logging
1515
import unittest
1616

1717
import grpc
@@ -170,12 +170,15 @@ class MockServerTestBase(unittest.TestCase):
170170
spanner_service: SpannerServicer = None
171171
database_admin_service: DatabaseAdminServicer = None
172172
port: int = None
173+
logger: logging.Logger = None
173174

174175
def __init__(self, *args, **kwargs):
175176
super(MockServerTestBase, self).__init__(*args, **kwargs)
176177
self._client = None
177178
self._instance = None
178179
self._database = None
180+
self.logger = logging.getLogger("MockServerTestBase")
181+
self.logger.setLevel(logging.WARN)
179182

180183
@classmethod
181184
def setup_class(cls):
@@ -227,6 +230,7 @@ def database(self) -> Database:
227230
"test-database",
228231
pool=FixedSizePool(size=10),
229232
enable_interceptors_in_tests=True,
233+
logger=self.logger,
230234
)
231235
return self._database
232236

tests/mockserver_tests/test_request_id_header.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,6 @@ def test_database_execute_partitioned_dml_request_id(self):
227227
(1, REQ_RAND_PROCESS_ID, NTH_CLIENT, CHANNEL_ID, exec_sql_seq, 1),
228228
)
229229
]
230-
print(f"Filtered unary segments: {filtered_unary_segments}")
231-
print(f"Want unary segments: {want_unary_segments}")
232-
print(f"Got stream segments: {got_stream_segments}")
233-
print(f"Want stream segments: {want_stream_segments}")
234230
assert all(seg in filtered_unary_segments for seg in want_unary_segments)
235231
assert got_stream_segments == want_stream_segments
236232

@@ -269,8 +265,6 @@ def test_unary_retryable_error(self):
269265
(1, REQ_RAND_PROCESS_ID, NTH_CLIENT, CHANNEL_ID, exec_sql_seq, 1),
270266
)
271267
]
272-
print(f"Got stream segments: {got_stream_segments}")
273-
print(f"Want stream segments: {want_stream_segments}")
274268
assert got_stream_segments == want_stream_segments
275269

276270
def test_streaming_retryable_error(self):

0 commit comments

Comments
 (0)