[ 
https://issues.apache.org/jira/browse/LOG4J2-3295?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17467462#comment-17467462
 ] 

Ralph Goers commented on LOG4J2-3295:
-------------------------------------

Mike, It occurs to me that the reason things work for you with Log4j 1 is that 
during reconfiguration Log4j 1 first removes all configuration from loggers and 
then stops the appenders. Once that is done it starts building the new 
configuration. Of course, during this time anything being logged is lost since 
the loggers are no longer configured to do anything. While you can't get Log4j 
2 to do that, you can do something similar. If you reconfigure Log4j to use the 
default configuration (writing errors to the console) that will cause it to 
shutdown the configuration and close the file(s). You can then reconfigure with 
the new configuration and everything will work. If you do this any errors that 
might happen during reconfiguration would be routed to the console while all 
other events are ignored, which is a slightly better experience than you would 
get with Log4j 1 but accomplishes what you are looking for.

> Reconfiguration failure with an error
> -------------------------------------
>
>                 Key: LOG4J2-3295
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-3295
>             Project: Log4j 2
>          Issue Type: Bug
>          Components: Configuration, Configurators
>    Affects Versions: 2.17.0
>            Reporter: Mike Li
>            Priority: Blocker
>
> Steps to recreate:
>  # start with log4j2.xml with following content:
> <?xml version="1.0" encoding="UTF-8"?>
> <Configuration status="DEBUG" monitorInterval="30">
>  
>     <Appenders>
>     <File name="FILE" fileName="c:\temp\product.log">
>         <PatternLayout>
>             <Pattern>ProductName:%-10p %d\{yyyy-MM-dd HH:mm:ss.SSS}
> {GMT} context:%x [%t] - %-50m - %M(%F:%L)%n</Pattern>
>         </PatternLayout>
>         
>     </File>
>     </Appenders>
>     <Loggers>
>         <Root level="INFO">
>             <AppenderRef ref="FILE"/>
>         </Root>
>     </Loggers>
> </Configuration>
>  
> 2. After the application is started and running, edit the log4j2.xml file 
> with the following content, notice that the fileName in the WRAP appender is 
> the same as that in the original FILE appender.
>  
> <?xml version="1.0" encoding="UTF-8"?>
> <Configuration status="DEBUG" monitorInterval="30" >
>     <Appenders>
>     <Async name="ASYNC" includeLocation="true" bufferSize="4096">
>         <AppenderRef ref="WRAP"/>
>     </Async>
>     <RollingFile name="WRAP" fileName="c:\temp\product.log" 
> filePattern="c:\temp\product.log.%i">
>         <PatternLayout pattern="ProductName:%-7p %d\{yyyy-MM-dd 
> HH:mm:ss.SSS}{GMT}
> context:%x [%t] - %-50m - %M(%F:%L)%n"/>
>         <Policies>
>             <SizeBasedTriggeringPolicy size="1048576"/>
>         </Policies>
>         <DefaultRolloverStrategy max="4"/>
>     </RollingFile>
>     </Appenders>
>     <Loggers>
>         <Root level="DEBUG">
>             <AppenderRef ref="ASYNC"/>
>         </Root>
>     </Loggers>
> </Configuration>
>  
> 3. Wait for the log4j to detect the change in 30 seconds to reconfigure. When 
> the reconfigure starts, it reports an error. Here is the exception stack:
>  
> 2021-12-28 17:42:41,688 Log4j2-TF-1-ConfiguratonFileWatcher-4 ERROR Could not 
> create plugin of type class 
> org.apache.logging.log4j.core.appender.RollingFileAppender for element 
> RollingFile org.apache.logging.log4j.core.config.ConfigurationException: 
> Configuration has multiple incompatible Appenders pointing to the same 
> resource 'c:\temp\product.log'
>     at 
> org.apache.logging.log4j.core.appender.AbstractManager.narrow(AbstractManager.java:169)
>     at 
> org.apache.logging.log4j.core.appender.rolling.RollingFileManager.getFileManager(RollingFileManager.java:217)
>     at 
> org.apache.logging.log4j.core.appender.RollingFileAppender$Builder.build(RollingFileAppender.java:146)
>     at 
> org.apache.logging.log4j.core.appender.RollingFileAppender$Builder.build(RollingFileAppender.java:62)
>     at 
> org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:122)
>     at 
> org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:1107)
>     at 
> org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1032)
>     at 
> org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1024)
>     at 
> org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:643)
>     at 
> org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:243)
>     at 
> org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:289)
>     at 
> org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:626)
>     at 
> org.apache.logging.log4j.core.LoggerContext.onChange(LoggerContext.java:762)
>     at 
> org.apache.logging.log4j.core.util.AbstractWatcher$ReconfigurationRunnable.run(AbstractWatcher.java:93)
>     at java.base/java.lang.Thread.run(Thread.java:831)



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to