Search in sources :

Example 1 with RepositorySelector

use of org.apache.log4j.spi.RepositorySelector 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");
    }
}
Also used : LoggerRepository(org.apache.log4j.spi.LoggerRepository) RepositorySelector(org.apache.log4j.spi.RepositorySelector) LoggerFactory(org.apache.log4j.spi.LoggerFactory) Logger(org.apache.log4j.Logger)

Example 2 with RepositorySelector

use of org.apache.log4j.spi.RepositorySelector 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);
}
Also used : Hierarchy(org.apache.log4j.Hierarchy) DefaultRepositorySelector(org.apache.log4j.spi.DefaultRepositorySelector) RepositorySelector(org.apache.log4j.spi.RepositorySelector) DefaultRepositorySelector(org.apache.log4j.spi.DefaultRepositorySelector) NullAppender(org.apache.log4j.varia.NullAppender) RootLogger(org.apache.log4j.spi.RootLogger) Logger(org.apache.log4j.Logger) AcsLogger(alma.acs.logging.AcsLogger)

Example 3 with RepositorySelector

use of org.apache.log4j.spi.RepositorySelector in project OpenOLAT by OpenOLAT.

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");
    }
}
Also used : LoggerRepository(org.apache.log4j.spi.LoggerRepository) RepositorySelector(org.apache.log4j.spi.RepositorySelector) LoggerFactory(org.apache.log4j.spi.LoggerFactory) Logger(org.apache.log4j.Logger)

Aggregations

Logger (org.apache.log4j.Logger)3 RepositorySelector (org.apache.log4j.spi.RepositorySelector)3 LoggerFactory (org.apache.log4j.spi.LoggerFactory)2 LoggerRepository (org.apache.log4j.spi.LoggerRepository)2 AcsLogger (alma.acs.logging.AcsLogger)1 Hierarchy (org.apache.log4j.Hierarchy)1 DefaultRepositorySelector (org.apache.log4j.spi.DefaultRepositorySelector)1 RootLogger (org.apache.log4j.spi.RootLogger)1 NullAppender (org.apache.log4j.varia.NullAppender)1