避免丢弃 MemoryHandler 中的旧条目
当记录到 MemoryHandler 时,当 numofentries > 时,MemoryHandler 会删除旧条目。尺寸。
我想避免这种行为,或者至少在日志中标记旧条目被抑制。
小测试用例:
import java.util.logging.*;
public class SSCE01 {
public static void main (String [] args) {
Logger rootLogger = Logger.getLogger("");
rootLogger.removeHandler(rootLogger.getHandlers()[0]); //remove default Console Handler
ConsoleHandler ch = new ConsoleHandler();
Logger l = Logger.getLogger("test");
MemoryHandler mh = new MemoryHandler(ch,3,Level.OFF);
l.addHandler(mh);
l.severe("this shouldnt be logged");
l.severe("this shouldnt be logged");
l.severe("this shouldnt be logged");
l.severe("this should be logged");
l.severe("this should be logged");
l.severe("this should be logged");
mh.push();
}
}
When logging to a MemoryHandler, the MemoryHandler removes older entries when the numofentries > size.
I want to avoid this behavior, or at least mark down to the log that older entries are suppressed.
Little test case:
import java.util.logging.*;
public class SSCE01 {
public static void main (String [] args) {
Logger rootLogger = Logger.getLogger("");
rootLogger.removeHandler(rootLogger.getHandlers()[0]); //remove default Console Handler
ConsoleHandler ch = new ConsoleHandler();
Logger l = Logger.getLogger("test");
MemoryHandler mh = new MemoryHandler(ch,3,Level.OFF);
l.addHandler(mh);
l.severe("this shouldnt be logged");
l.severe("this shouldnt be logged");
l.severe("this shouldnt be logged");
l.severe("this should be logged");
l.severe("this should be logged");
l.severe("this should be logged");
mh.push();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个坏主意,你会在记忆机器中充满痕迹。尽管如此,
MemoryHandle
是一个循环缓冲区,因此当它被填满时,其他条目将被删除。如果您希望不删除条目,只需使用 Integer.MAX_VALUE 作为大小来构造它 - 这又是一个坏主意。这会影响你的应用程序性能,人们往往会避免这种情况。考虑使用将跟踪转储到辅助存储中的处理程序,添加时间戳;并使用那里的痕迹构建您需要的任何逻辑。
编辑
根据您报告的代码,您可以将日志记录功能封装在另一个类中,该类记录
MemoryHandler
中的条目数。类似于:不使用 Java API 的日志记录调用,而是直接使用
MyMemoryConsoleHandler
,以便您可以控制推送到控制台的内容。请注意
同步
方法,如果您有多线程应用程序,则需要这样做。否则你可能会遇到竞争条件。This is a bad idea, you'll fill the memory machine with traces. Despite this,
MemoryHandle
is a circular buffer so when it gets filled other entries will be removed. If you want entries to not be removed just construct it withInteger.MAX_VALUE
as size - again this a bad idea. This will collide with your app performance, and people tend to avoid that.Consider using a handler that dumps traces into secondary storage, add a timestamp; and build whatever logic you need using the traces from there.
Edit
From the code you reported, you could encapsulate your logging functionality in another class that records the number of entries in
MemoryHandler
. Something like:Instead of using Java API's logging calls directly use your
MyMemoryConsoleHandler
so that you have control over what is pushed to the console.Pay attention to
synchronized
methods, this is needed in case you have a multi-threaded application. Otherwise you could end up with race conditions.