edit this page - page history - about editing

Controlling logging in OAW

openArchitectureWare

We have to do some magic to enable OAW logging to go through our own logging class. Since OAW instantiates the logs at runtime, and this instantiation is far away from any Eclipse plugin we are using to run it, we have to provide this information to commons.logging directly.

In r613 of IAML I managed to implement this, following the instructions on a German OAW forum.

Create your own Log class:

public class MyLog extends SimpleLog implements org.apache.commons.logging.Log, Serializable {
  private static final long serialVersionUID = 9181448189753075665L;
  
  private static String lastLogFactory = null;
  
  /**
   * Register this log to commons.logging.
   */
  public static void registerToLogFactory()
  {
    lastLogFactory = System.getProperty("org.apache.commons.logging.Log");
    System.setProperty("org.apache.commons.logging.Log", MyLog.class.getName());
  }

  /**
   * Reset the log from commons.logging, so that other plugins
   * continue to work as normal.
   */
  public static void unregisterFromLogFactory()
  {
    if (lastLogFactory == null) {
      // we have to clear it this way, we can't set it to null, or we will get a NullPointerException
      System.clearProperty("org.apache.commons.logging.Log");
    } else {
      System.setProperty("org.apache.commons.logging.Log", lastLogFactory);
    }
  }
  
  /**
   * org.apache.commons.logging.impl.LogFactoryImpl requires a
   * constructor with a String parameters, or a NoSuchMethodException
   * will be thrown.
   * 
   * @param name Name of the log
   */
  public MyLog(String name) { 
    super(name);
    
    setLevel(LOG_LEVEL_ALL);
  }

  /**
   * Override any log methods here.
   */
  @Override
  protected void log(int type, Object message, Throwable t) {
    // TODO Auto-generated method stub
    super.log(type, "[my log] " + message, t);
  }
}

Around your WorkflowRunner instantiation, specify commons.logging to use the current ClassLoader, and register the log with commons.logging:

ClassLoader oldcl = Thread.currentThread().getContextClassLoader();
try {
  // to enable custom logging
  Thread.currentThread().setContextClassLoader(OawCodeGenerator.class.getClassLoader());
  MyLog.registerToLogFactory();

  ...
  new WorkflowRunner().run(wfFile,
    new ProgressMonitorAdapter(monitor), properties, slotContents);
  ...
} finally {
  // reset the classloader/log
  Thread.currentThread().setContextClassLoader(oldcl);
  MyLog.unregisterFromLogFactory();
}

Categories: Code Snippets | OAW | Java
edit this page - what links to here? - page history - top
Last edited by jevon jevon 114 months ago