Logging from Within a Class

How can I perform logging from within a Jython class? I currently have the logger setup like the following.

In my configuration script file:

logger=system.util.getLogger("MyLogger")
globals={'logger':logger}

In my class script file:

import project.config.globals as globals
logger=globals['logger']
class HelperClass(object):
    def testFunction(self):
        logger.trace("Log this statement")

I also tried to use the logger variable directly from the config script definition, but that didn’t work either so I tried the dictionary approach as sited by pturmel.

Edit:
Function definition corrected. I removed the content of the class for the example pasted here. The use details are from a button action script:

import project.Helpers as Helpers

test=Helpers.HelperClass()
test.testFunction()

I expected a log entry.

A few of things:

  1. Is this the entire script? I see no instantiation of the class or call to the function, so I wouldn’t expect it to actually log anything
  2. Be careful with scoping.
  3. I’m not completely certain but unless you are intending to make HelperClass a subclass of “object”, then your class signature is incorrect.
  4. You will probably be required to include a ‘self’ parameter in the function call

I would tend to write your class like this to ensure scoping is valid:

import project.config.globals as globals

class HelperClass():
     _logger=globals['logger']
     def testFunction(self,object):
          _logger.trace('Log this statement')

#Then at some other point in the script
HelperClass.testFunction(object)
1 Like

Edit made to the post.

Depending on where you are looking for the log message to be, I assume the client or the designer, ensure that the logging level is set to trace, otherwise you will not see the log. The default logging level of the console in the designer is info, so you will not see a message in the console of the designer if you are using trace, however, if you are looking in the client then you should be able to see them so long as the logging level is set correctly.

By default the logging level is set at Info, so Trace and Debug logs are dropped. Try using info and see if the message shows up as you would expect.

import project.config.globals as globals
logger=globals['logger']
class HelperClass(object):
     def testFunction(self):
          logger.info('Log this statement')

How can I get into the system logs if this is running as a shared script? It the object instantiation on the client and that is why it is failing to so into the system logs (gateway page --> status --> logs)?

On the gateway page Status->Diagnostics->Logs

Click on the settings button and set the logging level to TRACE if it isn’t already.

Accept the changes. Note that the logging level is reset to INFO any time the Gateway is restarted.

The logger name I have set is not available, so no entries have been made? I can’t se the level until at least one entry is made. I have some info-level entries in the real application which should be included with the default settings.

Shared scripts don't automatically run on the gateway - they're just able to be called from any scope. You would have to run whatever event you're actually running on the gateway to directly use system.util.getLogger and have it show up in the gateway logs.