use of org.apache.logging.log4j.core.impl.Log4jContextFactory in project hive by apache.
the class TestHiveAsyncLogging method testAsyncLoggingInitialization.
// this test requires disruptor jar in classpath
@Test
public void testAsyncLoggingInitialization() throws Exception {
HiveConf conf = new HiveConf();
conf.setBoolVar(ConfVars.HIVE_ASYNC_LOG_ENABLED, false);
LogUtils.initHiveLog4jCommon(conf, ConfVars.HIVE_LOG4J_FILE);
Log4jContextFactory log4jContextFactory = (Log4jContextFactory) LogManager.getFactory();
ContextSelector contextSelector = log4jContextFactory.getSelector();
assertTrue(contextSelector instanceof ClassLoaderContextSelector);
conf.setBoolVar(ConfVars.HIVE_ASYNC_LOG_ENABLED, true);
LogUtils.initHiveLog4jCommon(conf, ConfVars.HIVE_LOG4J_FILE);
log4jContextFactory = (Log4jContextFactory) LogManager.getFactory();
contextSelector = log4jContextFactory.getSelector();
assertTrue(contextSelector instanceof AsyncLoggerContextSelector);
}
use of org.apache.logging.log4j.core.impl.Log4jContextFactory in project druid by druid-io.
the class Log4jShutterDownerModule method configure.
@Override
public void configure(Binder binder) {
try {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
if (loader == null) {
loader = getClass().getClassLoader();
}
// Reflection to try and allow non Log4j2 stuff to run. This acts as a gateway to stop errors in the next few lines
// In log4j api
final Class<?> logManagerClazz = Class.forName("org.apache.logging.log4j.LogManager", false, loader);
// In log4j core
final Class<?> callbackRegistryClazz = Class.forName("org.apache.logging.log4j.core.util.ShutdownCallbackRegistry", false, loader);
final LoggerContextFactory contextFactory = LogManager.getFactory();
if (!(contextFactory instanceof Log4jContextFactory)) {
log.warn("Expected [%s] found [%s]. Unknown class for context factory. Not logging shutdown", Log4jContextFactory.class.getCanonicalName(), contextFactory.getClass().getCanonicalName());
return;
}
final ShutdownCallbackRegistry registry = ((Log4jContextFactory) contextFactory).getShutdownCallbackRegistry();
if (!(registry instanceof Log4jShutdown)) {
log.warn("Shutdown callback registry expected class [%s] found [%s]. Skipping shutdown registry", Log4jShutdown.class.getCanonicalName(), registry.getClass().getCanonicalName());
return;
}
binder.bind(Log4jShutdown.class).toInstance((Log4jShutdown) registry);
binder.bind(Key.get(Log4jShutterDowner.class, Names.named("ForTheEagerness"))).to(Log4jShutterDowner.class).asEagerSingleton();
} catch (ClassNotFoundException | ClassCastException | LinkageError e) {
log.warn(e, "Not registering log4j shutdown hooks. Not using log4j?");
}
}
use of org.apache.logging.log4j.core.impl.Log4jContextFactory in project hive by apache.
the class LogUtils method checkAndSetAsyncLogging.
public static boolean checkAndSetAsyncLogging(final Configuration conf) {
final boolean asyncLogging = HiveConf.getBoolVar(conf, ConfVars.HIVE_ASYNC_LOG_ENABLED);
if (asyncLogging) {
System.setProperty("Log4jContextSelector", "org.apache.logging.log4j.core.async.AsyncLoggerContextSelector");
// default is ClassLoaderContextSelector which is created during automatic logging
// initialization in a static initialization block.
// Changing ContextSelector at runtime requires creating new context factory which will
// internally create new context selector based on system property.
LogManager.setFactory(new Log4jContextFactory());
}
return asyncLogging;
}
use of org.apache.logging.log4j.core.impl.Log4jContextFactory in project logging-log4j2 by apache.
the class ShutdownCallbackRegistryTest method testShutdownCallbackRegistry.
@Test
public void testShutdownCallbackRegistry() throws Exception {
final LoggerContext context = ctx.getLoggerContext();
assertTrue("LoggerContext should be started", context.isStarted());
assertThat(Registry.CALLBACKS, hasSize(1));
Registry.shutdown();
assertTrue("LoggerContext should be stopped", context.isStopped());
assertThat(Registry.CALLBACKS, hasSize(0));
final ContextSelector selector = ((Log4jContextFactory) LogManager.getFactory()).getSelector();
assertThat(selector.getLoggerContexts(), not(hasItem(context)));
}
use of org.apache.logging.log4j.core.impl.Log4jContextFactory in project logging-log4j2 by apache.
the class Log4jWebInitializerImpl method initializeJndi.
private void initializeJndi(final String location) {
final URI configLocation = getConfigURI(location);
if (this.name == null) {
throw new IllegalStateException("A log4jContextName context parameter is required");
}
LoggerContext context;
final LoggerContextFactory factory = LogManager.getFactory();
if (factory instanceof Log4jContextFactory) {
final ContextSelector selector = ((Log4jContextFactory) factory).getSelector();
if (selector instanceof NamedContextSelector) {
this.namedContextSelector = (NamedContextSelector) selector;
context = this.namedContextSelector.locateContext(this.name, this.servletContext, configLocation);
ContextAnchor.THREAD_CONTEXT.set(context);
if (context.isInitialized()) {
context.start();
}
ContextAnchor.THREAD_CONTEXT.remove();
} else {
LOGGER.warn("Potential problem: Selector is not an instance of NamedContextSelector.");
return;
}
} else {
LOGGER.warn("Potential problem: LoggerContextFactory is not an instance of Log4jContextFactory.");
return;
}
this.loggerContext = context;
LOGGER.debug("Created logger context for [{}] using [{}].", this.name, context.getClass().getClassLoader());
}
Aggregations