简体   繁体   English

即使将 Additivity 设置为 false,log4j ConsoleAppender 也会打印到控制台两次

[英]log4j ConsoleAppender printing to console twice even after setting Additivity to false

I have log4j appenders that print outputs to the console once when running the program in intellIj我有 log4j 附加程序,在 intellIj 中运行程序时将输出打印到控制台一次在此处输入图像描述

but twice when running the program via executable.jar.但是通过executable.jar运行程序时两次

在此处输入图像描述

The actual logs that get written to the log file only appear once which is correct.写入日志文件的实际日志只出现一次,这是正确的。

在此处输入图像描述

I define my logger/appenders programmatically in this code block我在这个代码块中以编程方式定义了我的记录器/附加器

and after everything is done I call一切都完成后,我打电话给

rootLogger.setAdditivity(false);

but for some reason my classes still output twice to cmdline console when running as a.jar.但由于某种原因,当我作为 a.jar 运行时,我的课程仍然是 output 两次到命令行控制台。 I call loggers in each class by defining a log variable at the top like so:我通过在顶部定义一个日志变量来调用每个 class 中的记录器,如下所示:

public class SomeClass {

    static Logger log = Logger.getLogger(SomeClass .class);

and this log variable is used everywhere in the "SomeClass" class.并且此日志变量在“SomeClass”class 中随处使用。

I've tried setting additivity to false for all the package loggers as well before and after I set their levels...我已经尝试将所有 package 记录器的可加性设置为 false 以及在我设置它们的级别之前和之后......

Logger.getLogger("com.company").setAdditivity(false);
Logger.getLogger("com.company.project.database.Admin").setAdditivity(false);
...

but when I run the code that way it complains it cannot find the appenders for the class that I'm defining all the log4j stuff in, and nothing gets written to output/log.但是当我以这种方式运行代码时,它抱怨它找不到 class 的附加程序,我正在定义所有 log4j 内容,并且没有任何内容写入输出/日志。

I think you are on the right track with additivity, however additivity is a property of a child, telling log4j not to use the parents: https://logging.apache.org/log4net/release/sdk/html/P_log4net_Repository_Hierarchy_Logger_Additivity.htm You have set additivity to false for the ultimate parent logger, that will never do anything as root will not be a child of anything. I think you are on the right track with additivity, however additivity is a property of a child, telling log4j not to use the parents: https://logging.apache.org/log4net/release/sdk/html/P_log4net_Repository_Hierarchy_Logger_Additivity.htm You已将最终父记录器的可加性设置为 false ,因为 root 不会是任何东西的孩子,所以它永远不会做任何事情。

Try setting the additivity to false of each package logger and hopefully that will solve the issue.尝试将每个 package 记录器的可加性设置为 false,希望这能解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM