↓ Skip to main content
An ink and watercolour drawing of a round metal sieve on a wooden stand above an open notebook on a workbench. Four small paper slips in orange, teal and rose lie caught in the sieve, three slips have fallen through onto the notebook's pages, and a pencil lies beside it.

Logging From a Script Without Slowing It Down

Maximo Automation Scripts - This article is part of a series.
Part 14: This Article
Most logging advice for automation scripts points at the wrong setting. The one that decides what a script writes is its own Log Level field. This article shows how the log methods behave, why print output gets lost, and how to write log calls that cost nothing when nobody reads them.

This article closes Part 3 of the series. We checked IBM’s pages about logging against the behaviour of Manage in the MAS 9.2 series, and we ran test scripts at two log levels on our test system. Where the pages and Manage disagree, this page follows Manage.

The setting that decides
#

Every script logs to its own logger, maximo.script.<SCRIPTNAME>. Its parent, maximo.script, is the logger that the Logging application calls autoscript.

At the start of every run, Manage sets the script’s own logger to the value of the script’s Log Level field. We saw that on our test system: a script at log level DEBUG had debug output on, and the same script at log level ERROR had even info output off.

Which setting decides what a script writes Which setting decides what a script writes
Which setting decides what a script writes

Two consequences:

  • The autoscript logger is the wrong switch. IBM’s pages tell you to set it to the same level as the script. The script’s own level is the one that counts, because its logger has a level of its own.
  • A level set in the Logging application does not hold. If you give maximo.script.MYSCRIPT a level there, the next run of the script overwrites it with the Log Level field.

So to see a script’s output, set its Log Level field to INFO or DEBUG. The default is ERROR.

The log methods
#

MethodWrites at
service.log(msg)DEBUG when the script’s level is DEBUG, INFO otherwise
service.log_debug(msg) to service.log_fatal(msg)its own level; each also takes an error as a second argument
service.logError(msg)ERROR

service.log has a trap: at the default level ERROR it writes nothing, because it writes at INFO. Use log_info or log_error when you mean a specific level.

Each call goes to the log at once. That matters for long scripts, where you want to see progress while they run.

print, stderr and the test dialog#

print works differently from the log methods:

  • It is buffered. Manage collects the output and writes it in one block after the script has ended, at INFO or DEBUG, depending on the script’s level. At WARN, ERROR or FATAL it is not written.
  • It is lost when the script fails. The error path skips the step that writes the buffer, so the lines you need most never reach the log.
  • Output to stderr is written at ERROR.

The Test Script dialog of the Automation Scripts application has its own rule. service.log and service.logError go to the dialog’s process log and skip the log file. The log_ methods go to the logger as usual.

A guard that makes debug logging free
#

A log call builds its message first, and only then does the logger decide whether to write it. So this line runs a database count on every run, even when debug logging is off:

service.log_debug("open lines: " + str(lines.count()))

Ask the logger first:

log = service.getLogger("maximo.script." + service.getScriptName())
if log.isDebugEnabled():
    service.log_debug("open lines: " + str(lines.count()))

The guard costs one check. The count runs only when someone reads the output.

Two details make this guard work everywhere:

  • Ask the script’s own logger, maximo.script.<SCRIPTNAME>. Its parent can have a different level.
  • Use service.getScriptName(). The variable scriptName is missing in a script called through the REST API. On our test system, a guard built on scriptName failed there with a NameError.

IBM’s best-practice pages recommend service.isLoggingEnabled() for this check. The method does not exist in the 9.2 series, and our test call failed with an AttributeError.

The framework’s own debug output
#

At the script’s level DEBUG, Manage writes useful lines of its own to the script’s logger:

  • which launch point and script are about to run,
  • every variable value that goes into the script, and every value it copies back,
  • the run time, as “The total time taken to execute the … script for the … launch point”.

In the lines that log the values copied back, a variable whose name starts and ends with an underscore, such as _apikey_, has its value hidden. The rule covers those lines, so keep secrets out of debug runs all the same.

A dump of the whole context after the run goes to the parent logger maximo.script. For that one, both the script and the autoscript logger need DEBUG.

IBM’s pages say that the run time is always written. It is written only at DEBUG.

A log file of its own
#

The Logging application can give script output a file of its own: an appender of the class psdi.util.logging.MXFileAppender on the autoscript logger. Manage turns it into a rolling file: 5 MB per file and 20 old files when the fields are empty, and a new file at every server start.

What IBM’s pages get wrong here
#

TopicIBM’s pagesWhat Manage does
The switchset the autoscript logger to the script’s levelthe script’s Log Level field decides
One logger per scriptcreate a logger with the script’s name and set its levelthe logger exists, and every run overwrites its level
service.isLoggingEnabled()checks whether logging is ondoes not exist; AttributeError
The run timealways writtenwritten only at DEBUG
The test dialogshows all logsshows only service.log and service.logError

Article 33 of this series collects the corrections that matter most.

What comes next
#

This ends Part 3. Part 4 is about data: why you let Manage save, how to ask whether a row exists, and what a script costs.

IBM’s own pages are the reference to keep next to this series:

The complete list of corrections, with the evidence for each one, is in our member area. Members get access on request, so get in touch if you want it. If you want help with the scripts in your own Manage, our services page shows how we work.

IBM and Maximo are trademarks of International Business Machines Corporation, registered in many jurisdictions worldwide.

Maximo Automation Scripts - This article is part of a series.
Part 14: This Article