Search in sources :

Example 96 with Configuration

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

the class LoggerContextAdmin method setConfigLocationUri.

@Override
public void setConfigLocationUri(final String configLocation) throws URISyntaxException, IOException {
    if (configLocation == null || configLocation.isEmpty()) {
        throw new IllegalArgumentException("Missing configuration location");
    }
    LOGGER.debug("---------");
    LOGGER.debug("Remote request to reconfigure using location " + configLocation);
    final File configFile = new File(configLocation);
    ConfigurationSource configSource = null;
    if (configFile.exists()) {
        LOGGER.debug("Opening config file {}", configFile.getAbsolutePath());
        configSource = new ConfigurationSource(new FileInputStream(configFile), configFile);
    } else {
        final URL configURL = new URL(configLocation);
        LOGGER.debug("Opening config URL {}", configURL);
        configSource = new ConfigurationSource(configURL.openStream(), configURL);
    }
    final Configuration config = ConfigurationFactory.getInstance().getConfiguration(loggerContext, configSource);
    loggerContext.start(config);
    LOGGER.debug("Completed remote request to reconfigure.");
}
Also used : ConfigurationSource(org.apache.logging.log4j.core.config.ConfigurationSource) Configuration(org.apache.logging.log4j.core.config.Configuration) File(java.io.File) FileInputStream(java.io.FileInputStream) URL(java.net.URL)

Example 97 with Configuration

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

the class Log4jContextFactory method getContext.

/**
     * Loads the LoggerContext using the ContextSelector.
     * @param fqcn The fully qualified class name of the caller.
     * @param loader The ClassLoader to use or null.
     * @param externalContext An external context (such as a ServletContext) to be associated with the LoggerContext.
     * @param currentContext If true returns the current Context, if false returns the Context appropriate
     * for the caller if a more appropriate Context can be determined.
     * @param source The configuration source.
     * @return The LoggerContext.
     */
public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext, final boolean currentContext, final ConfigurationSource source) {
    final LoggerContext ctx = selector.getContext(fqcn, loader, currentContext, null);
    if (externalContext != null && ctx.getExternalContext() == null) {
        ctx.setExternalContext(externalContext);
    }
    if (ctx.getState() == LifeCycle.State.INITIALIZED) {
        if (source != null) {
            ContextAnchor.THREAD_CONTEXT.set(ctx);
            final Configuration config = ConfigurationFactory.getInstance().getConfiguration(ctx, source);
            LOGGER.debug("Starting LoggerContext[name={}] from configuration {}", ctx.getName(), source);
            ctx.start(config);
            ContextAnchor.THREAD_CONTEXT.remove();
        } else {
            ctx.start();
        }
    }
    return ctx;
}
Also used : CompositeConfiguration(org.apache.logging.log4j.core.config.composite.CompositeConfiguration) Configuration(org.apache.logging.log4j.core.config.Configuration) AbstractConfiguration(org.apache.logging.log4j.core.config.AbstractConfiguration) LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Example 98 with Configuration

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

the class DynamicThresholdFilterTest method testConfig.

@Test
public void testConfig() {
    try (final LoggerContext ctx = Configurator.initialize("Test1", "target/test-classes/log4j2-dynamicfilter.xml")) {
        final Configuration config = ctx.getConfiguration();
        final Filter filter = config.getFilter();
        assertNotNull("No DynamicThresholdFilter", filter);
        assertTrue("Not a DynamicThresholdFilter", filter instanceof DynamicThresholdFilter);
        final DynamicThresholdFilter dynamic = (DynamicThresholdFilter) filter;
        final String key = dynamic.getKey();
        assertNotNull("Key is null", key);
        assertEquals("Incorrect key value", "loginId", key);
        final Map<String, Level> map = dynamic.getLevelMap();
        assertNotNull("Map is null", map);
        assertEquals("Incorrect number of map elements", 1, map.size());
    }
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) Filter(org.apache.logging.log4j.core.Filter) Level(org.apache.logging.log4j.Level) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Test(org.junit.Test)

Example 99 with Configuration

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

the class MapFilterTest method testConfig.

@Test
public void testConfig() {
    final Configuration config = context.getConfiguration();
    final Filter filter = config.getFilter();
    assertNotNull("No MapFilter", filter);
    assertTrue("Not a MapFilter", filter instanceof MapFilter);
    final MapFilter mapFilter = (MapFilter) filter;
    assertFalse("Should not be And filter", mapFilter.isAnd());
    final Map<String, List<String>> map = mapFilter.getMap();
    assertNotNull("No Map", map);
    assertFalse("No elements in Map", map.isEmpty());
    assertEquals("Incorrect number of elements in Map", 1, map.size());
    assertTrue("Map does not contain key eventId", map.containsKey("eventId"));
    assertEquals("List does not contain 2 elements", 2, map.get("eventId").size());
    final Logger logger = LogManager.getLogger(MapFilterTest.class);
    final Map<String, String> eventMap = new HashMap<>();
    eventMap.put("eventId", "Login");
    logger.debug(new MapMessage(eventMap));
    final ListAppender app = context.getListAppender("LIST");
    final List<String> msgs = app.getMessages();
    assertNotNull("No messages", msgs);
    assertFalse("No messages", msgs.isEmpty());
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) Filter(org.apache.logging.log4j.core.Filter) HashMap(java.util.HashMap) MapMessage(org.apache.logging.log4j.message.MapMessage) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Test(org.junit.Test)

Example 100 with Configuration

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

the class ServletAppenderTest method testAppender.

@Test
public void testAppender() throws Exception {
    ContextAnchor.THREAD_CONTEXT.remove();
    final ServletContext servletContext = new MockServletContext();
    servletContext.setAttribute("TestAttr", "AttrValue");
    servletContext.setInitParameter("TestParam", "ParamValue");
    servletContext.setAttribute("Name1", "Ben");
    servletContext.setInitParameter("Name2", "Jerry");
    servletContext.setInitParameter(Log4jWebSupport.LOG4J_CONFIG_LOCATION, CONFIG);
    final Log4jWebLifeCycle initializer = WebLoggerContextUtils.getWebLifeCycle(servletContext);
    try {
        initializer.start();
        initializer.setLoggerContext();
        final LoggerContext ctx = ContextAnchor.THREAD_CONTEXT.get();
        assertNotNull("No LoggerContext", ctx);
        assertNotNull("No ServletContext", ctx.getExternalContext());
        final Configuration configuration = ctx.getConfiguration();
        assertNotNull("No configuration", configuration);
        final Appender appender = configuration.getAppender("Servlet");
        assertNotNull("No ServletAppender", appender);
        final Logger logger = LogManager.getLogger("Test");
        logger.info("This is a test");
        logger.error("This is a test 2", new IllegalStateException().fillInStackTrace());
    } catch (final IllegalStateException e) {
        fail("Failed to initialize Log4j properly." + e.getMessage());
    } finally {
        initializer.stop();
        ContextAnchor.THREAD_CONTEXT.remove();
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) Configuration(org.apache.logging.log4j.core.config.Configuration) ServletContext(javax.servlet.ServletContext) MockServletContext(org.springframework.mock.web.MockServletContext) Logger(org.apache.logging.log4j.Logger) LoggerContext(org.apache.logging.log4j.core.LoggerContext) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.Test)

Aggregations

Configuration (org.apache.logging.log4j.core.config.Configuration)105 LoggerContext (org.apache.logging.log4j.core.LoggerContext)54 Test (org.junit.Test)43 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)39 Appender (org.apache.logging.log4j.core.Appender)21 DefaultConfiguration (org.apache.logging.log4j.core.config.DefaultConfiguration)15 File (java.io.File)12 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)9 RollingFileAppender (org.apache.logging.log4j.core.appender.RollingFileAppender)9 Path (java.nio.file.Path)7 Level (org.apache.logging.log4j.Level)7 NullConfiguration (org.apache.logging.log4j.core.config.NullConfiguration)7 ArrayList (java.util.ArrayList)6 Logger (org.apache.logging.log4j.Logger)6 Filter (org.apache.logging.log4j.core.Filter)6 AbstractLogger (org.apache.logging.log4j.spi.AbstractLogger)6 PatternLayout (org.apache.logging.log4j.core.layout.PatternLayout)5 ListAppender (org.apache.logging.log4j.test.appender.ListAppender)5 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)4 AppenderRef (org.apache.logging.log4j.core.config.AppenderRef)4