Skip to content

Commit

Permalink
Logging Feature for 'BinLogStreamReader'
Browse files Browse the repository at this point in the history
  • Loading branch information
Chungeun Choi authored Sep 12, 2023
1 parent 2e439b6 commit a097cf0
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions pymysqlreplication/binlogstream.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

import struct
import logging
from distutils.version import LooseVersion

import pymysql
Expand Down Expand Up @@ -144,7 +145,8 @@ def __init__(self, connection_settings, server_id,
is_mariadb=False,
annotate_rows_event=False,
ignore_decode_errors=False,
verify_checksum=False,):
verify_checksum=False,
enable_logging=True,):
"""
Attributes:
ctl_connection_settings: Connection settings for cluster holding
Expand Down Expand Up @@ -187,6 +189,8 @@ def __init__(self, connection_settings, server_id,
ignore_decode_errors: If true, any decode errors encountered
when reading column data will be ignored.
verify_checksum: If true, verify events read from the binary log by examining checksums.
enable_logging: When set to True, logs various details helpful for debugging and monitoring
When set to False, logging is disabled to enhance performance.
"""

self.__connection_settings = connection_settings
Expand Down Expand Up @@ -228,6 +232,8 @@ def __init__(self, connection_settings, server_id,
self.skip_to_timestamp = skip_to_timestamp
self.is_mariadb = is_mariadb
self.__annotate_rows_event = annotate_rows_event
if enable_logging:
self.__log_valid_parameters()

if end_log_pos:
self.is_past_end_log_pos = False
Expand Down Expand Up @@ -368,9 +374,11 @@ def __connect_to_stream(self):

if not self.__blocking:
flags |= 0x01 # BINLOG_DUMP_NON_BLOCK

prelude += struct.pack('<H', flags)

prelude += struct.pack('<I', self.__server_id)

prelude += self.log_file.encode()
else:
if self.is_mariadb:
Expand Down Expand Up @@ -674,7 +682,7 @@ def __get_table_information(self, schema, table):
continue
else:
raise error

def __get_dbms(self):
if not self.__connected_ctl:
self.__connect_to_ctl()
Expand All @@ -687,5 +695,20 @@ def __get_dbms(self):
return 'mariadb'
return 'mysql'

def __log_valid_parameters(self):
ignored = ["allowed_events", "table_map"]
for parameter, value in self.__dict__.items():
if parameter.startswith("_BinLogStreamReader__"):
parameter = parameter.replace("_BinLogStreamReader__", "")
if parameter in ignored or not value:
continue
if type(value) == frozenset:
string_list = [str(item).split()[-1][:-2].split('.')[2] for item in value]
items = ", ".join(string_list)
comment = f"{parameter}: [{items}]"
else:
comment = f"{parameter}: {value}"
logging.info(comment)

def __iter__(self):
return iter(self.fetchone, None)

0 comments on commit a097cf0

Please sign in to comment.