The answer is no, it is impossible .
The way you do it now is good (as soon as it syncs, of course).
There is an alternative: instead of the Logger extension, expand java.util.logging.Handler, and then:
- Override publish () method
- Inject flush () and close ()
Create a method to count messages by level
I tried this and tested it and it worked:
import java.util.ArrayList; import java.util.List; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.LogRecord; public class CountHandler extends Handler { List<LogRecord> records = new ArrayList<LogRecord>(); @Override public void publish(LogRecord record) { records.add(record); } public int howMany(Level level) { int howMany = 0; for(LogRecord record : records) { if (record.getLevel().intValue() == level.intValue()) { howMany++; } } return howMany; } @Override public void flush() { } @Override public void close() throws SecurityException { } }
The advantage of this is that you do not need to redefine the methods for each individual Level. The only thing you need to add this handler to each of your registrars:
CountHandler messageCounter= new CountHandler(); logger.addHandler(messageCounter);
So take it for what it costs. Whatever you do, you have to write your own class.
source share