Java Code Examples for org.apache.log4j.spi.LoggerRepository#getLogger()

The following examples show how to use org.apache.log4j.spi.LoggerRepository#getLogger() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Log4jTest.java    From sofa-common-tools with Apache License 2.0 6 votes vote down vote up
@Test
public void testIndependentSpaceLog4j() {
    LoggerRepository repo1 = new Hierarchy(new RootLogger((Level) Level.DEBUG));
    URL url1 = LogbackTest.class.getResource("/com/alipay/sofa/rpc/log/log4j/log-conf.xml");
    OptionConverter.selectAndConfigure(url1, null, repo1);
    Logger logger1 = repo1.getLogger("com.foo.Bar");
    Assert.assertNotNull(logger1);

    //log4j logger 2

    LoggerRepository repo2 = new Hierarchy(new RootLogger((Level) Level.DEBUG));
    URL url2 = LogbackTest.class.getResource("/com/alipay/sofa/rpc/log/log4j/log4j_b.xml");
    OptionConverter.selectAndConfigure(url2, null, repo2);
    Logger logger2 = repo1.getLogger("com.foo.Bar2");
    Assert.assertNotNull(logger2);

    Assert.assertNotSame(logger1, logger2);
}
 
Example 2
Source File: Log4jTest.java    From sofa-common-tools with Apache License 2.0 6 votes vote down vote up
@Test
public void testLocalProperties() throws NoSuchFieldException, IllegalAccessException {

    LoggerRepository repo2 = new Hierarchy(new RootLogger((Level) Level.DEBUG));
    URL url2 = LogbackTest.class.getResource("/com/alipay/sofa/rpc/log/log4j/log4j_b.xml");
    DOMConfigurator domConfigurator = new DOMConfigurator();

    Field field = DOMConfigurator.class.getDeclaredField("props");
    field.setAccessible(true);
    Properties props = new Properties();
    field.set(domConfigurator, props);
    props.put("hello", "defaultb");

    domConfigurator.doConfigure(url2, repo2);

    Logger logger2 = repo2.getLogger("com.foo.Bar3");
    Assert.assertTrue(logger2.getAllAppenders().hasMoreElements());

}
 
Example 3
Source File: PropertyConfigurator.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
    Parse non-root elements, such non-root categories and renderers.
 */
 protected
 void parseCatsAndRenderers(Properties props, LoggerRepository hierarchy) {
   Enumeration enumeration = props.propertyNames();
   while(enumeration.hasMoreElements()) {
     String key = (String) enumeration.nextElement();
     if(key.startsWith(CATEGORY_PREFIX) || key.startsWith(LOGGER_PREFIX)) {
String loggerName = null;
if(key.startsWith(CATEGORY_PREFIX)) {
  loggerName = key.substring(CATEGORY_PREFIX.length());
} else if(key.startsWith(LOGGER_PREFIX)) {
  loggerName = key.substring(LOGGER_PREFIX.length());
}
String value =  OptionConverter.findAndSubst(key, props);
Logger logger = hierarchy.getLogger(loggerName, loggerFactory);
synchronized(logger) {
  parseCategory(props, logger, key, loggerName, value);
  parseAdditivityForLogger(props, logger, loggerName);
}
     } else if(key.startsWith(RENDERER_PREFIX)) {
String renderedClass = key.substring(RENDERER_PREFIX.length());
String renderingClass = OptionConverter.findAndSubst(key, props);
if(hierarchy instanceof RendererSupport) {
  RendererMap.addRenderer((RendererSupport) hierarchy, renderedClass,
			  renderingClass);
}
     }
   }
 }