Compile Your Patterns as You Need To
Make a point of compiling your Patterns on a just-in-time basis if there's a chance you won't need them. That is, don't compile a Pattern object until the time you actually need it, if you actually need it. This is especially true in a logically branching method, in which there might not be any need to even use a Pattern. Better yet, look for opportunities to conditionally compile your patterns.
For example, say you're parsing log entries in your log Handler, because you want to send out an emergency e-mail if any of the entries contain an Exception. Of course, you could compile the pattern (.*Exception) and check it against every String that's logged. However, this is probably overkill, because you don't, of course, expect any exceptions in your code. It's probably a better idea to simply do a String.indexOf for the substring Exception and only try the extraction process if the index is positive. This is demonstrated in Listing 4-9.
Listing 4-9: Log Handler That Checks for Exceptions
import java.util.logging.*;
import java.util.regex.*;
/**
* Listens for exceptions in the log entries
* @author M Habibi
*/
public class ExceptionHandler extends StreamHandler{
/**
* examines the record being logged, and checks to see if
* it contains an exception
* @param record the LogRecord about to be logged
*/
public void publish(LogRecord record){
//extract the message
String msg = record.getMessage();
//check to see if the message contains an exception
int exceptionIndex = msg.indexOf("Exception");
//if the message didn't contains the String
//'Exception', then don't bother compiling the regex
if (exceptionIndex > -1){
Pattern pattern =
Pattern.compile("(.*Exception.*)");
Matcher matcher = pattern.matcher(msg);
if (matcher != null && matcher.find()){
String err = "EXCEPTION FOUND " + matcher.group(1);
System.out.println(err);
//put emailer here
}
}
}
}
This isn't particularly clever code, but it does a good job of cultivating those suspects that require further investigation and those that can't possibly be candidates. The principle here, of course, is intelligent discrimination. If you're able to rule out impossible candidates, then you'll save time and resources in production. Listing 4-10 shows the test harness.
Listing 4-10: Adding the Log Handler to Your Log
import java.util.logging.*;
/**
* Demonstrates the usage of the log listener
*/
public class LogDemo{
public static void main(String args[]){
Logger log = Logger.global;
setLoggerhandler(log);
//test the code. Do even finest-grade entries get
//logged?
log.finest(new Exception().toString());
}
/**
* Sets a logger handler for the given log.
* @param log the logger that needs an listener
* @version 1.0 5/12/2002
*/
public static void setLoggerhandler(Logger logger){
Handler handler = new ExceptionHandler();
logger.addHandler(handler);
//set to handle all errors, so that it will examine
//all errors.
logger.setLevel(Level.ALL);
}}
| Note |
You'll notice that this code doesn't deal with the Logger.throwing method, and it doesn't actually send out an e-mail. Don't worry, code that does is provided at http://www.influxs.com. However, the implementation here would require explanations that are beyond the scope of regular expressions.
|