Logging scripts

I found this old thread in the forum that shows a way to log Gateway scripts by importing Apache’s log4j, and thought I’d give it a try. But as far as I can tell, it’s not working.

I used the code shown in that thread, and left the logger named “Test”, since I’m just playing with it at this point

[code]#Import Apache log4j
import sys
from org.apache.log4j import LogManager

#Create the logger with the name you want
logger = LogManager.getLogger(“Test”)

#Log the message

…and I hit submit instead of preview…

Anyway, I have called that directly in a Gateway Timer script, and it didn’t log anything. I have also tried making a script module that imports the apache module and sets up the logger, and that didn’t work from a Gateway script nor from the script playground.

Should I be using one of the existing Loggers from the System Console Levels page? If I can actually create one and call it whatever I want, should that Logger start showing up in this page so I can set a Level for it.

Also, should this work for scripts on Client screens as well from the Gateway?

To get an instance of a logger, use system.util.logger(“LoggerName”).loglevel where loglevel is one of the following:

This can be used in any context.

Ah, I’ve never seen that system.util.logger function. Is it documented anywhere? I can’t figure out what to do with that instance. It doesn’t seem to have much functionality attached to it:

[code]import system

logger = system.util.logger(“Alarming”).info
print dir(logger.class)[/code]

returns: [‘call’, ‘class’, ‘cmp’, ‘delattr’, ‘doc’, ‘get’, ‘getattribute’, ‘hash’, ‘init’, ‘new’, ‘reduce’, ‘reduce_ex’, ‘repr’, ‘setattr’, ‘str’, ‘im_class’, ‘im_func’, ‘im_self’]

Also, can I use this to create a new Logger, or do I have to use an existing one? And if I have to use an existing one, are there established best practices for which of them to use?

That call returns a log4j logger, similar to the code you posted above.

You would do something like

system.util.logger("Test Logger").info("Stuff to log")


logger = system.util.logger("Test Logger") logger.info("Info to log") logger.error("Error to log")

I tried running that exact code (with an import system) in the script playground. It did cause some things to print out to my output console, but nothing got written to the wrapper.log, nor did I see anything on the system console.

Am I missing a step here? :scratch:

I was only able to get the code to work in the Gateway context. In other words, it looks like it has to run in a Gateway Timer script, for example. This is true for either the system.util.logger code or the original code that was posted. Both work.

Okay. I’m actually working out some other Gateway Timer script issues right now, so once that’s sussed out I’ll try some dummy logging.

Thanks for the tip!