Click or drag to resize

Log messages in task

Problem

You want to write task messages to a persistent log file.

Solution

Get the reference to the ILog interface by calling getProperty(java.lang.String) and passing in TaskProperties.LOGGER.

Java
ILog logger = (ILog) this.getProperty(TaskProperties.LOGGER);

With an instance of the ILog interface, all log methods are written to the standard host log files by default.

Java
logger.error("My text");
logger.fatal("should appear");
logger.fatal("in the file");
Java
package stkparallelcomputingserversdk.howto;

import agi.parallel.client.ClusterJobScheduler;
import agi.parallel.client.IJobScheduler;
import agi.parallel.client.Job;
import agi.parallel.infrastructure.Task;
import agi.parallel.infrastructure.TaskProperties;
import agi.parallel.infrastructure.logging.ILog;

public class TaskLogging {
    public static void main(String[] args) {
        try (IJobScheduler scheduler = new ClusterJobScheduler("localhost")) {
            scheduler.connect();

            Job job = scheduler.createJob();
            job.addTask(new WriteSomeToLog());
            job.submit();

            job.waitUntilDone();
        }

        /*
         * Go to %ALLUSERSPROFILE%\AGI\STK Parallel Computing Server 2.9\logs to inspect the log
         * files.
         */
    }

    private static class WriteSomeToLog extends Task {
        @Override
        public void execute() {
            ILog logger = (ILog) this.getProperty(TaskProperties.LOGGER);

            logger.error("My text");
            logger.fatal("should appear");
            logger.fatal("in the file");
        }
    }
}
Discussion

Logging can be important for debugging, tracing, and post-mortem analysis. The ILog interface provides "free" logging within tasks. The main advantage to using the ILog interface versus a custom logging mechanism is that ILog is tightly integrated with the existing logging infrastructure. For instance, right clicking on a task within the monitoring applications displays the user log messages in the same logs as the host.

See Also

STK Parallel Computing Server 2.9 API for Java