UDT Instance tag change not working

I figured out the commented-out lines still caused failures.

I'm interested in understanding your reasoning for this, beyond just preference and this particular Ignition bug. I really just adopted using .format() generally because at the time I was self educating on Python it was presented as the modern way of doing it and I've grown accustomed to it. I also like that it automatically handles string conversion for various objects types and feels better than string concatenation with +... But I'm feeling I can be swayed with some solid rationale.

How is .infof() ...

The printf-style logger methods entirely skip the string formatting operation if the log level is disabled. When enabled, the formatting operation is pure java, not jython.

If you are careful to avoid preprocessing arguments for the loggers, the disabled level case factors out to close to nothing, CPU-wise. You should try to make sure your jython class objects, if you are using any, have proper __str__() implementations, so you can pass the object directly to loggers. The call to __str__() will be skipped where possible.

1 Like

@Thomas_Salisbury , I confirmed that the first example below will silently fail but the second will generate the log messages.

def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	logger = system.util.getLogger(tag.name)
	logger.info("valueChanged: tagPath={0}, previousValue={1}, currentValue={2}, initialChange={3}, missedEvents={4}".format(
		tagPath, previousValue, currentValue, initialChange, missedEvents
	))
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	logger = system.util.getLogger(tag.name)
	logger.info("valueChanged: tagPath={}, previousValue={}, currentValue={}, initialChange={}, missedEvents={}".format(
		tagPath, previousValue, currentValue, initialChange, missedEvents
	))

And this one works also, per @pturmel's recommendation.

def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	logger = system.util.getLogger(tag.name)
	logger.infof("valueChanged: tagPath=%s, previousValue=%s, currentValue=%s, initialChange=%s, missedEvents=%s",
		tagPath, previousValue, currentValue, initialChange, missedEvents
	)

Note Phil's caveat here:

.format is generally more useful/readable and you're more than welcome to use whatever (Python 2) formatting strategy you want for general purpose assembly of strings. Phil's advice is specifically when dealing with logging.

As a further aside, since I'm surprised Phil hasn't brought it up yet: Note that you can also immediately transfer from a tag script to a project library script (in the gateway scripting project) and write the script however you like, without this issue with format strings. It's only curly braces within the literal code string(s) directly attached to the tag that cause the issue.

Only so much bandwidth today for swatting code smells. :neutral_face:

1 Like

funny thing. I literally did that. moved the script to the project library and just called it form the tag change

1 Like