Search code examples
javalogginglog4japache-commons

Implementing logger for java application


I am trying to implement logger using apache commonn logging and log4j.xml file as configuration file .

So in the actual java code i am writing log as

I am using appache common logging

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

and i am logging information such as in . I am creating a instance of log for each class

 private static Log    logger  = LogFactory.getLog( MyClass.class );
    private static final String      name   ="SAM";
    logger.info("name= "+name);
  1. So now my question is does implementing logger like this create a performanace issue?

  2. Is it necessary to check log like

    if ( log.isInfoEnabled( ) ) 
     {
      log.info( "Info message" );
      }
    

The confusion is mainly because in Apache common logging they have mentioned to do this and in log4j they have mentioned it is unnecessary.

Each time when we write log in the files?

-Sam


Solution

    1. yes and no, logging always reduces your performance, but some functions are more expensive then others, e.g. getting the calling class/method-Name uses reflection and is very slow. But a normal logfuntion is not that expensive if you do not have an expensive statement in the calling logging function (this will be evaluated every time before the log-Level is checked. In this case you can use the .isLevelEnabledcheck to prevent the evaluation). Also logging to the console takes longer for the output than logging to a file. You will find more information about this by googling and in the FAQ/manual of log4j.

    2. You do not have to check the Log-Level before logging. This is done within the log-function itself. Therefore are the different methods for every Level or the Level-Argument in the generic log-Method.