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.");
}
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;
}
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());
}
}
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());
}
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();
}
}
Aggregations