Skip to content

Commit a097cf0

Browse files
author
Chungeun Choi
authored
Logging Feature for 'BinLogStreamReader'
1 parent 2e439b6 commit a097cf0

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

pymysqlreplication/binlogstream.py

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22

33
import struct
4+
import logging
45
from distutils.version import LooseVersion
56

67
import pymysql
@@ -144,7 +145,8 @@ def __init__(self, connection_settings, server_id,
144145
is_mariadb=False,
145146
annotate_rows_event=False,
146147
ignore_decode_errors=False,
147-
verify_checksum=False,):
148+
verify_checksum=False,
149+
enable_logging=True,):
148150
"""
149151
Attributes:
150152
ctl_connection_settings: Connection settings for cluster holding
@@ -187,6 +189,8 @@ def __init__(self, connection_settings, server_id,
187189
ignore_decode_errors: If true, any decode errors encountered
188190
when reading column data will be ignored.
189191
verify_checksum: If true, verify events read from the binary log by examining checksums.
192+
enable_logging: When set to True, logs various details helpful for debugging and monitoring
193+
When set to False, logging is disabled to enhance performance.
190194
"""
191195

192196
self.__connection_settings = connection_settings
@@ -228,6 +232,8 @@ def __init__(self, connection_settings, server_id,
228232
self.skip_to_timestamp = skip_to_timestamp
229233
self.is_mariadb = is_mariadb
230234
self.__annotate_rows_event = annotate_rows_event
235+
if enable_logging:
236+
self.__log_valid_parameters()
231237

232238
if end_log_pos:
233239
self.is_past_end_log_pos = False
@@ -368,9 +374,11 @@ def __connect_to_stream(self):
368374

369375
if not self.__blocking:
370376
flags |= 0x01 # BINLOG_DUMP_NON_BLOCK
377+
371378
prelude += struct.pack('<H', flags)
372379

373380
prelude += struct.pack('<I', self.__server_id)
381+
374382
prelude += self.log_file.encode()
375383
else:
376384
if self.is_mariadb:
@@ -674,7 +682,7 @@ def __get_table_information(self, schema, table):
674682
continue
675683
else:
676684
raise error
677-
685+
678686
def __get_dbms(self):
679687
if not self.__connected_ctl:
680688
self.__connect_to_ctl()
@@ -687,5 +695,20 @@ def __get_dbms(self):
687695
return 'mariadb'
688696
return 'mysql'
689697

698+
def __log_valid_parameters(self):
699+
ignored = ["allowed_events", "table_map"]
700+
for parameter, value in self.__dict__.items():
701+
if parameter.startswith("_BinLogStreamReader__"):
702+
parameter = parameter.replace("_BinLogStreamReader__", "")
703+
if parameter in ignored or not value:
704+
continue
705+
if type(value) == frozenset:
706+
string_list = [str(item).split()[-1][:-2].split('.')[2] for item in value]
707+
items = ", ".join(string_list)
708+
comment = f"{parameter}: [{items}]"
709+
else:
710+
comment = f"{parameter}: {value}"
711+
logging.info(comment)
712+
690713
def __iter__(self):
691714
return iter(self.fetchone, None)

0 commit comments

Comments
 (0)