From 7e6c070db6e823e4fed245e00aee83c3bfe22b04 Mon Sep 17 00:00:00 2001 From: Wei-Jie Huang Date: Mon, 23 Oct 2023 23:41:17 -0500 Subject: [PATCH] Replace root logger with module loggers --- pysyncobj/dns_resolver.py | 6 ++++-- pysyncobj/serializer.py | 12 +++++++----- pysyncobj/syncobj.py | 14 ++++++++------ 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/pysyncobj/dns_resolver.py b/pysyncobj/dns_resolver.py index 6eea365..d84f0e5 100644 --- a/pysyncobj/dns_resolver.py +++ b/pysyncobj/dns_resolver.py @@ -4,6 +4,8 @@ import logging from .monotonic import monotonic as monotonicTime +logger = logging.getLogger(__name__) + class DnsCachingResolver(object): def __init__(self, cacheTime, failCacheTime): @@ -24,7 +26,7 @@ def resolve(self, hostname): prevIps = ips ips = self.__doResolve(hostname) if not ips: - logging.warning("failed to resolve hostname: " + hostname) + logger.warning("failed to resolve hostname: " + hostname) ips = prevIps self.__cache[hostname] = (currTime, ips) return None if not ips else random.choice(ips) @@ -49,7 +51,7 @@ def __doResolve(self, hostname): if not ips: ips = list(set([addr[4][0] for addr in addrs])) except socket.gaierror: - logging.warning('failed to resolve host %s', hostname) + logger.warning('failed to resolve host %s', hostname) ips = [] return ips diff --git a/pysyncobj/serializer.py b/pysyncobj/serializer.py index 9454f6d..a77b23f 100644 --- a/pysyncobj/serializer.py +++ b/pysyncobj/serializer.py @@ -7,6 +7,8 @@ from .atomic_replace import atomicReplace from .config import SERIALIZER_STATE +logger = logging.getLogger(__name__) + class Serializer(object): def __init__(self, fileName, transmissionBatchSize, tryUseFork, @@ -131,7 +133,7 @@ def getTransmissionData(self, transmissionID): 'transmitted': 0, } except: - logging.exception('Failed to open file for transmission') + logger.exception('Failed to open file for transmission') self.__transmissions.pop(transmissionID, None) return None isFirst = transmission['transmitted'] == 0 @@ -142,7 +144,7 @@ def getTransmissionData(self, transmissionID): else: data = transmission['file'].read(self.__transmissionBatchSize) except: - logging.exception('Error reading transmission file') + logger.exception('Error reading transmission file') self.__transmissions.pop(transmissionID, None) return False size = len(data) @@ -178,7 +180,7 @@ def setTransmissionData(self, data): try: self.__incomingTransmissionFile = open(tmpFile, 'wb') except: - logging.exception('Failed to open file for incoming transition') + logger.exception('Failed to open file for incoming transition') self.__incomingTransmissionFile = None return False elif self.__incomingTransmissionFile is None: @@ -186,7 +188,7 @@ def setTransmissionData(self, data): try: self.__incomingTransmissionFile.write(data) except: - logging.exception('Failed to write incoming transition data') + logger.exception('Failed to write incoming transition data') self.__incomingTransmissionFile = None return False if isLast: @@ -195,7 +197,7 @@ def setTransmissionData(self, data): try: atomicReplace(tmpFile, self.__fileName) except: - logging.exception('Failed to rename temporary incoming transition file') + logger.exception('Failed to rename temporary incoming transition file') return False return True return False diff --git a/pysyncobj/syncobj.py b/pysyncobj/syncobj.py index ea483b2..ce93ae0 100644 --- a/pysyncobj/syncobj.py +++ b/pysyncobj/syncobj.py @@ -45,6 +45,8 @@ def iteritems(v): from .fast_queue import FastQueue from .monotonic import monotonic as monotonicTime +logger = logging.getLogger(__name__) + class _RAFT_STATE: FOLLOWER = 0 @@ -277,7 +279,7 @@ def __init__(self, selfNode, otherNodes, conf=None, consumers=None, nodeClass = while not self.__transport.ready: self.__transport.tryGetReady() except TransportNotReadyError: - logging.exception('failed to perform initialization') + logger.exception('failed to perform initialization') raise SyncObjException('BindError') # Backwards compatibility def destroy(self): @@ -505,7 +507,7 @@ def _autoTickThread(self): try: self.__transport.tryGetReady() except TransportNotReadyError: - logging.exception('failed to perform initialization') + logger.exception('failed to perform initialization') return finally: self.__initialised.set() @@ -647,7 +649,7 @@ def __applyLogEntries(self): self.__raftLastApplied += 1 except SyncObjExceptionWrongVer as e: - logging.error( + logger.error( 'request to switch to unsupported code version (self version: %d, requested version: %d)' % (self.__selfCodeVersion, e.ver)) @@ -778,7 +780,7 @@ def printStatus(self): """Dumps different debug info about cluster to default logger""" status = self.getStatus() for k, v in iteritems(status): - logging.info('%s: %s' % (str(k), str(v))) + logger.info('%s: %s' % (str(k), str(v))) def _printStatus(self): self.printStatus() @@ -1318,7 +1320,7 @@ def __tryLogCompaction(self): self.__lastSerializedEntry = serializeID if serializeState == SERIALIZER_STATE.FAILED: - logging.warning('Failed to store full dump') + logger.warning('Failed to store full dump') if serializeState != SERIALIZER_STATE.NOT_SERIALIZING: return @@ -1390,7 +1392,7 @@ def __loadDumpFile(self, clearJournal): self.__updateClusterConfiguration([node for node in data[3] if node != self.__selfNode]) self.__onSetCodeVersion(0) except: - logging.exception('failed to load full dump') + logger.exception('failed to load full dump') def __updateClusterConfiguration(self, newNodes): # newNodes: list of Node or node ID