Skip to content

Commit 3cdf7df

Browse files
committed
Improve logging
log the whole object when available use inbuilt log formatting rather than f strings or .format (should improve performance)
1 parent 6c97c05 commit 3cdf7df

File tree

1 file changed

+39
-47
lines changed

1 file changed

+39
-47
lines changed

server/recceiver/cfstore.py

Lines changed: 39 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -206,14 +206,14 @@ def from_channelfinder_dict(channel_dict: Dict[str, Any]) -> "CFChannel":
206206
@implementer(interfaces.IProcessor)
207207
class CFProcessor(service.Service):
208208
def __init__(self, name, conf):
209-
_log.info("CF_INIT {name}".format(name=name))
209+
self.cf_config = CFConfig.from_config_adapter(conf)
210+
_log.info("CF_INIT %s", self.cf_config)
210211
self.name = name
211212
self.channel_ioc_ids = defaultdict(list)
212213
self.iocs: Dict[str, IocInfo] = dict()
213214
self.client = None
214215
self.currentTime = getCurrentTime
215216
self.lock = DeferredLock()
216-
self.cf_config = CFConfig.from_config_adapter(conf)
217217

218218
def startService(self):
219219
service.Service.startService(self)
@@ -289,7 +289,7 @@ def _startServiceWithLock(self):
289289

290290
self.record_property_names_list = set(record_property_names_list)
291291
self.managed_properties = required_properties.union(record_property_names_list)
292-
_log.debug("record_property_names_list = {}".format(self.record_property_names_list))
292+
_log.debug("record_property_names_list = %s", self.record_property_names_list)
293293
except ConnectionError:
294294
_log.exception("Cannot connect to Channelfinder service")
295295
raise
@@ -339,9 +339,9 @@ def chainError(err):
339339
else:
340340
d.callback(None)
341341

342-
def chainResult(_ignored):
342+
def chainResult(result):
343343
if self.cancelled:
344-
raise defer.CancelledError()
344+
raise defer.CancelledError(f"CF Processor is cancelled, due to {result}")
345345
else:
346346
d.callback(None)
347347

@@ -454,7 +454,7 @@ def _commitWithThread(self, transaction: CommitTransaction):
454454
recordInfo = self.transaction_to_recordInfo(ioc_info, transaction)
455455

456456
records_to_delete = list(transaction.records_to_delete)
457-
_log.debug("Delete records: {s}".format(s=records_to_delete))
457+
_log.debug("Delete records: %s", records_to_delete)
458458

459459
recordInfoByName = self.record_info_by_name(recordInfo, ioc_info)
460460
self.update_ioc_infos(transaction, ioc_info, records_to_delete, recordInfoByName)
@@ -495,11 +495,11 @@ def clean_service(self):
495495
except RequestException as e:
496496
_log.error("Clean service failed: {s}".format(s=e))
497497
retry_seconds = min(60, sleep)
498-
_log.info("Clean service retry in {retry_seconds} seconds".format(retry_seconds=retry_seconds))
498+
_log.info("Clean service retry in %s seconds", retry_seconds)
499499
time.sleep(retry_seconds)
500500
sleep *= 1.5
501501
if self.running == 0 and sleep >= retry_limit:
502-
_log.info("Abandoning clean after {retry_limit} seconds".format(retry_limit=retry_limit))
502+
_log.info("Abandoning clean after %s seconds", retry_limit)
503503
return
504504

505505
def get_active_channels(self, recceiverid) -> List[CFChannel]:
@@ -520,9 +520,9 @@ def clean_channels(self, owner: str, channels: List[CFChannel]):
520520
new_channels = []
521521
for cf_channel in channels or []:
522522
new_channels.append(cf_channel.name)
523-
_log.info("Total channels to update: {nChannels}".format(nChannels=len(new_channels)))
523+
_log.info("Cleaning %s channels.", len(new_channels))
524524
_log.debug(
525-
'Update "pvStatus" property to "Inactive" for {n_channels} channels'.format(n_channels=len(new_channels))
525+
'Update "pvStatus" property to "Inactive" for %s channels', len(new_channels)
526526
)
527527
self.client.update(
528528
property=CFProperty.inactive(owner).as_dict(),
@@ -531,12 +531,8 @@ def clean_channels(self, owner: str, channels: List[CFChannel]):
531531

532532

533533
def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo], records_to_delete, ioc_info: IocInfo):
534-
_log.info("CF Update IOC: {iocid}".format(iocid=ioc_info.ioc_id))
535-
_log.debug(
536-
"CF Update IOC: {iocid} recordInfoByName {recordInfoByName}".format(
537-
iocid=ioc_info.ioc_id, recordInfoByName=recordInfoByName
538-
)
539-
)
534+
_log.info("CF Update IOC: %s", ioc_info)
535+
_log.debug("CF Update IOC: %s recordInfoByName %s", ioc_info, recordInfoByName)
540536
# Consider making this function a class methed then 'processor' simply becomes 'self'
541537
client = processor.client
542538
channel_ioc_ids = processor.channel_ioc_ids
@@ -547,30 +543,28 @@ def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo]
547543
iocid = ioc_info.ioc_id
548544

549545
if iocid not in iocs:
550-
_log.warning("IOC Env Info not found: {iocid}".format(iocid=iocid))
546+
_log.warning("IOC Env Info %s not found in ioc list: %s", ioc_info, iocs)
551547

552548
if ioc_info.hostname is None or ioc_info.ioc_name is None:
553-
raise Exception("missing hostName or iocName")
549+
raise Exception(f"Missing hostName {ioc_info.hostname} or iocName {ioc_info.ioc_name}")
554550

555551
if processor.cancelled:
556-
raise defer.CancelledError()
552+
raise defer.CancelledError("Processor cancelled in __updateCF__")
557553

558554
channels: List[CFChannel] = []
559555
"""A list of channels in channelfinder with the associated hostName and iocName"""
560-
_log.debug("Find existing channels by IOCID: {iocid}".format(iocid=iocid))
556+
_log.debug("Find existing channels by IOCID: %s", ioc_info)
561557
old_channels: List[CFChannel] = [
562558
CFChannel.from_channelfinder_dict(ch)
563559
for ch in client.findByArgs(prepareFindArgs(cf_config, [("iocid", iocid)]))
564560
]
565-
if processor.cancelled:
566-
raise defer.CancelledError()
567561

568562
if old_channels is not None:
569563
for cf_channel in old_channels:
570564
if (
571565
len(new_channels) == 0 or cf_channel.name in records_to_delete
572566
): # case: empty commit/del, remove all reference to ioc
573-
_log.debug("Channel {s} exists in Channelfinder not in new_channels".format(s=cf_channel.name))
567+
_log.debug("Channel %s exists in Channelfinder not in new_channels", cf_channel)
574568
if cf_channel.name in channel_ioc_ids:
575569
last_ioc_id = channel_ioc_ids[cf_channel.name][-1]
576570
cf_channel.owner = iocs[last_ioc_id].owner
@@ -588,7 +582,7 @@ def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo]
588582
processor.managed_properties,
589583
)
590584
channels.append(cf_channel)
591-
_log.debug("Add existing channel to previous IOC: {s}".format(s=channels[-1]))
585+
_log.debug("Add existing channel %s to previous IOC %s", cf_channel, last_ioc_id)
592586
"""In case alias exist, also delete them"""
593587
if cf_config.alias_enabled:
594588
if cf_channel.name in recordInfoByName:
@@ -621,7 +615,9 @@ def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo]
621615
processor.managed_properties,
622616
)
623617
channels.append(alias_channel)
624-
_log.debug("Add existing alias to previous IOC: {s}".format(s=channels[-1]))
618+
_log.debug(
619+
"Add existing alias %s to previous IOC: %s", alias_channel, last_alias_ioc_id
620+
)
625621

626622
else:
627623
"""Orphan the channel : mark as inactive, keep the old hostName and iocName"""
@@ -633,7 +629,7 @@ def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo]
633629
cf_channel,
634630
)
635631
channels.append(cf_channel)
636-
_log.debug("Add orphaned channel with no IOC: {s}".format(s=channels[-1]))
632+
_log.debug("Add orphaned channel %s with no IOC: %s", cf_channel, ioc_info)
637633
"""Also orphan any alias"""
638634
if cf_config.alias_enabled:
639635
if cf_channel.name in recordInfoByName:
@@ -647,16 +643,14 @@ def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo]
647643
alias_channel,
648644
)
649645
channels.append(alias_channel)
650-
_log.debug("Add orphaned alias with no IOC: {s}".format(s=channels[-1]))
646+
_log.debug("Add orphaned alias %s with no IOC: %s", alias_channel, ioc_info)
651647
else:
652648
if cf_channel.name in new_channels: # case: channel in old and new
653649
"""
654-
Channel exists in Channelfinder with same hostname and iocname.
650+
Channel exists in Channelfinder with same iocid.
655651
Update the status to ensure it is marked active and update the time.
656652
"""
657-
_log.debug(
658-
"Channel {s} exists in Channelfinder with same hostname and iocname".format(s=cf_channel.name)
659-
)
653+
_log.debug("Channel %s exists in Channelfinder with same iocid %s", cf_channel.name, iocid)
660654
cf_channel.properties = __merge_property_lists(
661655
[
662656
CFProperty.active(ioc_info.owner),
@@ -666,7 +660,7 @@ def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo]
666660
processor.managed_properties,
667661
)
668662
channels.append(cf_channel)
669-
_log.debug("Add existing channel with same IOC: {s}".format(s=channels[-1]))
663+
_log.debug("Add existing channel with same IOC: %s", cf_channel)
670664
new_channels.remove(cf_channel.name)
671665

672666
"""In case, alias exist"""
@@ -708,7 +702,7 @@ def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo]
708702
)
709703
)
710704
new_channels.remove(alias_name)
711-
_log.debug("Add existing alias with same IOC: {s}".format(s=channels[-1]))
705+
_log.debug("Add existing alias with same IOC: %s", cf_channel)
712706
# now pvNames contains a list of pv's new on this host/ioc
713707
"""A dictionary representing the current channelfinder information associated with the pvNames"""
714708
existingChannels: Dict[str, CFChannel] = {}
@@ -731,7 +725,7 @@ def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo]
731725
searchStrings.append(searchString)
732726

733727
for eachSearchString in searchStrings:
734-
_log.debug("Find existing channels by name: {search}".format(search=eachSearchString))
728+
_log.debug("Find existing channels by name: %s", eachSearchString)
735729
for found_channel in client.findByArgs(prepareFindArgs(cf_config, [("~name", eachSearchString)])):
736730
existingChannels[found_channel["name"]] = CFChannel.from_channelfinder_dict(found_channel)
737731
if processor.cancelled:
@@ -757,9 +751,7 @@ def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo]
757751
newProps = newProps + recordInfoByName[channel_name].infoProperties
758752

759753
if channel_name in existingChannels:
760-
_log.debug(
761-
f"""update existing channel{channel_name}: exists but with a different hostName and/or iocName"""
762-
)
754+
_log.debug("update existing channel %s: exists but with a different iocid from %s", channel_name, iocid)
763755

764756
existingChannel = existingChannels[channel_name]
765757
existingChannel.properties = __merge_property_lists(
@@ -768,7 +760,7 @@ def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo]
768760
processor.managed_properties,
769761
)
770762
channels.append(existingChannel)
771-
_log.debug("Add existing channel with different IOC: {s}".format(s=channels[-1]))
763+
_log.debug("Add existing channel with different IOC: %s", existingChannel)
772764
"""in case, alias exists, update their properties too"""
773765
if cf_config.alias_enabled:
774766
if channel_name in recordInfoByName:
@@ -786,23 +778,24 @@ def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo]
786778
channels.append(ach)
787779
else:
788780
channels.append(CFChannel(alias_name, ioc_info.owner, alProps))
789-
_log.debug("Add existing alias with different IOC: {s}".format(s=channels[-1]))
781+
_log.debug(
782+
"Add existing alias %s of %s with different IOC from %s", alias_name, channel_name, iocid
783+
)
790784

791785
else:
792786
"""New channel"""
793787
channels.append(CFChannel(channel_name, ioc_info.owner, newProps))
794-
_log.debug("Add new channel: {s}".format(s=channels[-1]))
788+
_log.debug("Add new channel: %s", channel_name)
795789
if cf_config.alias_enabled:
796790
if channel_name in recordInfoByName:
797791
alProps = [CFProperty.alias(ioc_info.owner, channel_name)]
798792
for p in newProps:
799793
alProps.append(p)
800794
for alias in recordInfoByName[channel_name].aliases:
801795
channels.append(CFChannel(alias, ioc_info.owner, alProps))
802-
_log.debug("Add new alias: {s}".format(s=channels[-1]))
803-
_log.info(
804-
"Total channels to update: {nChannels} {iocName}".format(nChannels=len(channels), iocName=ioc_info.ioc_name)
805-
)
796+
_log.debug("Add new alias: %s from %s", alias, channel_name)
797+
_log.info("Total channels to update: %s for ioc: %s", len(channels), ioc_info)
798+
806799
if len(channels) != 0:
807800
cf_set_chunked(client, channels, cf_config.cf_query_limit)
808801
else:
@@ -881,7 +874,7 @@ def poll(
881874
records_to_delete,
882875
ioc_info: IocInfo,
883876
):
884-
_log.info("Polling {iocName} begins...".format(iocName=ioc_info.ioc_name))
877+
_log.info("Polling for %s begins...", ioc_info)
885878
sleep = 1.0
886879
success = False
887880
while not success:
@@ -895,5 +888,4 @@ def poll(
895888
_log.info("ChannelFinder update retry in {retry_seconds} seconds".format(retry_seconds=retry_seconds))
896889
time.sleep(retry_seconds)
897890
sleep *= 1.5
898-
_log.info("Polling {iocName} complete".format(iocName=ioc_info.ioc_name))
899-
891+
_log.info("Polling %s complete", ioc_info)

0 commit comments

Comments
 (0)