You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by Stuart Yoxall <st...@optimad.com> on 2003/07/17 11:09:27 UTC

wrong class and method information when logging

Hi,

I am new to using log4j. I want to append the current userid to the output
in the logfile. I am using the following code to store the userid:

public class ThreadProperties {

    public static final long NO_USER_ID_AVAILABLE = -1;

    private static ThreadLocal currentUserID = new ThreadLocal() {
	protected synchronized Object initialValue() {
	/*
	 * our initial value will be a null
	 * we do not want to return a default value
	 */
	    return new Long( NO_USER_ID_AVAILABLE );
	}
    };

    /**
     * Used to set the thread scoped userID variable.
     * @param id int
     */
    public static void setCurrentUserID(long id) {
	/*
	* return the userID available through the static userID ThreadLocal
object
	*/
	currentUserID.set( new Long(id) );
    }

    public static long getCurrentUserID() {
	/*
	* return the userID available through the static userID ThreadLocal
object
	*/
	return ( (Long) (currentUserID.get()) ).longValue();

    }

}

My Logger is as follows:

package com.mycompany.util.logger;

import org.apache.log4j.Logger;

public class OptimadLogger{

  public static final String EMPTY_STRING_WITH_SPACE = " ";
  public static final String EMPTY_STRING = "";
  private Logger instanceLogger =null;

  private String getIDString(){
      StringBuffer sb = new StringBuffer();
      sb.append(" [User ID=");
      sb.append(ThreadProperties.getCurrentUserID());
      sb.append("]");
      sb.append(EMPTY_STRING_WITH_SPACE);
      return sb.toString();
  }

  private void setInstanceLogger(Logger aLogger){
      instanceLogger = aLogger;
  }

 public static OptimadLogger getOptimadLogger(String logger){
    OptimadLogger log = new OptimadLogger();
    log.setInstanceLogger(Logger.getLogger(logger));
    return log;
 }

 public void debug(Object ob){
    instanceLogger.debug(getIDString() + ob);
 }

 public void debug(Object ob, Throwable t){
    instanceLogger.debug(getIDString() + ob, t);
 }

/** Other logging methods left out*/

 public boolean isDebugEnabled(){
    return instanceLogger.isDebugEnabled();
 }
}

The above logs the userid to the logfile but I loose the class and method my
logger was called in. The trace shows:

2003-07-16 15:38:33,165 [DEBUG] OptimadLogger.debug -  [User ID=4] SQL
statement closed

as opposed to :

2003-07-16 15:38:33,165 [DEBUG] MyDAO.myMethod -  [User ID=4] SQL statement
closed

can i tell log4j to record the class that called my logger as opposed to the
logger itself?

thanks




---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-dev-help@jakarta.apache.org