Skip to content

Commit 8110e06

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 3d12567 commit 8110e06

File tree

1 file changed

+40
-49
lines changed

1 file changed

+40
-49
lines changed

server/recceiver/cfstore.py

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,14 @@ class CFChannel:
125125
@implementer(interfaces.IProcessor)
126126
class CFProcessor(service.Service):
127127
def __init__(self, name, conf):
128-
_log.info("CF_INIT {name}".format(name=name))
128+
self.cf_config = CFConfig.from_config_adapter(conf)
129+
_log.info("CF_INIT %s", self.cf_config)
129130
self.name = name
130131
self.channel_ioc_ids = defaultdict(list)
131132
self.iocs: Dict[str, IocInfo] = dict()
132133
self.client = None
133134
self.currentTime = getCurrentTime
134135
self.lock = DeferredLock()
135-
self.cf_config = CFConfig.from_config_adapter(conf)
136136

137137
def startService(self):
138138
service.Service.startService(self)
@@ -208,7 +208,7 @@ def _startServiceWithLock(self):
208208

209209
self.record_property_names_list = set(record_property_names_list)
210210
self.managed_properties = required_properties.union(record_property_names_list)
211-
_log.debug("record_property_names_list = {}".format(self.record_property_names_list))
211+
_log.debug("record_property_names_list = %s", self.record_property_names_list)
212212
except ConnectionError:
213213
_log.exception("Cannot connect to Channelfinder service")
214214
raise
@@ -258,9 +258,9 @@ def chainError(err):
258258
else:
259259
d.callback(None)
260260

261-
def chainResult(_ignored):
261+
def chainResult(result):
262262
if self.cancelled:
263-
raise defer.CancelledError()
263+
raise defer.CancelledError(f"CF Processor is cancelled, due to {result}")
264264
else:
265265
d.callback(None)
266266

@@ -372,7 +372,7 @@ def _commitWithThread(self, transaction: CommitTransaction):
372372
recordInfo = self.transaction_to_recordInfo(ioc_info, transaction)
373373

374374
records_to_delete = list(transaction.records_to_delete)
375-
_log.debug("Delete records: {s}".format(s=records_to_delete))
375+
_log.debug("Delete records: %s", records_to_delete)
376376

377377
recordInfoByName = self.record_info_by_name(recordInfo, ioc_info)
378378
self.update_ioc_infos(transaction, ioc_info, records_to_delete, recordInfoByName)
@@ -413,11 +413,11 @@ def clean_service(self):
413413
except RequestException as e:
414414
_log.error("Clean service failed: {s}".format(s=e))
415415
retry_seconds = min(60, sleep)
416-
_log.info("Clean service retry in {retry_seconds} seconds".format(retry_seconds=retry_seconds))
416+
_log.info("Clean service retry in %s seconds", retry_seconds)
417417
time.sleep(retry_seconds)
418418
sleep *= 1.5
419419
if self.running == 0 and sleep >= retry_limit:
420-
_log.info("Abandoning clean after {retry_limit} seconds".format(retry_limit=retry_limit))
420+
_log.info("Abandoning clean after %s seconds", retry_limit)
421421
return
422422

423423
def get_active_channels(self, recceiverid):
@@ -432,9 +432,9 @@ def clean_channels(self, owner: str, channels: List[CFChannel]):
432432
new_channels = []
433433
for cf_channel in channels or []:
434434
new_channels.append(cf_channel.name)
435-
_log.info("Total channels to update: {nChannels}".format(nChannels=len(new_channels)))
435+
_log.info("Cleaning %s channels.", len(new_channels))
436436
_log.debug(
437-
'Update "pvStatus" property to "Inactive" for {n_channels} channels'.format(n_channels=len(new_channels))
437+
'Update "pvStatus" property to "Inactive" for %s channels', len(new_channels)
438438
)
439439
self.client.update(
440440
property=create_inactive_property(owner).as_dict(),
@@ -475,12 +475,8 @@ def create_time_property(owner: str, time: str) -> CFProperty:
475475

476476

477477
def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo], records_to_delete, ioc_info: IocInfo):
478-
_log.info("CF Update IOC: {iocid}".format(iocid=ioc_info.ioc_id))
479-
_log.debug(
480-
"CF Update IOC: {iocid} recordInfoByName {recordInfoByName}".format(
481-
iocid=ioc_info.ioc_id, recordInfoByName=recordInfoByName
482-
)
483-
)
478+
_log.info("CF Update IOC: %s", ioc_info)
479+
_log.debug("CF Update IOC: %s recordInfoByName %s", ioc_info, recordInfoByName)
484480
# Consider making this function a class methed then 'processor' simply becomes 'self'
485481
client = processor.client
486482
channel_ioc_ids = processor.channel_ioc_ids
@@ -491,29 +487,27 @@ def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo]
491487
iocid = ioc_info.ioc_id
492488

493489
if iocid not in iocs:
494-
_log.warning("IOC Env Info not found: {iocid}".format(iocid=iocid))
490+
_log.warning("IOC Env Info %s not found in ioc list: %s", ioc_info, iocs)
495491

496492
if ioc_info.hostname is None or ioc_info.iocname is None:
497-
raise Exception("missing hostName or iocName")
493+
raise Exception(f"Missing hostName {ioc_info.hostname} or iocName {ioc_info.iocname}")
498494

499495
if processor.cancelled:
500-
raise defer.CancelledError()
496+
raise defer.CancelledError("Processor cancelled in __updateCF__")
501497

502498
channels: List[CFChannel] = []
503499
"""A list of channels in channelfinder with the associated hostName and iocName"""
504-
_log.debug("Find existing channels by IOCID: {iocid}".format(iocid=iocid))
500+
_log.debug("Find existing channels by IOCID: %s", ioc_info)
505501
old_channels: List[CFChannel] = [
506502
CFChannel(**ch) for ch in client.findByArgs(prepareFindArgs(cf_config, [("iocid", iocid)]))
507503
]
508-
if processor.cancelled:
509-
raise defer.CancelledError()
510504

511505
if old_channels is not None:
512506
for cf_channel in old_channels:
513507
if (
514508
len(new_channels) == 0 or cf_channel.name in records_to_delete
515509
): # case: empty commit/del, remove all reference to ioc
516-
_log.debug("Channel {s} exists in Channelfinder not in new_channels".format(s=cf_channel.name))
510+
_log.debug("Channel %s exists in Channelfinder not in new_channels", cf_channel)
517511
if cf_channel.name in channel_ioc_ids:
518512
last_ioc_id = channel_ioc_ids[cf_channel.name][-1]
519513
cf_channel.owner = iocs[last_ioc_id].owner
@@ -525,15 +519,13 @@ def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo]
525519
if cf_config.record_type_enabled:
526520
cf_channel.properties = __merge_property_lists(
527521
cf_channel.properties.append(
528-
create_recordType_property(
529-
ioc_info.owner, iocs[last_ioc_id]["recordType"]
530-
)
522+
create_recordType_property(ioc_info.owner, iocs[last_ioc_id]["recordType"])
531523
),
532524
cf_channel,
533525
processor.managed_properties,
534526
)
535527
channels.append(cf_channel)
536-
_log.debug("Add existing channel to previous IOC: {s}".format(s=channels[-1]))
528+
_log.debug("Add existing channel %s to previous IOC %s", cf_channel, last_ioc_id)
537529
"""In case alias exist, also delete them"""
538530
if cf_config.alias_enabled:
539531
if cf_channel.name in recordInfoByName:
@@ -566,7 +558,9 @@ def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo]
566558
processor.managed_properties,
567559
)
568560
channels.append(alias_channel)
569-
_log.debug("Add existing alias to previous IOC: {s}".format(s=channels[-1]))
561+
_log.debug(
562+
"Add existing alias %s to previous IOC: %s", alias_channel, last_alias_ioc_id
563+
)
570564

571565
else:
572566
"""Orphan the channel : mark as inactive, keep the old hostName and iocName"""
@@ -578,7 +572,7 @@ def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo]
578572
cf_channel,
579573
)
580574
channels.append(cf_channel)
581-
_log.debug("Add orphaned channel with no IOC: {s}".format(s=channels[-1]))
575+
_log.debug("Add orphaned channel %s with no IOC: %s", cf_channel, ioc_info)
582576
"""Also orphan any alias"""
583577
if cf_config.alias_enabled:
584578
if cf_channel.name in recordInfoByName:
@@ -592,16 +586,14 @@ def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo]
592586
alias_channel,
593587
)
594588
channels.append(alias_channel)
595-
_log.debug("Add orphaned alias with no IOC: {s}".format(s=channels[-1]))
589+
_log.debug("Add orphaned alias %s with no IOC: %s", alias_channel, ioc_info)
596590
else:
597591
if cf_channel.name in new_channels: # case: channel in old and new
598592
"""
599-
Channel exists in Channelfinder with same hostname and iocname.
593+
Channel exists in Channelfinder with same iocid.
600594
Update the status to ensure it is marked active and update the time.
601595
"""
602-
_log.debug(
603-
"Channel {s} exists in Channelfinder with same hostname and iocname".format(s=cf_channel.name)
604-
)
596+
_log.debug("Channel %s exists in Channelfinder with same iocid %s", cf_channel.name, iocid)
605597
cf_channel.properties = __merge_property_lists(
606598
[
607599
create_active_property(ioc_info.owner),
@@ -611,7 +603,7 @@ def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo]
611603
processor.managed_properties,
612604
)
613605
channels.append(cf_channel)
614-
_log.debug("Add existing channel with same IOC: {s}".format(s=channels[-1]))
606+
_log.debug("Add existing channel with same IOC: %s", cf_channel)
615607
new_channels.remove(cf_channel.name)
616608

617609
"""In case, alias exist"""
@@ -653,7 +645,7 @@ def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo]
653645
)
654646
)
655647
new_channels.remove(alias_name)
656-
_log.debug("Add existing alias with same IOC: {s}".format(s=channels[-1]))
648+
_log.debug("Add existing alias with same IOC: %s", cf_channel)
657649
# now pvNames contains a list of pv's new on this host/ioc
658650
"""A dictionary representing the current channelfinder information associated with the pvNames"""
659651
existingChannels = {}
@@ -676,7 +668,7 @@ def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo]
676668
searchStrings.append(searchString)
677669

678670
for eachSearchString in searchStrings:
679-
_log.debug("Find existing channels by name: {search}".format(search=eachSearchString))
671+
_log.debug("Find existing channels by name: %s", eachSearchString)
680672
for cf_channel in client.findByArgs(prepareFindArgs(cf_config, [("~name", eachSearchString)])):
681673
existingChannels[cf_channel.name] = cf_channel
682674
if processor.cancelled:
@@ -702,9 +694,7 @@ def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo]
702694
newProps = newProps + recordInfoByName[channel_name].infoProperties
703695

704696
if channel_name in existingChannels:
705-
_log.debug(
706-
f"""update existing channel{channel_name}: exists but with a different hostName and/or iocName"""
707-
)
697+
_log.debug("update existing channel %s: exists but with a different iocid from %s", channel_name, iocid)
708698

709699
existingChannel = existingChannels[channel_name]
710700
existingChannel.properties = __merge_property_lists(
@@ -713,7 +703,7 @@ def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo]
713703
processor.managed_properties,
714704
)
715705
channels.append(existingChannel)
716-
_log.debug("Add existing channel with different IOC: {s}".format(s=channels[-1]))
706+
_log.debug("Add existing channel with different IOC: %s", existingChannel)
717707
"""in case, alias exists, update their properties too"""
718708
if cf_config.alias_enabled:
719709
if channel_name in recordInfoByName:
@@ -731,23 +721,24 @@ def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo]
731721
channels.append(ach)
732722
else:
733723
channels.append(create_channel(alias_name, ioc_info.owner, alProps))
734-
_log.debug("Add existing alias with different IOC: {s}".format(s=channels[-1]))
724+
_log.debug(
725+
"Add existing alias %s of %s with different IOC from %s", alias_name, channel_name, iocid
726+
)
735727

736728
else:
737729
"""New channel"""
738730
channels.append(CFChannel(channel_name, ioc_info.owner, newProps))
739-
_log.debug("Add new channel: {s}".format(s=channels[-1]))
731+
_log.debug("Add new channel: %s", channel_name)
740732
if cf_config.alias_enabled:
741733
if channel_name in recordInfoByName:
742734
alProps = [create_alias_property(ioc_info.owner, channel_name)]
743735
for p in newProps:
744736
alProps.append(p)
745737
for alias in recordInfoByName[channel_name].aliases:
746738
channels.append(CFChannel(alias, ioc_info.owner, alProps))
747-
_log.debug("Add new alias: {s}".format(s=channels[-1]))
748-
_log.info(
749-
"Total channels to update: {nChannels} {iocName}".format(nChannels=len(channels), iocName=ioc_info.iocname)
750-
)
739+
_log.debug("Add new alias: %s from %s", alias, channel_name)
740+
_log.info("Total channels to update: %s for ioc: %s", len(channels), ioc_info)
741+
751742
if len(channels) != 0:
752743
cf_set_chunked(client, channels, cf_config.cf_query_limit)
753744
else:
@@ -829,7 +820,7 @@ def poll(
829820
records_to_delete,
830821
ioc_info: IocInfo,
831822
):
832-
_log.info("Polling {iocName} begins...".format(iocName=ioc_info.iocname))
823+
_log.info("Polling for %s begins...", ioc_info)
833824
sleep = 1.0
834825
success = False
835826
while not success:
@@ -843,4 +834,4 @@ def poll(
843834
_log.info("ChannelFinder update retry in {retry_seconds} seconds".format(retry_seconds=retry_seconds))
844835
time.sleep(retry_seconds)
845836
sleep *= 1.5
846-
_log.info("Polling {iocName} complete".format(iocName=ioc_info.iocname))
837+
_log.info("Polling %s complete", ioc_info)

0 commit comments

Comments
 (0)