|
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: 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 AlgorithmTo 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:
Therefore the accordingly template is:
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()" Only the ${message} is kept. Everything else will be replaced with the computed values. The result would be:
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:
Therefore the accordingly template is:
In this case, assuming that ${delimiter}=" - ", the mapping would be:
${enclosing_method}="otherMethod() : starttext" 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:
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:
Therefore the accordingly template is:
In this case, assuming that ${delimiter}=" - ", the mapping would be:
${enclosing_method}="starttext" 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:
Abstract: The modification task is best used with Log4E generated log statements! |
||
|
|