I was initially under the impression that a “print” command within a script would always write an entry into the wrapper logfile. However, I think I then read somewhere that this only applies to gateway event scripts.
Is there an easy way to write a custom message to a logfile somewhere?
Ideally I want to be able to do this anywhere scripting can be used in Ignition, i.e. it might be a script on the gateway (timer, tag change etc) or it might be on the client, maybe on a screen, button, wherever really.
E.g. if I had a script which did a few different tasks I might want to log the different stages within it to identify a possible problem.
“About to connect to database…”
“User pressed button X”
“Window Y opened”
I had a look at this thread
but I don’t think that had the answer.
Thanks in advance.
Using the following code will log to the consoles. If will log to each contexts respective console (Gateway > Gateway - Console, Client > Help - Diagnostics > Console, Designer > Tools - Console)
logger = system.util.logger("Test Logger")
logger.info("Info to log")
logger.error("Error to log")
Thanks Kyle, I’ve had a go with that.
Is there a way to view all the info centrally though?
As you say, Gateway events seem to go into the wrapper log and I can also view them via Console on the gateway homepage.
But if I log a message within a client (say on a push button) I need a way to view that from within the gateway somehow - where do the client logs get stored?
The only other thing I wondered about was creating an SQLTag called LogMessage (or similar) and then setting up an OnChange gateway event script for it which would write its contents into a database table and then reset the tag to blank (maybe this could be done with a transaction group).
I don’t know how well that idea would work though if I wanted to log a lot of messages in very quick succession, i.e. in the same script.
Or maybe create my own script module with a LogMessage function?
You could write directly to a database table when you wanted to log something. This would work in all contexts:
message = "My latest log message"
severity = "info"
system.db.runPrepUpdate("INSERT INTO logging (Message,Severity) VALUES (?,?)",[message,info])
Probably best to write a function that wraps the database insert so you could just pass the message and severity to the function as arguments.
The only downside to this method is that in the client it takes a bit of time to run a database query due to network overhead but this is handled easily by wrapping the query with system.util.invokeAsynchronous, but you only need this if you need/want the client to be very quick.
Yes this was one of ideas I had, presumably I could define a function and put it in Script Modules then reference it from any other script.