我在一个java程序中使用了log4j。我将其初始化为:
BasicConfigurator.configure(); // logger configuration
try {
logger.setLevel(Level.DEBUG);
} catch (Exception e) {
System.out.println("Logfile not found");
}但是,在程序执行过程中,我得到了3条log语句,而不是1条。例如,
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进行额外的配置?
发布于 2009-03-07 10:37:33
我也经历过类似的行为,结果发现不止一次配置了Log4J;使用的是BasicConfigurator,还使用了一个我已经忘记的log4j.xml文件。会不会是类路径上的某个地方有额外的Log4J配置?
发布于 2009-03-07 10:59:00
最有可能的情况是,你有多个Appenders。请参阅log4j manual (部分:附加程序和布局):
为给定记录器启用的每个日志记录请求都将被转发到该记录器中的所有附加器以及层次结构中更高的附加器。
您可以尝试将加性标志设置为false。
发布于 2009-03-07 10:28:46
好吧,你还没有展示你的程序是如何执行的。下面是一个完整的程序,它只显示一行:
import org.apache.log4j.*;
public class Test
{
public static void main(String[] args)
{
BasicConfigurator.configure(); // logger configuration
Logger logger = Logger.getLogger(Test.class);
logger.setLevel(Level.DEBUG);
logger.info("Hello");
}
}有没有可能你的代码真的运行了三次?
https://stackoverflow.com/questions/621633
复制相似问题