Search in sources :

Example 1 with Hierarchy

use of org.apache.log4j.Hierarchy 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"));
}
Also used : LoggingEvent(org.apache.log4j.spi.LoggingEvent) LoggerRepository(org.apache.log4j.spi.LoggerRepository) Hierarchy(org.apache.log4j.Hierarchy) AppenderSkeleton(org.apache.log4j.AppenderSkeleton) RootLogger(org.apache.log4j.spi.RootLogger) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 2 with Hierarchy

use of org.apache.log4j.Hierarchy 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)

Aggregations

Hierarchy (org.apache.log4j.Hierarchy)2 RootLogger (org.apache.log4j.spi.RootLogger)2 AcsLogger (alma.acs.logging.AcsLogger)1 LinkedList (java.util.LinkedList)1 AppenderSkeleton (org.apache.log4j.AppenderSkeleton)1 Logger (org.apache.log4j.Logger)1 DefaultRepositorySelector (org.apache.log4j.spi.DefaultRepositorySelector)1 LoggerRepository (org.apache.log4j.spi.LoggerRepository)1 LoggingEvent (org.apache.log4j.spi.LoggingEvent)1 RepositorySelector (org.apache.log4j.spi.RepositorySelector)1 NullAppender (org.apache.log4j.varia.NullAppender)1 Test (org.junit.Test)1