Logging in Maximo using Script

a person writing on white paper

Usually when developing several lines of code it is useful to write some information or comments in the Log File to verify that the code is executing as we wish.

To print in the MAXIMO log during the execution time of the Script, we can do it in the following ways:

Using a Logger instance

To instantiate a logger, we must first import the MXLoggerFactory class.

from psdi.util.logging import MXLoggerFactory

Then we instantiate a desired logger

logger = MXLoggerFactory.getLogger("maximo.script.custom")

Once an instance of the logger is created, in this case maximo.script.custom, we write messages in the System Log using the following line of code:

logger.info("Hello Maximo Insights!")

This would be the complete code of the example:

from psdi.util.logging import MXLoggerFactory

logger = MXLoggerFactory.getLogger("maximo.script.custom")
logger.info ("Hello Maximo Insights!")

# Printing with different Logging level
logger.fatal("FATAL logging level")
logger.error("ERROR logging level")
logger.warn("WARN logging level")
logger.info("INFO logging level")
logger.debug("DEBUG logging level")

Invoking ScriptService

In this case, it is easier to write to the MAXIMO log. We just need to use the implicit variable service, which is an instance of com.ibm.tivoli.maximo.script.ScriptService, and call its log() method.

service.log("Hello Maximo Insights!")

# Printing with different Logging level
service.log_fatal("FATAL logging level")
service.log_error("ERROR logging level")
service.log_warn("WARN logging level")
service.log_info("INFO logging level")
service.log_debug("DEBUG logging level")

As we can see, we don’t need to import libraries or instantiate any loggers, we just call the log() method. This is possible because by using the service implicit variable, internally it already imported the MXLoggerFactory class and also already created an instance of the maximo.script.<Script_Name> logger.

If we want to use another logger, we can do it as follows:

logger = service.getLogger("maximo.script.custom")
logger.log("Hello Maximo Insights!")

# Printing with different Logging level
logger.fatal("FATAL logging level")
logger.error("ERROR logging level")
logger.warn("WARN logging level")
logger.info("INFO logging level")
logger.debug("DEBUG logging level")

If you found my post interesting or useful and just want to say thanks, you can always buy me a coffee.

Leave a Reply