use of org.apache.log4j.spi.LoggerRepository in project voltdb by VoltDB.
the class ManagedUtil method registerLog4jMBeans.
/**
* Register the log4j JMX mbeans. Set environment variable
* "zookeeper.jmx.log4j.disable" to true to disable registration.
* @see http://logging.apache.org/log4j/1.2/apidocs/index.html?org/apache/log4j/jmx/package-summary.html
* @throws JMException if registration fails
*/
public static void registerLog4jMBeans() throws JMException {
if (Boolean.getBoolean("zookeeper.jmx.log4j.disable") == true) {
return;
}
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
// Create and Register the top level Log4J MBean
HierarchyDynamicMBean hdm = new HierarchyDynamicMBean();
ObjectName mbo = new ObjectName("log4j:hiearchy=default");
mbs.registerMBean(hdm, mbo);
// Add the root logger to the Hierarchy MBean
Logger rootLogger = Logger.getRootLogger();
hdm.addLoggerMBean(rootLogger.getName());
// Get each logger from the Log4J Repository and add it to
// the Hierarchy MBean created above.
LoggerRepository r = LogManager.getLoggerRepository();
Enumeration enumer = r.getCurrentLoggers();
Logger logger = null;
while (enumer.hasMoreElements()) {
logger = (Logger) enumer.nextElement();
hdm.addLoggerMBean(logger.getName());
}
}
use of org.apache.log4j.spi.LoggerRepository in project uavstack by uavorg.
the class Log4jHookProxy method figureOutLog4jConfig.
/**
* figure out the log4j's configuration: for example, appenders' file path, buff io, etc...
*
* @param context
* @param webapploader
*/
@SuppressWarnings({ "rawtypes", "static-access" })
private void figureOutLog4jConfig(HookContext context, ClassLoader webapploader) {
Logger logger4j = Logger.getLogger(Log4jHookProxy.class);
InterceptContext interceptContext = (InterceptContext) context.get(HookConstants.INTERCEPTCONTEXT);
if (interceptContext == null) {
logger.warn("No InterceptContext available, can't figure out Log4j configuration.", null);
return;
}
@SuppressWarnings("unchecked") LinkedList<LogProfileInfo> list = (LinkedList<LogProfileInfo>) interceptContext.get(HookConstants.LOG_PROFILE_LIST);
if (null == list) {
list = new LinkedList<LogProfileInfo>();
interceptContext.put(HookConstants.LOG_PROFILE_LIST, list);
}
String appid = (String) (interceptContext.get(InterceptConstants.CONTEXTPATH));
// figureout root logger
figureoutLogConfiguration(logger4j.getRootLogger(), list, appid);
// figureour norootlogger
LoggerRepository lr = null;
try {
lr = logger4j.getLoggerRepository();
} catch (NoSuchMethodError err) {
// for log4j-over-slf4j, doesn't have this method
return;
}
Enumeration logEnum = lr.getCurrentLoggers();
while (logEnum != null && logEnum.hasMoreElements()) {
Logger sLogger = (Logger) logEnum.nextElement();
figureoutLogConfiguration(sLogger, list, appid);
}
}
use of org.apache.log4j.spi.LoggerRepository in project logging-log4j2 by apache.
the class PropertyConfiguratorTest method testLocalVsGlobal.
@Test
public void testLocalVsGlobal() {
LoggerRepository repos1, repos2;
final Logger catA = Logger.getLogger(CAT_A_NAME);
final Logger catB = Logger.getLogger(CAT_B_NAME);
final Logger catC = Logger.getLogger(CAT_C_NAME);
final Properties globalSettings = new Properties();
globalSettings.put("log4j.logger." + CAT_A_NAME, Level.WARN.toString());
globalSettings.put("log4j.logger." + CAT_B_NAME, Level.WARN.toString());
globalSettings.put("log4j.logger." + CAT_C_NAME, Level.DEBUG.toString());
PropertyConfigurator.configure(globalSettings);
assertEquals(Level.WARN, catA.getLevel());
assertEquals(Level.WARN, catB.getLevel());
assertEquals(Level.DEBUG, catC.getLevel());
assertEquals(Level.WARN, catA.getLoggerRepository().getLogger(CAT_A_NAME).getLevel());
assertEquals(Level.WARN, catB.getLoggerRepository().getLogger(CAT_B_NAME).getLevel());
assertEquals(Level.DEBUG, catC.getLoggerRepository().getLogger(CAT_C_NAME).getLevel());
final Properties repos1Settings = new Properties();
repos1Settings.put("log4j.logger." + CAT_A_NAME, Level.DEBUG.toString());
repos1Settings.put("log4j.logger." + CAT_B_NAME, Level.INFO.toString());
repos1 = new Hierarchy(new RootLogger(Level.OFF));
new PropertyConfigurator().doConfigure(repos1Settings, repos1);
assertEquals(Level.DEBUG, repos1.getLogger(CAT_A_NAME).getLevel());
assertEquals(Level.INFO, repos1.getLogger(CAT_B_NAME).getLevel());
final Properties repos2Settings = new Properties();
repos2Settings.put("log4j.logger." + CAT_A_NAME, Level.INFO.toString());
repos2Settings.put("log4j.logger." + CAT_B_NAME, Level.DEBUG.toString());
repos2 = new Hierarchy(new RootLogger(Level.OFF));
new PropertyConfigurator().doConfigure(repos2Settings, repos2);
assertEquals(Level.INFO, repos2.getLogger(CAT_A_NAME).getLevel());
assertEquals(Level.DEBUG, repos2.getLogger(CAT_B_NAME).getLevel());
}
use of org.apache.log4j.spi.LoggerRepository in project commons by twitter.
the class JULBridgeLogManagerTest method checkAssimilateTakesOver.
@Test
public void checkAssimilateTakesOver() {
// Create a test log4j environment
final List<LoggingEvent> events = new LinkedList<LoggingEvent>();
org.apache.log4j.Logger log4jRoot = new RootLogger(org.apache.log4j.Level.ALL);
LoggerRepository loggerRepository = new Hierarchy(log4jRoot);
loggerRepository.setThreshold(org.apache.log4j.Level.INFO);
log4jRoot.addAppender(new AppenderSkeleton() {
@Override
public boolean requiresLayout() {
return false;
}
@Override
public void close() {
}
@Override
protected void append(LoggingEvent event) {
events.add(event);
}
});
JULBridgeLogManager.assimilate(loggerRepository);
Logger.getLogger("test.1").log(Level.INFO, "test message 1");
Logger.getLogger("test.2").log(Level.FINE, "test message 2");
Logger.getLogger("test.3").log(Level.WARNING, "test message 3");
assertThat(events.size(), is(2));
assertThat(events.get(0).getLoggerName(), is("test.1"));
assertThat(events.get(0).getMessage(), is((Object) "test message 1"));
assertThat(events.get(1).getLoggerName(), is("test.3"));
assertThat(events.get(1).getMessage(), is((Object) "test message 3"));
}
use of org.apache.log4j.spi.LoggerRepository in project openolat by klemens.
the class ThreadLocalLogLevelManager method install.
/**
* Installs the ThreadLogManager in this system.
* <p>
* Note that this can fail if some other framework
* has done a call to LogManager.setRepositorySelector
* with a guard already.
* @see org.apache.log4j.LogManager#setRepositorySelector(org.apache.log4j.spi.RepositorySelector, Object)
* @param logMessageModifier optional implementation of LogMessageModifier
* which allows messages to be modified should they be affected by
* a threadlocal loglevel overwrite. This allows for example for
* messages to be prepended with a token so that they can be easier
* found in the log
*/
void install(final LogMessageModifier logMessageModifier) {
try {
final LoggerFactory loggerFactory = new LoggerFactory() {
@SuppressWarnings("synthetic-access")
@Override
public Logger makeNewLoggerInstance(String name) {
return new ThreadLocalAwareLogger(name, threadLocalLogLevel_, logMessageModifier);
}
};
final Logger originalRootLogger = LogManager.getRootLogger();
final LoggerRepository parentRepository = originalRootLogger.getLoggerRepository();
final LoggerRepository repository = new ThreadLocalAwareLoggerRepository(originalRootLogger, parentRepository, loggerFactory);
LogManager.setRepositorySelector(new RepositorySelector() {
@Override
public LoggerRepository getLoggerRepository() {
return repository;
}
}, guard);
} catch (IllegalArgumentException | SecurityException re) {
// thrown by LogManager.setRepositorySelector
Logger.getLogger(ThreadLocalLogLevelManager.class).error("Could not install ThreadLocalLogLevelManager");
}
}
Aggregations