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

Robert Haycock commented on LOG4J2-2068:
----------------------------------------

I don't know how you like your tests added so I'll just paste a quick and dirty 
one here...

{code}
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.AbstractLifeCycle;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.status.StatusData;
import org.apache.logging.log4j.status.StatusListener;
import org.apache.logging.log4j.status.StatusLogger;
import org.junit.Test;

public class MyTest extends AbstractLifeCycle {

    @Test
    public void test_Log4j2Reload() {

        File config1 = new File("1.xml");
        File config2 = new File("2.xml");
        try {

            String firstConf = "<configuration monitorInterval=\"5\">" +
                "  <appenders>" +
                "    <console name=\"console\" target=\"SYSTEM_OUT\">" +
                "      <patternLayout pattern=\"%d %5p   -    %m%n\" />" +
                "    </console>" +
                "  </appenders>" +
                "  <loggers>" +
                "    <root level=\"warn\">" +
                "      <appenderRef ref=\"console\" />" +
                "    </root>" +
                "  </loggers>" +
                "</configuration>";
            FileWriter out = new FileWriter(config1);
            out.write(firstConf);
            out.close();

            String secondConf = "<configuration monitorInterval=\"5\">" +
                "  <loggers>" +
                "    <root level=\"warn\" />" +
                "  </loggers>" +
                "</configuration>";
            out = new FileWriter(config2);
            out.write(secondConf);
            out.close();

            System.getProperties().setProperty("log4j.configurationFile",
                config1.getAbsolutePath() + "," + config2.getAbsolutePath());

            LoggerContext loggerContext = (LoggerContext) 
LogManager.getContext(false);
            loggerContext.reconfigure();

            StatusLogger statusLogger = (StatusLogger) getStatusLogger();
            statusLogger.registerListener(new StatusListener() {
                @Override
                public void close() throws IOException {
                }

                @Override
                public void log(StatusData data) {
                    if (data.getLevel().equals(Level.ERROR))
                        errored();
                }

                @Override
                public Level getStatusLevel() {
                    return Level.ERROR;
                }
            });

            // Check flag is false before reload
            assertFalse(error);

            // Make a change to cause a reload
            out = new FileWriter(config2);
            out.append(' ');
            out.close();

            // Wait for the reload to trigger
            synchronized (this) {
                wait(10000);
            }

            // Check for error
            assertFalse("An error occurred during reconfigure", error);

        } catch (IOException | InterruptedException ex) {
            ex.printStackTrace();
            fail();
        } finally {
            try {
                config1.delete();
            } catch (Exception ex) {
            }
            try {
                config2.delete();
            } catch (Exception ex) {
            }
        }
    }

    boolean error;
    public void errored() {
        error = true;
    }
}
{code}

> Can't set monitorInterval for composite XML configuration.
> ----------------------------------------------------------
>
>                 Key: LOG4J2-2068
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-2068
>             Project: Log4j 2
>          Issue Type: Bug
>          Components: Core
>    Affects Versions: 2.9.1
>            Reporter: Robert Haycock
>
> When trying to combine a composite configuration with automatic reload, it 
> fails to reload. 
> When an {{XmlConfiguration}} is reloaded it calls 
> {{XmlConfiguration.reconfigure()}} which sets the {{rootElement}} field, and 
> everything is fine.
> When a {{CompositeConfiguration}} is reloaded, it doesn't call 
> {{reconfigure()}} on the {{XmlConfigurations}}. This means when it tries to 
> start the config {{XmlConfiguration.setup()}} is called and {{rootElement}} 
> is null, resulting in an error message "No logging configuration".
> End result is the config isn't loaded and there's no more logging.
> To reproduce, it doesn't matter what is in the configurations. Just need at 
> least 2 XML configs in the {{log4j.configurationFile}} property and the 
> {{monitorInterval}} set.
> (Ps. my first ticket)



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to