use of org.apache.log4j.spi.DefaultRepositorySelector in project ACS by ACS-Community.
the class Log4jFactory method enableAcsLogging.
/**
* This method must be called once in order to enable ACS logging behind the scenes of log4j logging.
* <p>
* The log4j framework is quite resistant against being substituted with a different logging framework.
* Even though it is possible to configure a custom logger factory using <code>log4j.loggerFactory</code>,
* that factory will not be used when 3rd party code calls the usual <code>Logger.getLogger(name)</code>.
* It seems to make sense only for cases where the custom logger is used as in <code>MyLogger.getLogger(name)</code>.
* log4j-over-slf4j (http://www.slf4j.org/legacy.html) simply re-implements the relevant log4j classes,
* which is too much trouble here for us because only basic log4j features are being used.
* <p>
* We make use of the RepositorySelector mechanism, which log4j foresees for a different purpose,
* to separate logging contexts in an application server that does not have classloader separation.
* (See also http://articles.qos.ch/sc.html.)
* It is not possible to configure this externally, so that an application must call this method.
* See also http://mail-archives.apache.org/mod_mbox/logging-log4j-user/200904.mbox/%3Ca44e15a30904020424g4b7d7fcx63ca32152c81f80d@mail.gmail.com%3E
* <p>
* @TODO: In the future we could let ClientLogManager call this method,
* but currently we are afraid of side effects with frameworks other than the laser alarm system
* that also use log4j (see http://jira.alma.cl/browse/COMP-8423).
*/
public static void enableAcsLogging() {
System.setProperty("log4j.defaultInitOverride", "true");
// System.setProperty("log4j.debug", "true");
Hierarchy h = new MyLog4jHierarchy();
RepositorySelector repositorySelector = new DefaultRepositorySelector(h);
LogManager.setRepositorySelector(repositorySelector, null);
Logger rootLogger = Logger.getRootLogger();
rootLogger.removeAllAppenders();
// to avoid "log4j:WARN No appenders could be found for logger (root)."
rootLogger.addAppender(new NullAppender());
rootLogger.setLevel(Level.ALL);
}
use of org.apache.log4j.spi.DefaultRepositorySelector in project scheduling by ow2-proactive.
the class Log4JRemover method removeLogger.
public static void removeLogger(String name, Hierarchy hierarchy) {
try {
Hierarchy usedHierarchy;
if (hierarchy == null) {
Field repositorySelectorField = LogManager.class.getDeclaredField("repositorySelector");
repositorySelectorField.setAccessible(true);
DefaultRepositorySelector selector = (DefaultRepositorySelector) repositorySelectorField.get(null);
Field repositoryField = DefaultRepositorySelector.class.getDeclaredField("repository");
repositoryField.setAccessible(true);
usedHierarchy = (Hierarchy) repositoryField.get(selector);
} else {
usedHierarchy = hierarchy;
}
Field htField = Hierarchy.class.getDeclaredField("ht");
htField.setAccessible(true);
Hashtable ht = (Hashtable) htField.get(usedHierarchy);
Class categoryKeyclazz = Class.forName("org.apache.log4j.CategoryKey");
Constructor<?> categoryKeyConstructor = categoryKeyclazz.getDeclaredConstructor(String.class);
categoryKeyConstructor.setAccessible(true);
Object key = categoryKeyConstructor.newInstance(name);
Logger logger;
synchronized (ht) {
// remove the logger entry and inside all its parents reference
logger = (Logger) ht.remove(key);
if (logger != null) {
String parentLogger = null;
while ((parentLogger = getParentLogger(name)) != null) {
key = categoryKeyConstructor.newInstance(parentLogger);
Vector categoryEntries = (Vector) ht.get(key);
categoryEntries.remove(logger);
if (categoryEntries.isEmpty()) {
// remove the parent category if empty
ht.remove(key);
}
name = parentLogger;
}
}
}
} catch (Exception e) {
logger.warn("Unable to access Log4J logger table, logger removal will be disabled", e);
}
}
Aggregations