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  Copy imageCopy
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  Copy imageCopy
logger.error("My text");
logger.fatal("should appear");
logger.fatal("in the file");
Java  Copy imageCopy
package stkscalabilitysdk.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) {
        IJobScheduler scheduler = new ClusterJobScheduler("localhost");
        try {
            scheduler.connect();
            Job job = scheduler.createJob();
            job.addTask(new WriteSomeToLog());
            job.submit();
            job.waitUntilDone();
        } finally {
            scheduler.dispose();
        }

        /*
         * Go to %ALLUSERSPROFILE%\AGI\STK Scalability 1.2\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