Search in sources :

Example 36 with Appender

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

the class NodeLogger method addKNIMEConsoleWriter.

/**
 * Adds a new {@link java.io.Writer} with the given level to this logger.
 *
 * @param writer The writer to add.
 * @param minLevel The minimum level to output.
 * @param maxLevel The maximum level to output.
 * @since 2.12
 */
public static void addKNIMEConsoleWriter(final Writer writer, final LEVEL minLevel, final LEVEL maxLevel) {
    final Appender a = Logger.getRootLogger().getAppender(KNIME_CONSOLE_APPENDER);
    final Layout layout;
    if (a != null) {
        layout = a.getLayout();
        checkLayoutFlags(layout);
    } else {
        layout = WF_DIR_LOG_FILE_LAYOUT;
    }
    addWriter(writer, layout, minLevel, maxLevel);
}
Also used : LogfileAppender(org.knime.core.util.LogfileAppender) NullAppender(org.apache.log4j.varia.NullAppender) Appender(org.apache.log4j.Appender) FileAppender(org.apache.log4j.FileAppender) WriterAppender(org.apache.log4j.WriterAppender) Layout(org.apache.log4j.Layout) PatternLayout(org.apache.log4j.PatternLayout)

Example 37 with Appender

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

the class NodeLogger method addWriter.

/**
 * Adds a new {@link java.io.Writer} with the given level to this logger.
 *
 * @param writer The writer to add.
 * @param layout the log file layout to use
 * @param minLevel The minimum level to output.
 * @param maxLevel The maximum level to output.
 * @since 2.12
 */
public static void addWriter(final Writer writer, final Layout layout, final LEVEL minLevel, final LEVEL maxLevel) {
    WriterAppender app = new WriterAppender(layout, writer);
    app.setImmediateFlush(true);
    LevelRangeFilter filter = new LevelRangeFilter();
    filter.setLevelMin(transLEVEL(minLevel));
    filter.setLevelMax(transLEVEL(maxLevel));
    app.addFilter(filter);
    // remove the writer first if existent
    synchronized (WRITER) {
        if (WRITER.containsKey(writer)) {
            Appender a = WRITER.get(writer);
            Logger.getRootLogger().removeAppender(a);
            WRITER.remove(writer);
        }
        // register new appender
        WRITER.put(writer, app);
    }
    Logger.getRootLogger().addAppender(app);
    checkLayoutFlags(layout);
}
Also used : LogfileAppender(org.knime.core.util.LogfileAppender) NullAppender(org.apache.log4j.varia.NullAppender) Appender(org.apache.log4j.Appender) FileAppender(org.apache.log4j.FileAppender) WriterAppender(org.apache.log4j.WriterAppender) LevelRangeFilter(org.apache.log4j.varia.LevelRangeFilter) WriterAppender(org.apache.log4j.WriterAppender)

Example 38 with Appender

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

the class DataStreamerImplSelfTest method testRetryWhenTopologyMismatch.

/**
 * Cluster topology mismatch shall result in DataStreamer retrying cache update with the latest topology and
 * no error logged to the console.
 *
 * @throws Exception if failed
 */
public void testRetryWhenTopologyMismatch() throws Exception {
    final int KEY = 1;
    final String VAL = "1";
    cnt = 0;
    StringWriter logWriter = new StringWriter();
    Appender logAppender = new WriterAppender(new SimpleLayout(), logWriter);
    Logger.getRootLogger().addAppender(logAppender);
    // cache-enabled nodes
    startGrids(MAX_CACHE_COUNT - 1);
    try (Ignite ignite = startGrid(MAX_CACHE_COUNT);
        IgniteDataStreamer<Integer, String> streamer = ignite.dataStreamer(DEFAULT_CACHE_NAME)) {
        // simulate stale topology for the next action
        needStaleTop = true;
        streamer.addData(KEY, VAL);
    } finally {
        needStaleTop = false;
        logWriter.flush();
        Logger.getRootLogger().removeAppender(logAppender);
        logAppender.close();
    }
    assertFalse(logWriter.toString().contains("DataStreamer will retry data transfer at stable topology"));
}
Also used : Appender(org.apache.log4j.Appender) WriterAppender(org.apache.log4j.WriterAppender) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SimpleLayout(org.apache.log4j.SimpleLayout) StringWriter(java.io.StringWriter) Ignite(org.apache.ignite.Ignite) WriterAppender(org.apache.log4j.WriterAppender)

Example 39 with Appender

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

the class GridSingleExecutionTest method getConfigurations.

/**
 * Initializes configurations.
 *
 * @param springCfgPath Configuration file path.
 * @param log Log file name.
 * @return List of configurations.
 * @throws IgniteCheckedException If failed..
 */
@SuppressWarnings("unchecked")
private static Iterable<IgniteConfiguration> getConfigurations(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);
    Collection<IgniteConfiguration> res = new ArrayList<>();
    for (IgniteConfiguration cfg : (Collection<IgniteConfiguration>) cfgMap.values()) {
        UUID nodeId = UUID.randomUUID();
        cfg.setNodeId(nodeId);
        cfg.setGridLogger(initLogger(log));
        res.add(cfg);
    }
    return res;
}
Also used : NullAppender(org.apache.log4j.varia.NullAppender) Appender(org.apache.log4j.Appender) ConsoleAppender(org.apache.log4j.ConsoleAppender) RollingFileAppender(org.apache.log4j.RollingFileAppender) MalformedURLException(java.net.MalformedURLException) NullAppender(org.apache.log4j.varia.NullAppender) ArrayList(java.util.ArrayList) FileSystemXmlApplicationContext(org.springframework.context.support.FileSystemXmlApplicationContext) FileSystemXmlApplicationContext(org.springframework.context.support.FileSystemXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) Collection(java.util.Collection) UUID(java.util.UUID) File(java.io.File) Map(java.util.Map) BeansException(org.springframework.beans.BeansException)

Example 40 with Appender

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

the class GridTestLog4jLogger 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);
        }
        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)

Aggregations

Appender (org.apache.log4j.Appender)73 FileAppender (org.apache.log4j.FileAppender)24 Logger (org.apache.log4j.Logger)20 File (java.io.File)12 ConsoleAppender (org.apache.log4j.ConsoleAppender)11 RollingFileAppender (org.apache.log4j.RollingFileAppender)11 WriterAppender (org.apache.log4j.WriterAppender)11 NullAppender (org.apache.log4j.varia.NullAppender)11 PatternLayout (org.apache.log4j.PatternLayout)9 Log4JLogger (org.apache.commons.logging.impl.Log4JLogger)8 Enumeration (java.util.Enumeration)7 Test (org.junit.Test)7 IOException (java.io.IOException)6 AsyncAppender (org.apache.log4j.AsyncAppender)6 Layout (org.apache.log4j.Layout)6 LogfileAppender (org.knime.core.util.LogfileAppender)6 DailyRollingFileAppender (org.apache.log4j.DailyRollingFileAppender)5 Filter (org.apache.log4j.spi.Filter)5 LevelRangeFilter (org.apache.log4j.varia.LevelRangeFilter)5 ActiveDbAppender (com.axway.ats.log.appenders.ActiveDbAppender)4