Script `print()` statements do not show up in the Output Console

is there some setting to allow scripts to print to Output Console? it works fine in the Script Console window, but does not in the Output Console.

1 Like

If using perspective, try system.perspective.print('Hello World')

print() calls end up in the wrapper.log file, I believe. The previously mentioned system.perspective.print() is the correct way to log session info in Perspective. If you want information logged at the Gateway level, then you should use system.util.getLogger() to create a logger, and then .info() to log a message.

While the script console can be used to test some scripts, it should not be used as an indication your script will work in all situations, as there are various scopes a script could be executed in.

Just note that, if you use a logger in different scopes, eg the designer script console or in Vision, then the logger will be created within that scope. For example, the designer loggers are accessed from Help -> Diagnostics, and under.. one of the tabs (i forget that name). Also take note of the minimum logger level being logged for your logger. The default is INFO, and you won't see any TRACE logs.

1 Like

okay. i have stored most of this for future reference, TBH. all i'm looking for is a dump to the Output Console so i can keep track of variable contents, &c without having to open a bajillion windows, most of which stop the script from running thus defeating the purpose. i know i have a lot to learn, and maybe i just need to make some tags to dump stuff to and then watch them in the Tag Browser (?), but i'll give system.perspective.print() a whirl and see if that's the bunny i'm hunting.

thank you all. get back to you shortly.

By far the simplest for initial testing of scripts in Perspective is to use system.perspective.print which will write to the browser's console (for the Designer, this is the Designer's console). For production however, you should be utilising loggers (system.util.getLogger(loggerName)) and log any errors, info, and/or trace logs to these. The preference is to create script libraries within the Scripting section of the Designer for the majority of your code, and call these functions from within your gui. I normally init a logger at the top of each script library which I use in all of the functions within the library:

LIBRARY = 'shared.util.something'
LOGGER = system.util.getLogger('Scripting-{}'.format(LIBRARY))
def foo():
    LOGGER.trace("i'm here!")
    LOGGER.info('hello!')
    LOGGER.error('goodbye!')

To view the logs, just go to the gateway Status webpage and to the Logs section. From there, you can filter to the script library you're looking at, and if needed, also change the minimum logging level to TRACE while you're testing to see your trace messages.

1 Like

this is some top-flight info here! wow. lemme make sure i understand:

at the top of every script you write, you add that code snippet to log anything that script does when you call LOGGER.trace/info/error... from within that script's functions. which then show up in the Ignition gateway's Logs; each prefaced log prefaced by Scripting-make.pancakes, for example. and in this case the LIBRARY would be make.pancakes, of which the foo function is a part.

do i have that correct? if so, that's fantastic! thank you. incredibly helpful.

if i don't, show me where i goofed up. :smiley:

No, the code snippet is placed at the top of each script LIBRARY (Scripting > Project Library > X). Then, as he's using that library he uses the logger of that specific library for clarity on where the logging is originating from.

Just going to put this here for some more clarification on 'print()'

2 Likes

*click* got it. thanks @nminchin. still getting up to speed here, but this has proven very useful in having a solid foundation. good logging, for me, is critical, so starting off on the best foot possible is a great blessing. thanks again. :smiley: