Search in sources :

Example 21 with DefaultConfiguration

use of org.apache.logging.log4j.core.config.DefaultConfiguration in project logging-log4j2 by apache.

the class LateConfigTest method testReconfiguration.

@Test
public void testReconfiguration() throws Exception {
    final Configuration cfg = context.getConfiguration();
    assertNotNull(cfg, "No configuration");
    assertTrue(cfg instanceof DefaultConfiguration, "Not set to default configuration");
    final File file = new File(CONFIG);
    final LoggerContext loggerContext = LoggerContext.getContext(null, false, file.toURI());
    assertNotNull(loggerContext, "No Logger Context");
    final Configuration newConfig = loggerContext.getConfiguration();
    assertNotSame(cfg, newConfig, "Configuration not reset");
    assertTrue(newConfig instanceof XmlConfiguration, "Reconfiguration failed");
    context = LoggerContext.getContext(false);
    final Configuration sameConfig = context.getConfiguration();
    assertSame(newConfig, sameConfig, "Configuration should not have been reset");
}
Also used : DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) XmlConfiguration(org.apache.logging.log4j.core.config.xml.XmlConfiguration) Configuration(org.apache.logging.log4j.core.config.Configuration) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) XmlConfiguration(org.apache.logging.log4j.core.config.xml.XmlConfiguration) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 22 with DefaultConfiguration

use of org.apache.logging.log4j.core.config.DefaultConfiguration in project logging-log4j2 by apache.

the class EncodingPatternConverterTest method testHandlesThrowable.

@Test
public void testHandlesThrowable() {
    final Configuration configuration = new DefaultConfiguration();
    assertFalse(EncodingPatternConverter.newInstance(configuration, new String[] { "%msg", "XML" }).handlesThrowable());
    assertTrue(EncodingPatternConverter.newInstance(configuration, new String[] { "%xThrowable{full}", "JSON" }).handlesThrowable());
    assertTrue(EncodingPatternConverter.newInstance(configuration, new String[] { "%ex", "XML" }).handlesThrowable());
}
Also used : DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) Configuration(org.apache.logging.log4j.core.config.Configuration) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) Test(org.junit.jupiter.api.Test)

Example 23 with DefaultConfiguration

use of org.apache.logging.log4j.core.config.DefaultConfiguration in project logging-log4j2 by apache.

the class ScriptConditionTest method testSelectFilesToDelete.

@Test
public void testSelectFilesToDelete() {
    final Configuration config = new DefaultConfiguration();
    // creates the ScriptManager
    config.initialize();
    // script that returns pathList
    final ScriptPlugin script = new ScriptPlugin("test", "javascript", "pathList;");
    final ScriptCondition condition = ScriptCondition.createCondition(script, config);
    final List<PathWithAttributes> pathList = new ArrayList<>();
    final Path base = Paths.get("baseDirectory");
    final List<PathWithAttributes> result = condition.selectFilesToDelete(base, pathList);
    assertSame(result, pathList);
}
Also used : Path(java.nio.file.Path) PathWithAttributes(org.apache.logging.log4j.core.appender.rolling.action.PathWithAttributes) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) Configuration(org.apache.logging.log4j.core.config.Configuration) ArrayList(java.util.ArrayList) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) ScriptPlugin(org.apache.logging.log4j.script.ScriptPlugin) Test(org.junit.jupiter.api.Test)

Example 24 with DefaultConfiguration

use of org.apache.logging.log4j.core.config.DefaultConfiguration in project logging-log4j2 by apache.

the class ScriptConditionTest method testSelectFilesToDelete2.

@Test
public void testSelectFilesToDelete2() {
    final Configuration config = new DefaultConfiguration();
    // creates the ScriptManager
    config.initialize();
    final List<PathWithAttributes> pathList = new ArrayList<>();
    pathList.add(new PathWithAttributes(Paths.get("/path/1"), new DummyFileAttributes()));
    pathList.add(new PathWithAttributes(Paths.get("/path/2"), new DummyFileAttributes()));
    pathList.add(new PathWithAttributes(Paths.get("/path/3"), new DummyFileAttributes()));
    final String scriptText = // 
    "pathList.remove(1);" + "pathList;";
    final ScriptPlugin script = new ScriptPlugin("test", "javascript", scriptText);
    final ScriptCondition condition = ScriptCondition.createCondition(script, config);
    final Path base = Paths.get("baseDirectory");
    final List<PathWithAttributes> result = condition.selectFilesToDelete(base, pathList);
    assertSame(result, pathList);
    assertEquals(2, result.size());
    assertEquals(Paths.get("/path/1"), result.get(0).getPath());
    assertEquals(Paths.get("/path/3"), result.get(1).getPath());
}
Also used : Path(java.nio.file.Path) PathWithAttributes(org.apache.logging.log4j.core.appender.rolling.action.PathWithAttributes) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) Configuration(org.apache.logging.log4j.core.config.Configuration) ArrayList(java.util.ArrayList) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) DummyFileAttributes(org.apache.logging.log4j.core.test.appender.rolling.action.DummyFileAttributes) ScriptPlugin(org.apache.logging.log4j.script.ScriptPlugin) Test(org.junit.jupiter.api.Test)

Example 25 with DefaultConfiguration

use of org.apache.logging.log4j.core.config.DefaultConfiguration in project logging-log4j2 by apache.

the class WatchHttpTest method testNotModified.

@Test
public void testNotModified() throws Exception {
    BlockingQueue<String> queue = new LinkedBlockingQueue<>();
    List<ConfigurationListener> listeners = new ArrayList<>();
    listeners.add(new TestConfigurationListener(queue, "log4j-test2.xml"));
    TimeZone timeZone = TimeZone.getTimeZone("UTC");
    Calendar now = Calendar.getInstance(timeZone);
    Calendar previous = now;
    previous.add(Calendar.MINUTE, -5);
    Configuration configuration = new DefaultConfiguration();
    Assume.assumeTrue(!IS_WINDOWS || Boolean.getBoolean(FORCE_RUN_KEY));
    URL url = new URL("http://localhost:" + wireMockRule.port() + "/log4j-test2.xml");
    StubMapping stubMapping = stubFor(get(urlPathEqualTo("/log4j-test2.xml")).willReturn(aResponse().withBodyFile(file).withStatus(304).withHeader("Last-Modified", formatter.format(now) + " GMT").withHeader("Content-Type", XML)));
    final ConfigurationScheduler scheduler = new ConfigurationScheduler();
    scheduler.incrementScheduledItems();
    final WatchManager watchManager = new WatchManager(scheduler);
    watchManager.setIntervalSeconds(1);
    scheduler.start();
    watchManager.start();
    try {
        watchManager.watch(new Source(url.toURI()), new HttpWatcher(configuration, null, listeners, previous.getTimeInMillis()));
        final String str = queue.poll(2, TimeUnit.SECONDS);
        assertNull("File changed.", str);
    } finally {
        removeStub(stubMapping);
        watchManager.stop();
        scheduler.stop();
    }
}
Also used : HttpWatcher(org.apache.logging.log4j.core.config.HttpWatcher) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) Configuration(org.apache.logging.log4j.core.config.Configuration) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) StubMapping(com.github.tomakehurst.wiremock.stubbing.StubMapping) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ConfigurationScheduler(org.apache.logging.log4j.core.config.ConfigurationScheduler) URL(java.net.URL) ConfigurationListener(org.apache.logging.log4j.core.config.ConfigurationListener) TimeZone(java.util.TimeZone) Test(org.junit.Test)

Aggregations

DefaultConfiguration (org.apache.logging.log4j.core.config.DefaultConfiguration)29 Configuration (org.apache.logging.log4j.core.config.Configuration)21 ArrayList (java.util.ArrayList)9 Test (org.junit.Test)8 Path (java.nio.file.Path)7 Test (org.junit.jupiter.api.Test)7 LoggerContext (org.apache.logging.log4j.core.LoggerContext)6 PathWithAttributes (org.apache.logging.log4j.core.appender.rolling.action.PathWithAttributes)3 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)3 Script (org.apache.logging.log4j.core.script.Script)3 ScriptPlugin (org.apache.logging.log4j.script.ScriptPlugin)3 Setup (org.openjdk.jmh.annotations.Setup)3 StubMapping (com.github.tomakehurst.wiremock.stubbing.StubMapping)2 File (java.io.File)2 URL (java.net.URL)2 Calendar (java.util.Calendar)2 TimeZone (java.util.TimeZone)2 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)2 Appender (org.apache.logging.log4j.core.Appender)2 LogEvent (org.apache.logging.log4j.core.LogEvent)2