Search in sources :

Example 26 with LoggingEvent

use of org.apache.log4j.spi.LoggingEvent in project hadoop by apache.

the class FairSchedulerEventLog method log.

/**
   * Log an event, writing a line in the log file of the form
   * <pre>
   * DATE    EVENT_TYPE   PARAM_1   PARAM_2   ...
   * </pre>
   */
synchronized void log(String eventType, Object... params) {
    try {
        if (logDisabled)
            return;
        StringBuffer buffer = new StringBuffer();
        buffer.append(eventType);
        for (Object param : params) {
            buffer.append("\t");
            buffer.append(param);
        }
        String message = buffer.toString();
        Logger logger = Logger.getLogger(getClass());
        appender.append(new LoggingEvent("", logger, Level.INFO, message, null));
    } catch (Exception e) {
        LOG.error("Failed to append to fair scheduler event log", e);
        logDisabled = true;
    }
}
Also used : LoggingEvent(org.apache.log4j.spi.LoggingEvent) Logger(org.apache.log4j.Logger) IOException(java.io.IOException)

Example 27 with LoggingEvent

use of org.apache.log4j.spi.LoggingEvent in project zeppelin by apache.

the class HeliumBundleFactory method configureLogger.

private void configureLogger() {
    org.apache.log4j.Logger npmLogger = org.apache.log4j.Logger.getLogger("com.github.eirslett.maven.plugins.frontend.lib.DefaultNpmRunner");
    Enumeration appenders = org.apache.log4j.Logger.getRootLogger().getAllAppenders();
    if (appenders != null) {
        while (appenders.hasMoreElements()) {
            Appender appender = (Appender) appenders.nextElement();
            appender.addFilter(new Filter() {

                @Override
                public int decide(LoggingEvent loggingEvent) {
                    if (loggingEvent.getLoggerName().contains("DefaultNpmRunner")) {
                        return DENY;
                    } else {
                        return NEUTRAL;
                    }
                }
            });
        }
    }
    npmLogger.addAppender(new WriterAppender(new PatternLayout("%m%n"), out));
}
Also used : Appender(org.apache.log4j.Appender) WriterAppender(org.apache.log4j.WriterAppender) LoggingEvent(org.apache.log4j.spi.LoggingEvent) Filter(org.apache.log4j.spi.Filter) PatternLayout(org.apache.log4j.PatternLayout) WriterAppender(org.apache.log4j.WriterAppender)

Example 28 with LoggingEvent

use of org.apache.log4j.spi.LoggingEvent in project zeppelin by apache.

the class AppendOutputRunnerTest method testWarnLoggerForLargeData.

@Test
public void testWarnLoggerForLargeData() throws InterruptedException {
    RemoteInterpreterProcessListener listener = mock(RemoteInterpreterProcessListener.class);
    AppendOutputRunner runner = new AppendOutputRunner(listener);
    String data = "data\n";
    int numEvents = 100000;
    for (int i = 0; i < numEvents; i++) {
        runner.appendBuffer("noteId", "paraId", 0, data);
    }
    TestAppender appender = new TestAppender();
    Logger logger = Logger.getRootLogger();
    logger.addAppender(appender);
    Logger.getLogger(RemoteInterpreterEventPoller.class);
    runner.run();
    List<LoggingEvent> log;
    int warnLogCounter;
    LoggingEvent sizeWarnLogEntry = null;
    do {
        warnLogCounter = 0;
        log = appender.getLog();
        for (LoggingEvent logEntry : log) {
            if (Level.WARN.equals(logEntry.getLevel())) {
                sizeWarnLogEntry = logEntry;
                warnLogCounter += 1;
            }
        }
    } while (warnLogCounter != 2);
    String loggerString = "Processing size for buffered append-output is high: " + (data.length() * numEvents) + " characters.";
    assertTrue(loggerString.equals(sizeWarnLogEntry.getMessage()));
}
Also used : LoggingEvent(org.apache.log4j.spi.LoggingEvent) Logger(org.apache.log4j.Logger) Test(org.junit.Test)

Example 29 with LoggingEvent

use of org.apache.log4j.spi.LoggingEvent in project gocd by gocd.

the class LoggingHelper method createConsoleFileAppender.

public static Appender createConsoleFileAppender(final CONSOLE_NDC ndc, String consoleFileName) {
    Appender consoleAppender = createFileAppender(consoleFileName);
    consoleAppender.setName(ndc.name());
    consoleAppender.setLayout(LOG4J_CONSOLE_PATTERN);
    //Console NDC acceptance filter
    consoleAppender.addFilter(new org.apache.log4j.spi.Filter() {

        @Override
        public int decide(LoggingEvent event) {
            return event.getNDC() == ndc.toString() ? ACCEPT : DENY;
        }
    });
    addConsoleNDCLogRemoverFilter();
    return consoleAppender;
}
Also used : Appender(org.apache.log4j.Appender) RollingFileAppender(org.apache.log4j.RollingFileAppender) LoggingEvent(org.apache.log4j.spi.LoggingEvent) Filter(org.apache.log4j.spi.Filter)

Example 30 with LoggingEvent

use of org.apache.log4j.spi.LoggingEvent in project flink by apache.

the class YarnTestBase method runWithArgs.

/**
	 * The test has been passed once the "terminateAfterString" has been seen.
	 * @param args Command line arguments for the runner
	 * @param terminateAfterString the runner is searching the stdout and stderr for this string. as soon as it appears, the test has passed
	 * @param failOnPatterns The runner is searching stdout and stderr for the pattern (regexp) specified here. If one appears, the test has failed
	 * @param type Set the type of the runner
	 * @param expectedReturnValue Expected return code from the runner.
	 * @param checkLogForTerminateString  If true, the runner checks also the log4j logger for the terminate string
	 */
protected void runWithArgs(String[] args, String terminateAfterString, String[] failOnPatterns, RunTypes type, int expectedReturnValue, boolean checkLogForTerminateString) {
    LOG.info("Running with args {}", Arrays.toString(args));
    outContent = new ByteArrayOutputStream();
    errContent = new ByteArrayOutputStream();
    System.setOut(new PrintStream(outContent));
    System.setErr(new PrintStream(errContent));
    // we wait for at most three minutes
    final int START_TIMEOUT_SECONDS = 180;
    final long deadline = System.currentTimeMillis() + (START_TIMEOUT_SECONDS * 1000);
    Runner runner = new Runner(args, type, expectedReturnValue);
    runner.start();
    boolean expectedStringSeen = false;
    boolean testPassedFromLog4j = false;
    do {
        sleep(1000);
        String outContentString = outContent.toString();
        String errContentString = errContent.toString();
        if (failOnPatterns != null) {
            for (String failOnString : failOnPatterns) {
                Pattern pattern = Pattern.compile(failOnString);
                if (pattern.matcher(outContentString).find() || pattern.matcher(errContentString).find()) {
                    LOG.warn("Failing test. Output contained illegal string '" + failOnString + "'");
                    sendOutput();
                    // stopping runner.
                    runner.sendStop();
                    Assert.fail("Output contained illegal string '" + failOnString + "'");
                }
            }
        }
        // check output for the expected terminateAfterString.
        if (checkLogForTerminateString) {
            LoggingEvent matchedEvent = UtilsTest.getEventContainingString(terminateAfterString);
            if (matchedEvent != null) {
                testPassedFromLog4j = true;
                LOG.info("Found expected output in logging event {}", matchedEvent);
            }
        }
        if (outContentString.contains(terminateAfterString) || errContentString.contains(terminateAfterString) || testPassedFromLog4j) {
            expectedStringSeen = true;
            LOG.info("Found expected output in redirected streams");
            // send "stop" command to command line interface
            LOG.info("RunWithArgs: request runner to stop");
            runner.sendStop();
            // wait for the thread to stop
            try {
                runner.join(30000);
            } catch (InterruptedException e) {
                LOG.warn("Interrupted while stopping runner", e);
            }
            LOG.warn("RunWithArgs runner stopped.");
        } else {
            // check if thread died
            if (!runner.isAlive()) {
                // leave loop: the runner died, so we can not expect new strings to show up.
                break;
            }
        }
    } while (runner.getRunnerError() == null && !expectedStringSeen && System.currentTimeMillis() < deadline);
    sendOutput();
    if (runner.getRunnerError() != null) {
        // this lets the test fail.
        throw new RuntimeException("Runner failed", runner.getRunnerError());
    }
    Assert.assertTrue("During the timeout period of " + START_TIMEOUT_SECONDS + " seconds the " + "expected string did not show up", expectedStringSeen);
    LOG.info("Test was successful");
}
Also used : LoggingEvent(org.apache.log4j.spi.LoggingEvent) PrintStream(java.io.PrintStream) Pattern(java.util.regex.Pattern) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

LoggingEvent (org.apache.log4j.spi.LoggingEvent)48 Test (org.junit.Test)21 Logger (org.apache.log4j.Logger)12 Pattern (java.util.regex.Pattern)7 Matcher (java.util.regex.Matcher)6 ByteArrayOutputStream (org.apache.commons.io.output.ByteArrayOutputStream)6 AppenderSkeleton (org.apache.log4j.AppenderSkeleton)3 LoggingException (com.axway.ats.log.autodb.exceptions.LoggingException)2 AbstractLoggingEvent (com.axway.ats.log.autodb.model.AbstractLoggingEvent)2 IOException (java.io.IOException)2 Appender (org.apache.log4j.Appender)2 Category (org.apache.log4j.Category)2 Level (org.apache.log4j.Level)2 Filter (org.apache.log4j.spi.Filter)2 LocationInfo (org.apache.log4j.spi.LocationInfo)2 InsertMessageEvent (com.axway.ats.log.autodb.events.InsertMessageEvent)1 CacheableEvent (com.axway.ats.log.autodb.model.CacheableEvent)1 SystemLogLevel (com.axway.ats.log.model.SystemLogLevel)1 CurrentUser (com.google.gerrit.server.CurrentUser)1 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)1