[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[xmlblaster] xmlBlaster LogChannelWrapper for log4j



Hi

due to the whish having log from xmlBlaster in log4j we need a class like
LogChannelWrapper.java. Thanks to Uwe and Juergen for their support.

This class can't go to the CVS since it needs log4j to compile which isn't
part of xmlBlaster (yet?).

enjoy!

kind regards

Heinrich
// Import xmlBlaster classes
import org.xmlBlaster.util.Global;

// Import jutils classes
import org.jutils.log.LogableDevice;
import org.jutils.log.LogChannel;

// Import log4j classes
import org.apache.log4j.Logger;


/**
 * <p>
 * This class wraps the logging from <a href="http://www.jutils.org";>http://www.jutils.org</a>
 * to be used with <a href="http://jakarta.apache.org/log4j";>http://jakarta.apache.org/log4j</a>.
 * </p>
 *
 * <p>
 * usage:
 * create a LogChannelWrapper either with or without global and log:
 * <pre>
 *   //LogChannelWrapper lcw = new LogChannelWrapper();
 *   LogChannelWrapper lcw = new LogChannelWrapper(glob, log);
 *
 *   // clear and initialise the LogChannels to .this class.
 *   lcw.initXmlBlasterLogging();
 * </pre>
 * </p>
 *
 * <br><br>
 * <i><small>
 * Copyright     Copyright (c) 2002,
 *               doubleSlash Net-Business GmbH, http://www.doubleSlash.de
 * <br><br>
 *  at version      $Id: LogChannelWrapper.java,v 1.1 2002/10/15 08:01:18 hgoetzger Exp $
 * <br>
 *  at author       <a href="mailto:Heinrich.Goetzger at exploding-systems.de";>Heinrich G&ouml;tzger</a>
 * </i></small>
 * 
 */

public class LogChannelWrapper implements LogableDevice {

  private Global glob = null;
  private Logger log = null;
  private boolean xmlBlasterLoggingInitialized = false;

	/**
	 * Creates a LogChannelWrapper.
	 * It get's an instance of the global object and creates a new instance
	 * of a Logger.
	 */
  public LogChannelWrapper() {
    glob = Global.instance();
    log = Logger.getLogger("org.xmlBlaster");
  } // end of constructor

	/**
	 * Creates a LogChannelWrapper.
	 * The global instance and the log instance will be given by the user.
	 *  at param global  instance of Global
	 *  at param log     instance of Logger
	 */
  public LogChannelWrapper(Global global, Logger log) {
    this.glob = global;
    this.log = log;
  } // end of constructor


  /**
   * add 'this' to all known LogChannels
   * so this.log() will be triggerd for all xmlBlaster specific logs.
   * So we have a chance, to log xmlBlaster stuff into our logfile.
   */
  public void initXmlBlasterLogging() {

    if (xmlBlasterLoggingInitialized)
      return;
    xmlBlasterLoggingInitialized = true;
		glob.getLog(null).removeAllDevices();
		glob.getLog("core").removeAllDevices();
    glob.getLog("auth").removeAllDevices();
    glob.getLog("cb").removeAllDevices();
    glob.getLog("mime").removeAllDevices();
    glob.getLog("corba").removeAllDevices();
    glob.getLog("xmlrpc").removeAllDevices();
    glob.getLog("admin").removeAllDevices();
    
		glob.getLog(null).addLogDevice(this);
		glob.getLog("core").addLogDevice(this);
    glob.getLog("auth").addLogDevice(this);
    glob.getLog("cb").addLogDevice(this);
    glob.getLog("mime").addLogDevice(this);
    glob.getLog("corba").addLogDevice(this);
    glob.getLog("xmlrpc").addLogDevice(this);
    glob.getLog("admin").addLogDevice(this);
  } // end of initXmlBlasterLogging


	/**
	 * <p>
	 * Event fired by LogChannel.java through interface LogableDevice.
	 * This method delegates all incoming logging request to the log4j
	 * log instance.
	 * </p>
	 * If no log devices are registered nothing happens.
	 * The logging level needs to be checked from the calling method.
	 *  at param      level
	 *             the value of the debug level, allowed values are
	 *             LogConstants.LOG_ERROR, LogConstants.LOG_WARN,
	 *             LogConstants.LOG_INFO, ...
	 *  at param      source
	 *             the name or identifier of the initiator of this logging
	 *  at param      str
	 *             the data which should be logged
	 */
   public void log(int level, String source, String str) {

     StringBuffer logTxt = new StringBuffer();
     logTxt.append("[").append(source).append("] ").append(str);

     switch (level) {
     case LogChannel.LOG_CALL:
     case LogChannel.LOG_DUMP:
     case LogChannel.LOG_TIME:
     case LogChannel.LOG_TRACE:
       if (log.isDebugEnabled())
       log.debug(logTxt);
       break;
     case LogChannel.LOG_ERROR:
       log.error(logTxt);
       break;
     case LogChannel.LOG_INFO:
       log.info(logTxt);
       break;
     case LogChannel.LOG_WARN:
       log.warn(logTxt);
       break;
     default:
       log.info(logTxt);
       break;
     }; // end of switch
   } // end of log

} // end of class