在java中重复记录log 4j

发布于 2024-07-15 05:51:50 字数 717 浏览 2 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

塔塔猫 2024-07-22 05:51:50

我也经历过类似的行为,结果发现 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?

风筝在阴天搁浅。 2024-07-22 05:51:50

您很可能有多个 Appender。 请参阅 log4j 手册(部分:附加程序和布局):

给定记录器的每个启用的日志记录请求都将转发到该记录器中的所有附加程序以及层次结构中更高的附加程序。

您可以尝试将可加性标志设置为 false。

Most probably you have more than one Appenders. See the log4j manual (Section: Appenders and Layouts):

Each enabled logging request for a given logger will be forwarded to all the appenders in that logger as well as the appenders higher in the hierarchy.

You can try setting the additivity flag to false.

千鲤 2024-07-22 05:51:50

好吧,你还没有展示你的程序是如何执行的。 这是一个完整的程序,仅显示一行:

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");
    }
}

您的代码是否可能真的运行了三次?

Well, you haven't shown how your program is executing. Here's a complete program which only shows a single line:

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");
    }
}

Is it possible that your code really is running three times?

清音悠歌 2024-07-22 05:51:50

就我而言,每次调用 BasicConfigurator.configure() 时,我都会添加一个新的附加程序。

我通过重置配置解决了这个问题:

BasicConfigurator.resetConfiguration() //reset first
BasicConfigurator.configure() // then configure

我必须承认我不清楚发生了什么,但它确实解决了我的问题。

In my case, I was adding a new appender each time I called the BasicConfigurator.configure().

I solved it reseting the configuration:

BasicConfigurator.resetConfiguration() //reset first
BasicConfigurator.configure() // then configure

I must confess that I don't clearly understand what's going on, but it surely solved my problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文