简体   繁体   English

log4j2通过代码添加记录器+附加器

[英]log4j2 adding logger + appender by code

I am trying to add by code a custom appender that should log some package. 我试图通过代码添加应该记录一些程序包的自定义附加程序。 And everything works using this code : 使用此代码,一切都可以正常工作:

String loggerName = "org.test";
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration cfg = ctx.getConfiguration();
if (cfg instanceof XmlConfiguration) {
       //add appender if not added
       Appender appender = cfg.getAppender(MyAppender.NAME);
if (appender == null) {
            Appender appender = MyAppender.createAppender(MyAppender.NAME, "%highlight{%d [%t] %-5level: %msg%n%throwable}", "false", null);
            appender.start();
            cfg.addAppender(appender); 
}
LoggerConfig logger = ((XmlConfiguration) cfg).getLogger(loggerName);
            if (logger == null) {
                logger = new LoggerConfig(loggerName, Level.DEBUG, true);
                cfg.addLogger(loggerName, logger);
            }
           logger.addAppender(appender, Level.DEBUG, null);
} //closing the instanceof XMLConfiguration part

So in short.. as you can see I am creating an appender if there was no appender defined before. 简而言之..如您所见,如果之前没有定义附加器,我将创建一个附加器。 Then I am creating a logger for org.test if there was no added and adding the appender to this logger. 然后,如果没有添加org.test,我将为其创建一个记录器,并将追加器添加到该记录器中。

Everything is fine except one thing: The new appender and logger works correctly. 除了一件事,一切都很好:新的附加器和记录器可以正常工作。 However I have other appenders in my XML configuration Console Appender and so on.. and for some reason they are ALSO added as DEBUG level for the org.test logger... even if this logger doesnt have ANY other appenders configured like in my case.. I am debuging .. it has ONLY one appender, in the configuration object.. but the logger still sends debug messages to ALL appends that I have... (I guess all that I have on ROOT level which were set to INFO) ... from now on are also showing DEBUG messages from org.test ... how I can remove this appends and use only this one.. :( 但是我在XML配置控制台Appender中还有其他附加器,依此类推..由于某种原因,它们也被添加为org.test记录器的DEBUG级别...即使该记录器没有像我这样配置的任何其他附加器..我正在调试..它在配置对象中只有一个追加程序..但记录器仍将调试消息发送到我拥有的所有追加...(我想我在ROOT级别上拥有的所有信息都已设置为INFO )...从现在开始还显示来自org.test的DEBUG消息...我如何删除此附加并仅使用此附加.. :(

Thanks. 谢谢。

This is happening because the additivity property of the logger is true by default. 之所以发生这种情况,是因为默认情况下记录器的additivity属性为true When this property is true the log messages are propagated to the parent loggers, in your case the ROOT one, I think. 当此属性为true时 ,日志消息将传播到父记录器,您认为是ROOT记录器。 Set the additivity property in the logger to false and you will get the desired output. 将记录器中的additivity属性设置为false ,您将获得所需的输出。 More details here http://logging.apache.org/log4j/2.x/manual/configuration.html#Additivity . 更多详细信息,请参见http://logging.apache.org/log4j/2.x/manual/configuration.html#Additivity

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

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