My project manager wanted to get rid of the timestamp in the logging, because the logging gets a timestamp in the table in the database system, where the logging, and the rest of the standard output, is redirected to. So I started googling, and soon I found that I had to write a Formatter of my own, and how to write it:
Handler handler = new ConsoleHandler();
handler.setFormatter(new Formatter() {
public String format(LogRecord record) {
StringBuilder sb = new StringBuilder();
sb.append("Hello World ");
sb.append(record.getLevel()).append(' ');
sb.append(record.getMessage()).append(' ');
sb.append(record.getLoggerName()).append(' ');
sb.append(record.getSourceMethodName());
sb.append("\n");
Throwable t = record.getThrown();
if (t != null) {
StringWriter sw = new StringWriter();
t.printStackTrace(new PrintWriter(sw));
sb.append(sw.toString());
}
return sb.toString();
}
});
I ran the code, I did not see ‘Hello World’, but the timestamp was still there, so my Formatter had no effect. Then I came across the LogManager class. I searched, and searched, and searched the class’ API, but I could not find any method the Handler with my new Formatter would fit in. Hmmmmmm. It would only fit into the Logger class’ addHandler() method. So I made a new Logger:
(meer…)