AquaLogic User Interaction Development Guide

     Previous Next  Open TOC in new window   View as PDF - New Window  Get Adobe Reader - New Window
Content starts here

Using IDK Logging in Java

This example demonstrates how to enable and use IDK logging in a remote Java application.

  1. The first step in this example is to enable logging programmatically, by defining the logging application name and setting the log to network option to true. For details on logging options, see Configuring Java IDK Logging (web.xml).
    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
  2. Initialize LogFactory. The recommended way to initialize non-web applications is in a static block in the application's main class or a logging utility class. Always check to see if LogFactory has already been initialized (for example, as part of an IDK-based web application).
    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' .

  3. Create an instance of ILogger by calling LogFactory.getLogger. In the code below, the LoggingExample method sends an Info level log message when an instance is created. The snippet below also uses ILogger.functionBegin and ILogger.functionEnd to log when a method is entered and exited, ILogger.action to log significant events, and ILogger.performanceBegin and ILogger.performanceEnd to log the time required to execute the methods.
    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

  4. The code below demonstrates available logging levels and provides an example of how to use token substitution in formatting strings to construct messages. The thread runs through a small test of logging messages and transfers work to the next by calling yield(). Note: Wrap any complex message construction in a conditional block to avoid doing work if there are no listeners at that log level.
    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)


  Back to Top      Previous Next