Search in sources :

Example 1 with LoggerRepository

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());
    }
}
Also used : LoggerRepository(org.apache.log4j.spi.LoggerRepository) Enumeration(java.util.Enumeration) HierarchyDynamicMBean(org.apache.log4j.jmx.HierarchyDynamicMBean) Logger(org.apache.log4j.Logger) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 2 with LoggerRepository

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);
    }
}
Also used : LoggerRepository(org.apache.log4j.spi.LoggerRepository) InterceptContext(com.creditease.monitor.interceptframework.spi.InterceptContext) Enumeration(java.util.Enumeration) Logger(org.apache.log4j.Logger) LogProfileInfo(com.creditease.uav.profiling.handlers.log.LogProfileInfo) LinkedList(java.util.LinkedList)

Example 3 with LoggerRepository

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());
}
Also used : LoggerRepository(org.apache.log4j.spi.LoggerRepository) RootLogger(org.apache.log4j.spi.RootLogger) RootLogger(org.apache.log4j.spi.RootLogger) Properties(java.util.Properties) Test(org.junit.jupiter.api.Test)

Example 4 with LoggerRepository

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"));
}
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 5 with LoggerRepository

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");
    }
}
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

LoggerRepository (org.apache.log4j.spi.LoggerRepository)9 Logger (org.apache.log4j.Logger)6 RootLogger (org.apache.log4j.spi.RootLogger)3 Enumeration (java.util.Enumeration)2 LinkedList (java.util.LinkedList)2 LoggerFactory (org.apache.log4j.spi.LoggerFactory)2 LoggingEvent (org.apache.log4j.spi.LoggingEvent)2 RepositorySelector (org.apache.log4j.spi.RepositorySelector)2 InterceptContext (com.creditease.monitor.interceptframework.spi.InterceptContext)1 LogProfileInfo (com.creditease.uav.profiling.handlers.log.LogProfileInfo)1 BufferedInputStream (java.io.BufferedInputStream)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 MBeanServer (javax.management.MBeanServer)1 ObjectName (javax.management.ObjectName)1 JScrollBar (javax.swing.JScrollBar)1 JScrollPane (javax.swing.JScrollPane)1 JTextArea (javax.swing.JTextArea)1