Logger Modification

Already exisiting loggers can be modified by Log4E.

Note that the modification algorithm changed from v0.5.5 to the current version and that using this task could delete a logger message under certain circumstances! See the modification algorithm section for more information.

Several operation are executed:

  • Surround isLevelEnabled() statements
  • Insert info about current method
  • Move invocation in return statement in front of logger
  • Example:

    Before:

    public class ActualClass {
    	//typical copy/paste mistake
    	/**
    	 * Logger for this class
    	 */
    	private static final Logger logger = Logger.getLogger(Other.class);
    	
    	public String myMethod(String str, int integer) {
    		logger.debug("otherMethod() - starttext");
    		try {
    			
    		} catch (Exception e) {
    			logger.error("otherMethod() - errortext", e);
    			
    			//Your code...
    			
    			logger.debug("otherMethod() - othertext");
    			
    			logger.debug("otherMethod() - returntext");
    			return "text";
    		}
    		logger.debug("otherMethod() - endtext");
    		return toString();
    	}
    }
    

    After:

    public class ActualClass {
    	//typical copy/paste mistake
    	/**
    	 * Logger for this class
    	 */
    	private static final Logger logger = Logger.getLogger(ActualClass.class);
    	
    	public String myMethod(String str, int integer) {
    		if (logger.isDebugEnabled()) {
    			logger.debug(
    				"myMethod(String str = "
    					+ str
    					+ ", int integer = "
    					+ integer
    					+ ") - starttext");
    		}
    		try {
    			
    		} catch (Exception e) {
    			logger.error("myMethod() - errortext", e);
    			
    			//Your code...
    			
    			if (logger.isDebugEnabled()) {
    				logger.debug("myMethod() - othertext");
    			}
    			
    			if (logger.isDebugEnabled()) {
    				logger.debug("myMethod() - returntext - return value = text");
    			}
    			return "text";
    		}
    	
    		String returnString = toString();
    		if (logger.isDebugEnabled()) {
    			logger.debug("myMethod() - endtext - return value = " + returnString);
    		}
    		return returnString;
    	}
    }
    

    Note that Log4E checks if "returnString" already exists and appends a number. e.g. if "returnString" exists the new variable would be "returnString2" (and so on).

    Modification Algorithm

    To analyse the existing log statement in source code, Log4E uses the accordingly template (defined in "Log4E > Profile > Templates" preference page)

    Example 1:

    Assume the existing log statement is:

      debug("otherMethod() - starttext");

    Therefore the accordingly template is:

      debug("${enclosing_method}${delimiter}${message}${delimiter}${return_value}")

    Log4E makes a guess to map the tokens to the right variables. In this case, assuming that ${delimiter}=" - ", everything is alright. The mapping would be:

    ${enclosing_method}="otherMethod()"
    ${message}="starttext"
    ${return_value}=""

    Only the ${message} is kept. Everything else will be replaced with the computed values. The result would be:

      debug("myMethod() - starttext");

    To compute the right tokens Log4E uses the delimiter. If the delimiter, defined in the "Log4E > Format" preferences, is different to delimiter used in the source code, Log4E won't be able to extract the user message.

    Example 2:

    Assume the existing log statement is:

      debug("otherMethod() : starttext");

    Therefore the accordingly template is:

      debug("${enclosing_method}${delimiter}${message}${delimiter}${return_value}")

    In this case, assuming that ${delimiter}=" - ", the mapping would be:

    ${enclosing_method}="otherMethod() : starttext"
    ${message}=""
    ${return_value}=""

    When Log4E recognizes that the ${message} is empty it will pop up a warning. The user can choose from there if he or she wants to start the Preview Wizard before confirming the changes.

    If confirmed by user the result would be:

      debug("myMethod()");

    Note that the logger statement "error("otherMethod()", e)" generated by Log4E doesn't have a message by default. Therefore Log4E will pop up a warning when performing modification even if the message must be empty in this case.

    Example 3:

    Assume the existing log statement is:

      debug("starttext - otherMethod()");

    Therefore the accordingly template is:

      debug("${enclosing_method}${delimiter}${message}${delimiter}${return_value}")

    In this case, assuming that ${delimiter}=" - ", the mapping would be:

    ${enclosing_method}="starttext"
    ${message}="otherMethod()"
    ${return_value}=""

    This is the worst case. Log4E does not recognize that the mapping is wrong because ${message} is not empty. Log4E would delete the exisitng message without a warning:

      debug("myMethod() - otherMethod()");

    Abstract:

    The modification task is best used with Log4E generated log statements!



    http://log4e.jayefem.de