[
https://issues.apache.org/jira/browse/LOG4J2-1919?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16024176#comment-16024176
]
Ralph Goers commented on LOG4J2-1919:
-------------------------------------
The privateConfig.loggerConfig points to the LoggerConfig that is to be used by
the Logger. Whether it is shared with other Loggers can only be determined by
seeing if the LoggerConfig's name matches the Logger's name. If it does not
then you should expect that it is shared. There is nothing wrong with this
behavior - it is exactly what is required.
http://logging.apache.org/log4j/2.x/log4j-core/apidocs/index.html. The javadoc
at
http://logging.apache.org/log4j/2.x/log4j-core/apidocs/org/apache/logging/log4j/core/config/Configuration.html#getLoggerConfig-java.lang.String-
tells you exactly what it does.
http://logging.apache.org/log4j/2.x/manual/customconfig.html#AddingToCurrent
has most of what you need. The part you are missing is that when you make the
call to getLogger you need to check the LoggerConfig to determine if its name
is the same as the Logger you are interested in. If it doesn't match you need
to create a new LoggerConfig to match the Logger and attach the Appender to it.
> Change the appender of a logger will affect other loggers
> ---------------------------------------------------------
>
> Key: LOG4J2-1919
> URL: https://issues.apache.org/jira/browse/LOG4J2-1919
> Project: Log4j 2
> Issue Type: Bug
> Reporter: Xianyin Xin
>
> {code}
> String logger1Name = Test.class.getName();
> String logger2Name = "fileLogger";
> Logger logger1 = LogManager.getLogger(logger1Name);
> Logger logger2 = LogManager.getLogger(logger2Name);
> Writer writer = null;
> try {
> writer = new PrintWriter(new FileOutputStream("D:/file.log"));
> } catch (FileNotFoundException e) {
> e.printStackTrace();
> }
> String fileAppender = "myFileAppender";
> LoggerContext loggerContext = (LoggerContext)
> LogManager.getContext(false);
> Configuration conf = loggerContext.getConfiguration();
> final PatternLayout layout = PatternLayout.createDefaultLayout(conf);
> final Appender appender = WriterAppender
> .createAppender(layout, null, writer, fileAppender, false, true);
> appender.start();
> // CHANGE ONLY the appender for logger2, but logger1 will be affected
> also:
> Map<String, Appender> appenderMap = conf.getAppenders();
> LoggerConfig config = conf.getLoggerConfig(logger2Name);
> for (Map.Entry<String, Appender> entry : appenderMap.entrySet()) {
> config.removeAppender(entry.getKey());
> }
> config.addAppender(appender, null, null);
> logger1.info("hahaha");
> logger2.info("hehehe");
> {code}
> The output in "D:file.log" reads,
> {quote}
> hahaha
> hehehe
> {quote}
> But the stdout appender for logger1 ouputs nothing.
> I don't know if it is the design. However, the LoggerConfig returned by
> {{conf.getLoggerConfig(logger_name)}} is in fact the {{PrivateConfig}} of
> logger2, which can be seen from {{Logger.removeAppender()}} and
> {{Logger.addAppender()}}.
--
This message was sent by Atlassian JIRA
(v6.3.15#6346)