Search in sources :

Example 6 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
    list.addAll(figureoutLogConfiguration(logger4j.getRootLogger(), 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();
        list.addAll(figureoutLogConfiguration(sLogger, 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 7 with LoggerRepository

use of org.apache.log4j.spi.LoggerRepository in project JMRI by JMRI.

the class Log4JTreePane method initComponents.

/**
     * 2nd stage of initialization, invoked after the constructor is complete.
     */
@SuppressWarnings("unchecked")
@Override
public void initComponents() throws Exception {
    LoggerRepository repo = Logger.getRootLogger().getLoggerRepository();
    List<String> list = new ArrayList<String>();
    for (java.util.Enumeration<Logger> e = repo.getCurrentLoggers(); e.hasMoreElements(); ) {
        Logger l = e.nextElement();
        list.add(l.getName() + " - " + (l.getLevel() != null ? "[" + l.getLevel().toString() + "]" : "{" + Logger.getRootLogger().getLevel().toString() + "}"));
    }
    java.util.Collections.sort(list);
    StringBuilder result = new StringBuilder();
    for (String s : list) {
        result.append(s).append("\n");
    }
    JTextArea text = new JTextArea();
    text.setText(result.toString());
    JScrollPane scroll = new JScrollPane(text);
    add(scroll);
    // start scrolled to top
    text.setCaretPosition(0);
    JScrollBar b = scroll.getVerticalScrollBar();
    b.setValue(b.getMaximum());
}
Also used : JScrollPane(javax.swing.JScrollPane) LoggerRepository(org.apache.log4j.spi.LoggerRepository) JTextArea(javax.swing.JTextArea) ArrayList(java.util.ArrayList) Logger(org.apache.log4j.Logger) JScrollBar(javax.swing.JScrollBar)

Example 8 with LoggerRepository

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

Example 9 with LoggerRepository

use of org.apache.log4j.spi.LoggerRepository in project parseq by linkedin.

the class TestUtil method withDisabledLogging.

public static void withDisabledLogging(final Runnable r) {
    // Note: this assumes we're using Log4J and needs to be fixed up if this changes.
    final LoggerRepository loggerRepo = LogManager.getLoggerRepository();
    final Level oldLevel = loggerRepo.getThreshold();
    loggerRepo.setThreshold(Level.OFF);
    try {
        r.run();
    } finally {
        loggerRepo.setThreshold(oldLevel);
    }
}
Also used : LoggerRepository(org.apache.log4j.spi.LoggerRepository) Level(org.apache.log4j.Level)

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