简体   繁体   中英

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在此处输入图像描述

but twice when running the program via 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. I call loggers in each class by defining a log variable at the top like so:

public class SomeClass {

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

and this log variable is used everywhere in the "SomeClass" class.

I've tried setting additivity to false for all the package loggers as well before and after I set their levels...

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.

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.

Try setting the additivity to false of each package logger and hopefully that will solve the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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