您必须扩展 MemoryHandler 以提供自定义推送行为。您可以通过将推送级别设置为ALL
覆盖推送方法或将推送级别设置为OFF
然后手动从发布方法发出推送来执行此操作。
如果您只想在看到许多日志记录后才开始记录,那么您需要创建如下内容:
public class PopoffHandler extends MemoryHandler {
private long count;
private final long size;
public PopoffHandler(Handler target, int size) {
super(target, size, Level.ALL);
this.size = size;
}
@Override
public synchronized void push() {
if (count == size) {
super.push();
} else {
++count;
}
}
@Override
public void setPushLevel(Level newLevel) {
if (newLevel == null) {
throw new NullPointerException();
}
super.setPushLevel(Level.ALL);
}
}
如果您想在组中记录记录,那么您需要执行以下操作:
public class ChunkedHandler extends MemoryHandler {
private long count;
private final long size;
public ChunkedHandler(Handler target, int size) {
super(target, size, Level.OFF);
this.size = size;
}
@Override
public synchronized void publish(LogRecord record) {
super.publish(record);
if (++count % size == 0L) {
super.push();
}
}
@Override
public void setPushLevel(Level newLevel) {
if (newLevel == null) {
throw new NullPointerException();
}
super.setPushLevel(Level.OFF);
}
}