Hi Stuart,

For future reference, your question is more appropriate for the log4j-user
email list.

What you have effectively done is create a wrapper class around the log4j
logger class.  If you want to have log4j display the class from which the
original trace statement is called, then you need to tell log4j the "fully
qualified class name" (FQCN) of the wrapper class.  I would recommend
looking at the mail archives for log4j-user for examples and discussion.  I
thought we had added a wrapper example to the log4j wiki pages, but I cannot
find it now.  Maybe someone else can point us to it or create a new wiki
page.

I would also suggest looking into using the log4j MDC class for storing your
user id.  It supports a generic mechanism to store key/value information per
thread, and then have that information printed to trace messages.

hth,
-Mark

> -----Original Message-----
> From: Stuart Yoxall [mailto:[EMAIL PROTECTED]
> Sent: Thursday, July 17, 2003 2:09 AM
> To: '[EMAIL PROTECTED]'
> Subject: 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: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to