在java中重复记录log 4j
我在java程序中使用了log4j。 我用 :
BasicConfigurator.configure(); // logger configuration
try {
logger.setLevel(Level.DEBUG);
} catch (Exception e) {
System.out.println("Logfile not found");
}
但是,在程序执行期间我得到 3 个日志语句而不是一个。 例如,
3 lines
1047 [main] INFO ibis.Preproc.Ratings - Added AS TIMEZONE to tZones[0] = GMT-12:00
1047 [main] INFO ibis.Preproc.Ratings - Added AS TIMEZONE to tZones[0] = GMT-12:00
1047 [main] INFO ibis.Preproc.Ratings - Added AS TIMEZONE to tZones[0] = GMT-12:00
而不是一行
1047 [main] INFO ibis.Preproc.Ratings - Added AS TIMEZONE to tZones[0] = GMT-12:00
是否需要对 log4j 进行任何额外的配置来避免这种情况?
I used log4j in a java program. I initialized it with :
BasicConfigurator.configure(); // logger configuration
try {
logger.setLevel(Level.DEBUG);
} catch (Exception e) {
System.out.println("Logfile not found");
}
But, during the execution of the program i get 3 log statements instead of one. For example,
3 lines
1047 [main] INFO ibis.Preproc.Ratings - Added AS TIMEZONE to tZones[0] = GMT-12:00
1047 [main] INFO ibis.Preproc.Ratings - Added AS TIMEZONE to tZones[0] = GMT-12:00
1047 [main] INFO ibis.Preproc.Ratings - Added AS TIMEZONE to tZones[0] = GMT-12:00
instead of one line
1047 [main] INFO ibis.Preproc.Ratings - Added AS TIMEZONE to tZones[0] = GMT-12:00
Are there any extra configurations to be done to log4j to avoid this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我也经历过类似的行为,结果发现 Log4J 被配置了不止一次; 使用 BasicConfigurator,还使用我忘记的 log4j.xml 文件。 难道类路径上的某个地方还有额外的 Log4J 配置吗?
I've experienced similar behavior, and it turned out that Log4J was configured more than once; using the BasicConfigurator, but also with a log4j.xml file I had forgotten about. Could it be that there's an additional Log4J configuration somewhere on the classpath?
您很可能有多个 Appender。 请参阅 log4j 手册(部分:附加程序和布局):
您可以尝试将可加性标志设置为 false。
Most probably you have more than one Appenders. See the log4j manual (Section: Appenders and Layouts):
You can try setting the additivity flag to false.
好吧,你还没有展示你的程序是如何执行的。 这是一个完整的程序,仅显示一行:
您的代码是否可能真的运行了三次?
Well, you haven't shown how your program is executing. Here's a complete program which only shows a single line:
Is it possible that your code really is running three times?
就我而言,每次调用 BasicConfigurator.configure() 时,我都会添加一个新的附加程序。
我通过重置配置解决了这个问题:
我必须承认我不清楚发生了什么,但它确实解决了我的问题。
In my case, I was adding a new appender each time I called the BasicConfigurator.configure().
I solved it reseting the configuration:
I must confess that I don't clearly understand what's going on, but it surely solved my problem.