This example demonstrates how to enable and use IDK logging in a remote Java application.
import com.plumtree.remote.logging.ILogger; import com.plumtree.remote.logging.LogFactory; public class LoggingExample extends Thread { private static final String INSTANCES_COMPONENT_NAME = 'Instances'; private static final String MAIN_LOOP_COMPONENT_NAME = 'Main Loop'; // set the application name // (legal characters: ASCII alphanumerics plus . - _ and space) public static final String LOGGING_APPLICATION_NAME = 'Logging_API_Example-1'; // set to true to multicast log messages to local network // set to false to send message only listeners on local machine public static final boolean LOG_TO_NETWORK = true; private ILogger logger; //instance logging class private static ILogger mainLogger; // main component logging class
if (!LogFactory.isInitialized()) { LogFactory.initialize(LOGGING_APPLICATION_NAME, LOG_TO_NETWORK); } System.out.print('Set your logging receiver to the \'server\' or \'application name\' '); System.out.println(LogFactory.getApplicationName()); System.out.println('The logging component names are \'EDK\', \'' + MAIN_LOOP_COMPONENT_NAME + '\' and \'' + INSTANCES_COMPONENT_NAME + '\'.'); mainLogger = LogFactory.getLogger(MAIN_LOOP_COMPONENT_NAME, LoggingExample.class);
This code creates the following messages in ALI Logging Spy. These messages are sent automatically by the IDK. For the sample code above, the <app name> entry would be Logging_API_Example-1.
1 <#> <app name> <date/time> Info EDK main LogFactory Initiating EDK logging on behalf of EDK: LogFactory.
2 <#> <app name> <date/time> Info EDK main LogFactory Verbose logging of internal EDK classes is off. It may be enabled by setting ptedk.VerboseLogging='true' .
public LoggingExample(String instanceName) { setName(instanceName); this.logger = LogFactory.getLogger(INSTANCES_COMPONENT_NAME, LoggingExample.class); mainLogger.info('Created new instance named {0}', instanceName); } public static void main(String[] args) { final String methodName = 'main'; mainLogger.functionBegin(methodName); // get a timestamp to measure performance of this function long performanceStartTicks = mainLogger.performanceBegin(); mainLogger.action('Creating and starting instances'); LoggingExample bill = new LoggingExample('Bill'); bill.start(); LoggingExample larry = new LoggingExample('Larry'); larry.start(); mainLogger.action('Done creating instances'); // send log message with time since performanceBegin mainLogger.performanceEnd(methodName, performanceStartTicks); mainLogger.functionEnd(methodName); }
This code creates the following messages in ALI Logging Spy.
3 <#> <app name> <date/time> Function Main Loop main LoggingExample Entering Function main
4 <#> <app name> <date/time> Action Main Loop main LoggingExample Creating and starting instances
5 <#> <app name> <date/time> Info Main Loop main LoggingExample Created new instance named Bill
6 <#> <app name> <date/time> Info Main Loop main LoggingExample Created new instance named Larry
7 <#> <app name> <date/time> Action Main Loop main LoggingExample Done creating instances
8 <#> <app name> <date/time> Performance Main Loop main LoggingExample main took 0 ms.
9 <#> <app name> <date/time> Function Main Loop main LoggingExample Leaving Function mainInfo
public void run() { String levelDescriptionFormat = '{0} level messages are {1} by default in the log receiver.'; logger.debug(levelDescriptionFormat, 'Debug', 'off'); logger.info(levelDescriptionFormat, 'Info', 'off'); logger.warn(levelDescriptionFormat, 'Warn', 'on'); logger.error(levelDescriptionFormat, 'Error', 'on'); logger.fatal(levelDescriptionFormat, 'Fatal', 'on'); yield(); // Exceptions may also be caught and logged, and may use token substitution try { throw new InterruptedException(getName() + ' was interrupted.'); } catch (Exception eCaught) { logger.warn(eCaught, 'Caught an exception from {0}. ', eCaught.getClass().getPackage().getName()); } }
This code creates the following messages in ALI Logging Spy:
10 <#> <app name> <date/time> Function Instances Larry LoggingExample Entering Function run
11 <#> <app name> <date/time> Action Instances Bill LoggingExample Action log messages are on by default in the log receiver.
12 <#> <app name> <date/time> Debug Instances Bill LoggingExample Debug level messages are off by default in the log receiver.
13 <#> <app name> <date/time> Info Instances Bill LoggingExample Info level messages are off by default in the log receiver.
14 <#> <app name> <date/time> Warning Instances Bill LoggingExample Warn level messages are on by default in the log receiver.
15 <#> <app name> <date/time> Error Instances Bill LoggingExample Error level messages are on by default in the log receiver.
16 <#> <app name> <date/time> Fatal Instances Bill LoggingExample Fatal level messages are on by default in the log receiver.
17 <#> <app name> <date/time> Action Instances Larry LoggingExample Action log messages are on by default in the log receiver.
18 <#> <app name> <date/time> Debug Instances Larry LoggingExample Debug level messages are off by default in the log receiver.
19 <#> <app name> <date/time> Info Instances Larry LoggingExample Info level messages are off by default in the log receiver.
20 <#> <app name> <date/time> Warning Instances Larry LoggingExample Warn level messages are on by default in the log receiver.
21 <#> <app name> <date/time> Error Instances Larry LoggingExample Error level messages are on by default in the log receiver.
22 <#> <app name> <date/time> Fatal Instances Larry LoggingExample Fatal level messages are on by default in the log receiver.
23 <#> <app name> <date/time> Warning Instances Bill LoggingExample Caught an exception from - java.lang. java.lang.InterruptedException: Bill was interrupted. - java.lang.InterruptedException: Bill was interrupted. at - com.plumtree.remote.logging.example.LoggingExample.run(LoggingExample.java:110)
24 <#> <app name> <date/time> Warning Instances Larry LoggingExample Caught an exception from - java.lang. java.lang.InterruptedException: Larry was interrupted. - java.lang.InterruptedException: Larry was interrupted. at - com.plumtree.remote.logging.example.LoggingExample.run(LoggingExample.java:110)