Testing and debugging event scripts on components is easy. Open up the Output Console in the Designer and sprinkle your code with print statements to see what is going on.
This approach doesn't work as well with gateway scripts because print statements are not output to the Output Console in the Designer.
A gateway script is any Python script that runs in the Gateway and not in a designer or client. Here are a variety of tools to use for testing and debugging gateway scripts:
The Designer has a tool called "Script Playground". It can be found here: Tools -> Script Playground. Take the code in your gateway script and paste it into the Script Playground and run it. See if it works. This kind of testing will work for many gateway scripts.
It may be difficult to catch infrequent problems by running gateway script code in the designer. Some of the other methods of testing and debugging are better suited for that.
Note: Some functions such as system.gui.getWindow and other GUI functions work in the designer but do not work in gateway scripts. Code with such functions will run without error in the Script Playground but won't actually work when run in the Gateway. If code runs fine in the Script Playground but not in a gateway script then use one of the other methods of testing and debugging.
Check the Status -> Gateway Scripts web page in your Gateway for exceptions. This is an easy way to see if an exception is being raised in your scripts and find out what the exception is. Here is an example of a Gateway Timer Script called "test" that has an error:
Ignition 7.6 and earlier has an undocumented scripting function called system.util.logger(). This function is used to create a logger. Once a logger is created messages can be sent to the System Console Gateway webpage. This is a great way to test different parts of gateway scripts. In Ignition 7.7 there is a new documented function called system.util.getLogger() which is used for the same purpose. Here is an example:
You can see the result in the first line of logging in the image below.
Using Try/Except with Logging gives you the most control for handling and reporting exceptions in gateway scripts. Here is an example:
In the example above code is put between try and except. If any exception occurs it gets logged.
This code is especially useful in Ignition 7.7.0 Tag Event Scripts because exceptions in Tag Event Scripts do not show in the System Console and do not show in the Gateway Status web pages, and they are also not logged to the wrapper.log file. There are plans in Ignition 7.7.1 to add default logging of Tag Event Script errors.
Use print statements and the wrapper.log file. This can be a cumbersome way to debug and test gateway scripts if the right tool isn't used. Raised exceptions and the output of print statements are automatically sent to a log file called wrapper.log which exists where Ignition is installed on Windows or /var/log/ignition/ on Linux. A big limitation with this approach is that you have to log in to the server where Ignition is installed to get or see the wrapper.log file.
If Ignition is installed on Linux and you have ssh access to it, the command "tail -F /var/log/ignition/wrapper.log" will show real-time changes to the wrapper.log file. This is a very convenient way to debug and test gateway scripts.
If Ignition is installed on Window, similar tail programs like Tail for Win32 can be used.
Note: In Ignition 7.7.0, exceptions in Tag Event Script are not automatically logged to the wrapper.log file. The "Try/Except with Logging" method must be used.
Please leave a comment about this article. Let me know about your experiences with testing and debugging Gateway Event Scripts. Or let me know if this blog post helps you.