[
https://issues.apache.org/jira/browse/LOG4J2-1919?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16023977#comment-16023977
]
Xianyin Xin commented on LOG4J2-1919:
-------------------------------------
Thanks [[email protected]]. I agree with what you said, and the test
result is coincide with your comment. But I wonder if it is reasonable when you
exec {{org.apache.logging.log4j.core.Logger.get()}}, you get the common
LoggerConfig that many loggers use, as the implementation of this method is
{code}
public LoggerConfig get() {
return privateConfig.loggerConfig;
}
{code}
which indicates this is the private config of the logger.
Background: what i want is a method to create/modify the logger private config
programatically, but i didn't find that at the first time. The above method to
"get the private config" is indeed misleading.
> 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)