Skip to content

Commit 963359f

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 2ba0ed7 commit 963359f

File tree

1 file changed

+29
-38
lines changed

1 file changed

+29
-38
lines changed

server/recceiver/cfstore.py

Lines changed: 29 additions & 38 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.load_from_conf(conf)
129+
_log.info("CF_INIT %s", self.cf_config)
129130
self.name = name
130131
self.channel_iocs = 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.load_from_conf(conf)
136136

137137
def startService(self):
138138
service.Service.startService(self)
@@ -261,9 +261,9 @@ def chainError(err):
261261
else:
262262
d.callback(None)
263263

264-
def chainResult(_ignored):
264+
def chainResult(result):
265265
if self.cancelled:
266-
raise defer.CancelledError()
266+
raise defer.CancelledError(f"CF Processor is cancelled, due to {result}")
267267
else:
268268
d.callback(None)
269269

@@ -416,11 +416,11 @@ def clean_service(self):
416416
except RequestException as e:
417417
_log.error("Clean service failed: {s}".format(s=e))
418418
retry_seconds = min(60, sleep)
419-
_log.info("Clean service retry in {retry_seconds} seconds".format(retry_seconds=retry_seconds))
419+
_log.info("Clean service retry in %s seconds", retry_seconds)
420420
time.sleep(retry_seconds)
421421
sleep *= 1.5
422422
if self.running == 0 and sleep >= retry_limit:
423-
_log.info("Abandoning clean after {retry_limit} seconds".format(retry_limit=retry_limit))
423+
_log.info("Abandoning clean after %s seconds", retry_limit)
424424
return
425425

426426
def get_active_channels(self, recceiverid):
@@ -435,7 +435,7 @@ def clean_channels(self, owner: str, channels: List[CFChannel]):
435435
new_channels = []
436436
for cf_channel in channels or []:
437437
new_channels.append(cf_channel.name)
438-
_log.info("Total channels to update: {nChannels}".format(nChannels=len(new_channels)))
438+
_log.info("Cleaning %s channels.", len(new_channels))
439439
_log.debug(
440440
'Update "pvStatus" property to "Inactive" for {n_channels} channels'.format(n_channels=len(new_channels))
441441
)
@@ -478,12 +478,8 @@ def create_time_property(owner: str, time: str) -> CFProperty:
478478

479479

480480
def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo], records_to_delete, ioc_info: IocInfo):
481-
_log.info("CF Update IOC: {iocid}".format(iocid=ioc_info.ioc_id))
482-
_log.debug(
483-
"CF Update IOC: {iocid} recordInfoByName {recordInfoByName}".format(
484-
iocid=ioc_info.ioc_id, recordInfoByName=recordInfoByName
485-
)
486-
)
481+
_log.info("CF Update IOC: %s", ioc_info)
482+
_log.debug("CF Update IOC: %s recordInfoByName %s", ioc_info, recordInfoByName)
487483
# Consider making this function a class methed then 'processor' simply becomes 'self'
488484
client = processor.client
489485
iocs = processor.iocs
@@ -493,38 +489,34 @@ def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo]
493489
iocid = ioc_info.ioc_id
494490

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

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

501497
if processor.cancelled:
502-
raise defer.CancelledError()
498+
raise defer.CancelledError("Processor cancelled in __updateCF__")
503499

504500
channels: List[CFChannel] = []
505501
"""A list of channels in channelfinder with the associated hostName and iocName"""
506502
_log.debug("Find existing channels by IOCID: {iocid}".format(iocid=iocid))
507503
old_channels: List[CFChannel] = [
508504
CFChannel(**ch) for ch in client.findByArgs(prepareFindArgs(cf_config, [("iocid", iocid)]))
509505
]
510-
if processor.cancelled:
511-
raise defer.CancelledError()
512506

513507
if old_channels is not None:
514508
for cf_channel in old_channels:
515509
if (
516510
len(new_channels) == 0 or cf_channel.name in records_to_delete
517511
): # case: empty commit/del, remove all reference to ioc
518-
_log.debug("Channel {s} exists in Channelfinder not in new_channels".format(s=cf_channel.name))
512+
_log.debug("Channel %s exists in Channelfinder not in new_channels", cf_channel.name)
519513
else:
520514
if cf_channel.name in new_channels: # case: channel in old and new
521515
"""
522-
Channel exists in Channelfinder with same hostname and iocname.
516+
Channel exists in Channelfinder with same iocid.
523517
Update the status to ensure it is marked active and update the time.
524518
"""
525-
_log.debug(
526-
"Channel {s} exists in Channelfinder with same hostname and iocname".format(s=cf_channel.name)
527-
)
519+
_log.debug("Channel %s exists in Channelfinder with same iocid %s", cf_channel.name, iocid)
528520
cf_channel.properties = __merge_property_lists(
529521
[
530522
create_active_property(ioc_info.owner),
@@ -534,7 +526,7 @@ def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo]
534526
processor.managed_properties,
535527
)
536528
channels.append(cf_channel)
537-
_log.debug("Add existing channel with same IOC: {s}".format(s=channels[-1]))
529+
_log.debug("Add existing channel with same IOC: %s", cf_channel)
538530
new_channels.remove(cf_channel.name)
539531

540532
"""In case, alias exist"""
@@ -565,7 +557,7 @@ def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo]
565557
)
566558
)
567559
new_channels.remove(alias_name)
568-
_log.debug("Add existing alias with same IOC: {s}".format(s=channels[-1]))
560+
_log.debug("Add existing alias with same IOC: %s", cf_channel)
569561
# now pvNames contains a list of pv's new on this host/ioc
570562
"""A dictionary representing the current channelfinder information associated with the pvNames"""
571563
existingChannels = {}
@@ -588,7 +580,7 @@ def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo]
588580
searchStrings.append(searchString)
589581

590582
for eachSearchString in searchStrings:
591-
_log.debug("Find existing channels by name: {search}".format(search=eachSearchString))
583+
_log.debug("Find existing channels by name: %s", eachSearchString)
592584
for cf_channel in client.findByArgs(prepareFindArgs(cf_config, [("~name", eachSearchString)])):
593585
existingChannels[cf_channel.name] = cf_channel
594586
if processor.cancelled:
@@ -610,9 +602,7 @@ def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo]
610602
newProps = newProps + recordInfoByName[channel_name].infoProperties
611603

612604
if channel_name in existingChannels:
613-
_log.debug(
614-
f"""update existing channel{channel_name}: exists but with a different hostName and/or iocName"""
615-
)
605+
_log.debug("update existing channel %s: exists but with a different iocid from %s", channel_name, iocid)
616606

617607
existingChannel = existingChannels[channel_name]
618608
existingChannel.properties = __merge_property_lists(
@@ -621,7 +611,7 @@ def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo]
621611
processor.managed_properties,
622612
)
623613
channels.append(existingChannel)
624-
_log.debug("Add existing channel with different IOC: {s}".format(s=channels[-1]))
614+
_log.debug("Add existing channel with different IOC: %s", existingChannel)
625615
"""in case, alias exists, update their properties too"""
626616
if cf_config.alias:
627617
if channel_name in recordInfoByName:
@@ -639,23 +629,24 @@ def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo]
639629
channels.append(ach)
640630
else:
641631
channels.append(create_channel(alias_name, ioc_info.owner, alProps))
642-
_log.debug("Add existing alias with different IOC: {s}".format(s=channels[-1]))
632+
_log.debug(
633+
"Add existing alias %s of %s with different IOC from %s", alias_name, channel_name, iocid
634+
)
643635

644636
else:
645637
"""New channel"""
646638
channels.append(CFChannel(channel_name, ioc_info.owner, newProps))
647-
_log.debug("Add new channel: {s}".format(s=channels[-1]))
639+
_log.debug("Add new channel: %s", channel_name)
648640
if cf_config.alias:
649641
if channel_name in recordInfoByName:
650642
alProps = [create_alias_property(ioc_info.owner, channel_name)]
651643
for p in newProps:
652644
alProps.append(p)
653645
for alias in recordInfoByName[channel_name].aliases:
654646
channels.append(CFChannel(alias, ioc_info.owner, alProps))
655-
_log.debug("Add new alias: {s}".format(s=channels[-1]))
656-
_log.info(
657-
"Total channels to update: {nChannels} {iocName}".format(nChannels=len(channels), iocName=ioc_info.iocname)
658-
)
647+
_log.debug("Add new alias: %s from %s", alias, channel_name)
648+
_log.info("Total channels to update: %s for ioc: %s", len(channels), ioc_info)
649+
659650
if len(channels) != 0:
660651
cf_set_chunked(client, channels, cf_config.findSizeLimit)
661652
else:
@@ -737,7 +728,7 @@ def poll(
737728
records_to_delete,
738729
ioc_info: IocInfo,
739730
):
740-
_log.info("Polling {iocName} begins...".format(iocName=ioc_info.iocname))
731+
_log.info("Polling for %s begins...", ioc_info)
741732
sleep = 1.0
742733
success = False
743734
while not success:
@@ -751,4 +742,4 @@ def poll(
751742
_log.info("ChannelFinder update retry in {retry_seconds} seconds".format(retry_seconds=retry_seconds))
752743
time.sleep(retry_seconds)
753744
sleep *= 1.5
754-
_log.info("Polling {iocName} complete".format(iocName=ioc_info.iocname))
745+
_log.info("Polling %s complete", ioc_info)

0 commit comments

Comments
 (0)