Recently I was given job of making error manager, which is like putting your feet on the axe. Whenever an error is thrown it should be logged in jira, wow perhaps now you would have realized why the axe stuff. No escapes, had to do and it helps though at times ;-).
Now issue being how to go about it, oops into rescue as usual. I extended one of the log4j's class and added customization in the "
public void append(LoggingEvent event)"
which helped the cause.
In a gist,
public class CustomLog4j extends DailyRollingFileAppender {
private static final String COMMA = ",";
private static final String ERROR = "ERROR";
private String level;
@Override
public void append(LoggingEvent event) {
super.append(event);
String[] stackTrace = null;
this.level = event.getLevel().toString();
if (level.equals(ERROR)) {
if (event != null) {
LocationInfo locationInfo
= event.getLocationInformation();
if (shouldContinueForThisClass(
locationInfo.getClassName())){
/*Custom code*/
}
}
}
}
public void setLevel(String level) {
this.level = level;
}
public String getLevel() {
return level;
}
public boolean shouldContinueForThisClass(String clazz) {
String[] clazzez = Props.getErrorProperties().getProperty(
ErrorConstants.CLASS_FILTER).split(COMMA);
for (String filter : clazzez) {
if (clazz.indexOf(filter) != -1) {
return false;
}
}
return true;
}
}
shouldContinueForThisClass method is there to avoid some classes error being registered,I have a properties file where I define class names/package keywords which I fetch by
String[] clazzez = Props.getErrorProperties().getProperty(
ErrorConstants.CLASS_FILTER).split(COMMA);
this is also to avoid Error Manager going in loops, in case my CustomLog4j crashes :(
So this way I achieved the task, the moment any error happens, it gets logged in jira and mail comes to the person assigned, most of the time its me :(
Labels: custom log4j, java, log4j