You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Vivek Kapadekar <vk...@bitfone.com> on 2003/06/05 00:07:55 UTC

log4J and Wrapper

hi All
We want to  write our own wrapper around log4J, a very simplistic one.
Is  there one already written by some one, which is advisable to use?
Also with the wrapper, the log message, show the message originating
from the wrapper instead of the Source class? Is forcedLog() with FQCN
the only way to get around it ?

Thanks
--Viv



    I discovered that specifying the FCQN  of the wrapper class will do the
    trick. If you look at code in LocationInfo, it basically picks up the
    line in the stack trace appearing before the FCQN of the logger class,
    which by default will be set to the Category which logs the message. 
    
    So instead of logging message in you Wrapper like 
    
    MyLogWrapper {
       debug(String mssg) 
       {
          _logger.debug(mssg);
       }
    }
    
    Try logging it using
    MyLogWrapper {
       debug(String mssg) 
       {
          _logger.log(_fcqn, Priority.DEBUG, mssg, null);
       }
    }
    
    This will ensure the correct location info is printed.
    
    
    ~preetham
    
    
       
    
    > -----Original Message-----
    > From: Amandeep Midha [mailto:Amandeep.Midha@itsprojects.com] 
    > Sent: Monday, May 26, 2003 3:26 PM
    > To: Log4J Users List
    > Subject: Re: Getting location and line number of log messages
    > 
    > 
    > 
    > Hi Preetham,
    > 
    > I am using the following context capturing code in 
    > wrapper....hope this is useful for you!
    > 
    > best regards,
    > Amandeep
    > 
    > 
    > LogMgrImpl()//String name)
    >     {
    >         objLogger = Logger.getRootLogger();
    >         swObj = new StringWriter();
    >         pwObj = new PrintWriter(swObj);
    >     }
    > 
    >     /**
    >      * Takes the priority level and message to log the information.
    >      * It also logs the context from where the message is 
    > logged like class name,method name with line no
    >      * @param level - Priority Level ( as String )
    >      * @param message - Log information
    >      */
    >     public void logContext(String strLevel , Object objMessage) {
    >         if(!isLogEnabled(strLevel))
    >             return;
    >         String strMsg;
    >         synchronized(swObj) {
    >             Throwable objThrowable = new Throwable();
    >             objThrowable .printStackTrace(pwObj);
    >             strMsg = swObj.toString();
    >             swObj.getBuffer().setLength(0); //flushing
    >         }
    >         try {
    >             int intBegin = strMsg.indexOf(")") ;
    > 
    >             strMsg =  strMsg.substring(intBegin+3,strMsg.indexOf
    > (")",intBegin+1)+1);
    >             if(strMsg != null)
    >                 strMsg = " "+ strMsg.trim();
    >             
    > objLogger.log(Level.toLevel(strLevel),(objMessage+strMsg).trim
    > ());
    >         }catch(StringIndexOutOfBoundsException
    > objStringIndexOutOfBoundsException) {
    >             objLogger.log(Level.toLevel(strLevel),objMessage);
    >         }
    >     }
    > 
    > 
    > 
    > 
    > 
    > -----------------------------------------
    > Amandeep Midha (CHARMIE)
    > IT Solutions (India) Pvt. Ltd.
    > No. 17, South End Road,
    > Basavangudi
    > Bangalore - 560 004. India
    > TEL : 91-80-6657180 EXT: 2116
    > CELL: +91 9886148227
    > FAX: 91-80-6655755
    > -----------------------------------------
    > "Never Fear Shadows ;They only mean there is a light shining 
    > somewhere nearby ...."
    > 
    > 
    >                                                               
    >                                                       
    >                     "Preetham                                 
    >                                                       
    >                     Kajekar"             To:     
    > <lo...@jakarta.apache.org>                                    
    >                     <preetham@cisc       cc:                  
    >                                                       
    >                     o.com>               Subject:     Getting 
    > location and line number of log messages              
    >                                                               
    >                                                       
    >                     05/26/2003                                
    >                                                       
    >                     04:20 PM                                  
    >                                                       
    >                     Please respond                            
    >                                                       
    >                     to "Log4J                                 
    >                                                       
    >                     Users List"                               
    >                                                       
    >                                                               
    >                                                       
    > 
    > 
    > 
    > 
    > Hi,
    >    I am interested in using the %l and %L option of log4j to 
    > get the location information and the line number of logged messages.
    >   We have a wrapped arround log4j to use the log4j API's and 
    > consequently all messages logged have the line number and the 
    > location of the wrapper class. Is there a way I can get log4j 
    > to get this information from the actual file which logged it 
    > ?(Perhaps go one step deeper in to the current threads stack trace)
    > 
    > 
    > Thanks,
    >  Cheers
    > 
    > ~preetham
    > 
    > ---------------------------------------------------------------------
    > To unsubscribe, e-mail: log4j-user-unsubscribe@jakarta.apache.org
    > For additional commands, e-mail: log4j-user-help@jakarta.apache.org
    > 
    > 
    > 
    > 
    > -----------------------Disclaimer------------------------
    > 
    > The views of the author may not necessarily reflect those
    > of the Company. All liability is excluded to the extent 
    > permitted by law for any claims arising as a result of the 
    > use of this medium to transmit information by or to IT 
    > Solutions (India) Pvt. Ltd.
    > 
    > We have taken precautions to minimize the risk of
    > transmitting software viruses, but we advise you to
    > carry out your own virus checks on any attachment to
    > this message.  We cannot accept liability for any loss or 
    > damage caused by software viruses.
    > 
    > ------------------------Disclaimer------------------------
    > 
    > 
    > ---------------------------------------------------------------------
    > To unsubscribe, e-mail: log4j-user-unsubscribe@jakarta.apache.org
    > For additional commands, e-mail: log4j-user-help@jakarta.apache.org
    > 
    
    ---------------------------------------------------------------------
    To unsubscribe, e-mail: log4j-user-unsubscribe@jakarta.apache.org
    For additional commands, e-mail: log4j-user-help@jakarta.apache.org
    
    
    

Re: log4J and Wrapper

Posted by Tom Maccariella <tm...@yahoo.com>.
See Jakarta Commons Logger:
http://jakarta.apache.org/commons/logging.html

Tom

Vivek Kapadekar <vk...@bitfone.com> wrote:
hi All
We want to write our own wrapper around log4J, a very simplistic one.
Is there one already written by some one, which is advisable to use?
Also with the wrapper, the log message, show the message originating
from the wrapper instead of the Source class? Is forcedLog() with FQCN
the only way to get around it ?

Thanks
--Viv



I discovered that specifying the FCQN of the wrapper class will do the
trick. If you look at code in LocationInfo, it basically picks up the
line in the stack trace appearing before the FCQN of the logger class,
which by default will be set to the Category which logs the message. 

So instead of logging message in you Wrapper like 

MyLogWrapper {
debug(String mssg) 
{
_logger.debug(mssg);
}
}

Try logging it using
MyLogWrapper {
debug(String mssg) 
{
_logger.log(_fcqn, Priority.DEBUG, mssg, null);
}
}

This will ensure the correct location info is printed.


~preetham




> -----Original Message-----
> From: Amandeep Midha [mailto:Amandeep.Midha@itsprojects.com] 
> Sent: Monday, May 26, 2003 3:26 PM
> To: Log4J Users List
> Subject: Re: Getting location and line number of log messages
> 
> 
> 
> Hi Preetham,
> 
> I am using the following context capturing code in 
> wrapper....hope this is useful for you!
> 
> best regards,
> Amandeep
> 
> 
> LogMgrImpl()//String name)
> {
> objLogger = Logger.getRootLogger();
> swObj = new StringWriter();
> pwObj = new PrintWriter(swObj);
> }
> 
> /**
> * Takes the priority level and message to log the information.
> * It also logs the context from where the message is 
> logged like class name,method name with line no
> * @param level - Priority Level ( as String )
> * @param message - Log information
> */
> public void logContext(String strLevel , Object objMessage) {
> if(!isLogEnabled(strLevel))
> return;
> String strMsg;
> synchronized(swObj) {
> Throwable objThrowable = new Throwable();
> objThrowable .printStackTrace(pwObj);
> strMsg = swObj.toString();
> swObj.getBuffer().setLength(0); //flushing
> }
> try {
> int intBegin = strMsg.indexOf(")") ;
> 
> strMsg = strMsg.substring(intBegin+3,strMsg.indexOf
> (")",intBegin+1)+1);
> if(strMsg != null)
> strMsg = " "+ strMsg.trim();
> 
> objLogger.log(Level.toLevel(strLevel),(objMessage+strMsg).trim
> ());
> }catch(StringIndexOutOfBoundsException
> objStringIndexOutOfBoundsException) {
> objLogger.log(Level.toLevel(strLevel),objMessage);
> }
> }
> 
> 
> 
> 
> 
> -----------------------------------------
> Amandeep Midha (CHARMIE)
> IT Solutions (India) Pvt. Ltd.
> No. 17, South End Road,
> Basavangudi
> Bangalore - 560 004. India
> TEL : 91-80-6657180 EXT: 2116
> CELL: +91 9886148227
> FAX: 91-80-6655755
> -----------------------------------------
> "Never Fear Shadows ;They only mean there is a light shining 
> somewhere nearby ...."
> 
> 
> 
> 
> "Preetham 
> 
> Kajekar" To: 
> 
> 
> 
> o.com> Subject: Getting 
> location and line number of log messages 
> 
> 
> 05/26/2003 
> 
> 04:20 PM 
> 
> Please respond 
> 
> to "Log4J 
> 
> Users List" 
> 
> 
> 
> 
> 
> 
> 
> Hi,
> I am interested in using the %l and %L option of log4j to 
> get the location information and the line number of logged messages.
> We have a wrapped arround log4j to use the log4j API's and 
> consequently all messages logged have the line number and the 
> location of the wrapper class. Is there a way I can get log4j 
> to get this information from the actual file which logged it 
> ?(Perhaps go one step deeper in to the current threads stack trace)
> 
> 
> Thanks,
> Cheers
> 
> ~preetham
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: log4j-user-help@jakarta.apache.org
> 
> 
> 
> 
> -----------------------Disclaimer------------------------
> 
> The views of the author may not necessarily reflect those
> of the Company. All liability is excluded to the extent 
> permitted by law for any claims arising as a result of the 
> use of this medium to transmit information by or to IT 
> Solutions (India) Pvt. Ltd.
> 
> We have taken precautions to minimize the risk of
> transmitting software viruses, but we advise you to
> carry out your own virus checks on any attachment to
> this message. We cannot accept liability for any loss or 
> damage caused by software viruses.
> 
> ------------------------Disclaimer------------------------
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: log4j-user-help@jakarta.apache.org
> 

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





Thanks,
Tom
 
Tom Maccariella
tmacc_nj@yahoo.com


---------------------------------
Do you Yahoo!?
Free online calendar with sync to Outlook(TM).