Search in sources :

Example 51 with Appender

use of org.apache.log4j.Appender in project apex-core by apache.

the class LoggerUtilTest method testAndRemoveAppender.

private static void testAndRemoveAppender(String name) {
    Appender appender = org.apache.log4j.Logger.getRootLogger().getAppender(name);
    assertNotNull(appender);
    assertTrue(LoggerUtil.getAppendersNames().contains(name));
    LoggerUtil.removeAppender(name);
    assertNull(org.apache.log4j.Logger.getRootLogger().getAppender(name));
}
Also used : Appender(org.apache.log4j.Appender) ConsoleAppender(org.apache.log4j.ConsoleAppender)

Example 52 with Appender

use of org.apache.log4j.Appender in project ignite by apache.

the class Log4JLogger method addConsoleAppenderIfNeeded.

/**
     * Adds console appender when needed with some default logging settings.
     *
     * @param logLevel Optional log level.
     * @param implInitC Optional log implementation init closure.
     */
private void addConsoleAppenderIfNeeded(@Nullable Level logLevel, @Nullable IgniteClosure<Boolean, Logger> implInitC) {
    if (inited) {
        if (implInitC != null)
            // Do not init.
            impl = implInitC.apply(false);
        return;
    }
    synchronized (mux) {
        if (inited) {
            if (implInitC != null)
                // Do not init.
                impl = implInitC.apply(false);
            return;
        }
        if (implInitC != null)
            // Init logger impl.
            impl = implInitC.apply(true);
        boolean quiet = Boolean.valueOf(System.getProperty(IGNITE_QUIET, "true"));
        boolean consoleAppenderFound = false;
        Category rootCategory = null;
        ConsoleAppender errAppender = null;
        for (Category l = impl; l != null; ) {
            if (!consoleAppenderFound) {
                for (Enumeration appenders = l.getAllAppenders(); appenders.hasMoreElements(); ) {
                    Appender appender = (Appender) appenders.nextElement();
                    if (appender instanceof ConsoleAppender) {
                        if ("CONSOLE_ERR".equals(appender.getName())) {
                            // Treat CONSOLE_ERR appender as a system one and don't count it.
                            errAppender = (ConsoleAppender) appender;
                            continue;
                        }
                        consoleAppenderFound = true;
                        break;
                    }
                }
            }
            if (l.getParent() == null) {
                rootCategory = l;
                break;
            } else
                l = l.getParent();
        }
        if (consoleAppenderFound && quiet)
            // User configured console appender, but log is quiet.
            quiet = false;
        if (!consoleAppenderFound && !quiet && Boolean.valueOf(System.getProperty(IGNITE_CONSOLE_APPENDER, "true"))) {
            // Console appender not found => we've looked through all categories up to root.
            assert rootCategory != null;
            // to configuration and did not set IGNITE_CONSOLE_APPENDER to false.
            if (errAppender != null) {
                rootCategory.addAppender(createConsoleAppender(Level.INFO));
                if (errAppender.getThreshold() == Level.ERROR)
                    errAppender.setThreshold(Level.WARN);
            } else
                // No error console appender => create console appender with no level limit.
                rootCategory.addAppender(createConsoleAppender(Level.OFF));
            if (logLevel != null)
                impl.setLevel(logLevel);
        }
        // If still don't have appenders, disable logging.
        if (!isConfigured())
            impl.setLevel(Level.OFF);
        quiet0 = quiet;
        inited = true;
    }
}
Also used : ConsoleAppender(org.apache.log4j.ConsoleAppender) Appender(org.apache.log4j.Appender) ConsoleAppender(org.apache.log4j.ConsoleAppender) FileAppender(org.apache.log4j.FileAppender) Category(org.apache.log4j.Category) Enumeration(java.util.Enumeration)

Example 53 with Appender

use of org.apache.log4j.Appender in project ignite by apache.

the class GridCacheAbstractLoadTest method configuration.

/**
     * Initializes configurations.
     *
     * @param springCfgPath Configuration file path.
     * @param log Log file name.
     * @return Configuration.
     * @throws IgniteCheckedException If fails.
     */
@SuppressWarnings("unchecked")
protected IgniteConfiguration configuration(String springCfgPath, String log) throws IgniteCheckedException {
    File path = GridTestUtils.resolveIgnitePath(springCfgPath);
    if (path == null)
        throw new IgniteCheckedException("Spring XML configuration file path is invalid: " + new File(springCfgPath) + ". Note that this path should be either absolute path or a relative path to IGNITE_HOME.");
    if (!path.isFile())
        throw new IgniteCheckedException("Provided file path is not a file: " + path);
    // Add no-op logger to remove no-appender warning.
    Appender app = new NullAppender();
    Logger.getRootLogger().addAppender(app);
    ApplicationContext springCtx;
    try {
        springCtx = new FileSystemXmlApplicationContext(path.toURI().toURL().toString());
    } catch (BeansException | MalformedURLException e) {
        throw new IgniteCheckedException("Failed to instantiate Spring XML application context: " + e.getMessage(), e);
    }
    Map cfgMap;
    try {
        // Note: Spring is not generics-friendly.
        cfgMap = springCtx.getBeansOfType(IgniteConfiguration.class);
    } catch (BeansException e) {
        throw new IgniteCheckedException("Failed to instantiate bean [type=" + IgniteConfiguration.class + ", err=" + e.getMessage() + ']', e);
    }
    if (cfgMap == null)
        throw new IgniteCheckedException("Failed to find a single grid factory configuration in: " + path);
    // Remove previously added no-op logger.
    Logger.getRootLogger().removeAppender(app);
    if (cfgMap.isEmpty())
        throw new IgniteCheckedException("Can't find grid factory configuration in: " + path);
    else if (cfgMap.size() > 1)
        throw new IgniteCheckedException("More than one configuration provided for cache load test: " + cfgMap.values());
    IgniteConfiguration cfg = (IgniteConfiguration) cfgMap.values().iterator().next();
    cfg.setGridLogger(initLogger(log));
    cfg.getTransactionConfiguration().setDefaultTxIsolation(isolation);
    cfg.getTransactionConfiguration().setDefaultTxConcurrency(concurrency);
    return cfg;
}
Also used : NullAppender(org.apache.log4j.varia.NullAppender) Appender(org.apache.log4j.Appender) ConsoleAppender(org.apache.log4j.ConsoleAppender) RollingFileAppender(org.apache.log4j.RollingFileAppender) FileSystemXmlApplicationContext(org.springframework.context.support.FileSystemXmlApplicationContext) FileSystemXmlApplicationContext(org.springframework.context.support.FileSystemXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) MalformedURLException(java.net.MalformedURLException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) NullAppender(org.apache.log4j.varia.NullAppender) File(java.io.File) Map(java.util.Map) BeansException(org.springframework.beans.BeansException)

Example 54 with Appender

use of org.apache.log4j.Appender in project tomee by apache.

the class Log4jLogger method getHandlers.

public synchronized Handler[] getHandlers() {
    final List<Handler> ret = new ArrayList<Handler>();
    final Enumeration<?> en = log.getAllAppenders();
    while (en.hasMoreElements()) {
        final Appender ap = (Appender) en.nextElement();
        if (ap instanceof HandlerWrapper) {
            ret.add(((HandlerWrapper) ap).getHandler());
        }
    }
    return ret.toArray(new Handler[ret.size()]);
}
Also used : Appender(org.apache.log4j.Appender) ArrayList(java.util.ArrayList) Handler(java.util.logging.Handler)

Aggregations

Appender (org.apache.log4j.Appender)54 FileAppender (org.apache.log4j.FileAppender)17 Logger (org.apache.log4j.Logger)15 File (java.io.File)13 ConsoleAppender (org.apache.log4j.ConsoleAppender)11 RollingFileAppender (org.apache.log4j.RollingFileAppender)11 Log4JLogger (org.apache.commons.logging.impl.Log4JLogger)8 Enumeration (java.util.Enumeration)7 AsyncAppender (org.apache.log4j.AsyncAppender)6 PatternLayout (org.apache.log4j.PatternLayout)6 IOException (java.io.IOException)5 ActiveDbAppender (com.axway.ats.log.appenders.ActiveDbAppender)4 PassiveDbAppender (com.axway.ats.log.appenders.PassiveDbAppender)4 MalformedURLException (java.net.MalformedURLException)4 Map (java.util.Map)4 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)4 Category (org.apache.log4j.Category)4 DailyRollingFileAppender (org.apache.log4j.DailyRollingFileAppender)4 Layout (org.apache.log4j.Layout)4