Search in sources :

Example 6 with Log4JLogger

use of org.apache.commons.logging.impl.Log4JLogger in project hadoop by apache.

the class TestAuditLogs method verifyAuditLogsRepeat.

// Ensure audit log has exactly N entries
private void verifyAuditLogsRepeat(boolean expectSuccess, int ndupe) throws IOException {
    // Turn off the logs
    Logger logger = ((Log4JLogger) FSNamesystem.auditLog).getLogger();
    logger.setLevel(Level.OFF);
    // Close the appenders and force all logs to be flushed
    Enumeration<?> appenders = logger.getAllAppenders();
    while (appenders.hasMoreElements()) {
        Appender appender = (Appender) appenders.nextElement();
        appender.close();
    }
    BufferedReader reader = new BufferedReader(new FileReader(auditLogFile));
    String line = null;
    boolean ret = true;
    try {
        for (int i = 0; i < ndupe; i++) {
            line = reader.readLine();
            assertNotNull(line);
            assertTrue("Expected audit event not found in audit log", auditPattern.matcher(line).matches());
            ret &= successPattern.matcher(line).matches();
        }
        assertNull("Unexpected event in audit log", reader.readLine());
        assertTrue("Expected success=" + expectSuccess, ret == expectSuccess);
    } finally {
        reader.close();
    }
}
Also used : Appender(org.apache.log4j.Appender) AsyncAppender(org.apache.log4j.AsyncAppender) RollingFileAppender(org.apache.log4j.RollingFileAppender) Log4JLogger(org.apache.commons.logging.impl.Log4JLogger) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Log4JLogger(org.apache.commons.logging.impl.Log4JLogger) Logger(org.apache.log4j.Logger)

Example 7 with Log4JLogger

use of org.apache.commons.logging.impl.Log4JLogger in project hadoop by apache.

the class TestAuditLogs method verifyAuditLogsCheckPattern.

// Ensure audit log has exactly N entries
private void verifyAuditLogsCheckPattern(boolean expectSuccess, int ndupe, Pattern pattern) throws IOException {
    // Turn off the logs
    Logger logger = ((Log4JLogger) FSNamesystem.auditLog).getLogger();
    logger.setLevel(Level.OFF);
    // Close the appenders and force all logs to be flushed
    Enumeration<?> appenders = logger.getAllAppenders();
    while (appenders.hasMoreElements()) {
        Appender appender = (Appender) appenders.nextElement();
        appender.close();
    }
    BufferedReader reader = new BufferedReader(new FileReader(auditLogFile));
    String line = null;
    boolean ret = true;
    boolean patternMatches = false;
    try {
        for (int i = 0; i < ndupe; i++) {
            line = reader.readLine();
            assertNotNull(line);
            patternMatches |= pattern.matcher(line).matches();
            ret &= successPattern.matcher(line).matches();
        }
        assertNull("Unexpected event in audit log", reader.readLine());
        assertTrue("Expected audit event not found in audit log", patternMatches);
        assertTrue("Expected success=" + expectSuccess, ret == expectSuccess);
    } finally {
        reader.close();
    }
}
Also used : Appender(org.apache.log4j.Appender) AsyncAppender(org.apache.log4j.AsyncAppender) RollingFileAppender(org.apache.log4j.RollingFileAppender) Log4JLogger(org.apache.commons.logging.impl.Log4JLogger) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Log4JLogger(org.apache.commons.logging.impl.Log4JLogger) Logger(org.apache.log4j.Logger)

Example 8 with Log4JLogger

use of org.apache.commons.logging.impl.Log4JLogger in project hadoop by apache.

the class TestAuditLogs method configureAuditLogs.

private void configureAuditLogs() throws IOException {
    // Shutdown the LogManager to release all logger open file handles.
    // Unfortunately, Apache commons logging library does not provide
    // means to release underlying loggers. For additional info look up
    // commons library FAQ.
    LogManager.shutdown();
    File file = new File(auditLogFile);
    if (file.exists()) {
        assertTrue(file.delete());
    }
    Logger logger = ((Log4JLogger) FSNamesystem.auditLog).getLogger();
    // disable logging while the cluster startup preps files
    logger.setLevel(Level.OFF);
    PatternLayout layout = new PatternLayout("%m%n");
    RollingFileAppender appender = new RollingFileAppender(layout, auditLogFile);
    logger.addAppender(appender);
}
Also used : Log4JLogger(org.apache.commons.logging.impl.Log4JLogger) RollingFileAppender(org.apache.log4j.RollingFileAppender) PatternLayout(org.apache.log4j.PatternLayout) Log4JLogger(org.apache.commons.logging.impl.Log4JLogger) Logger(org.apache.log4j.Logger) File(java.io.File)

Example 9 with Log4JLogger

use of org.apache.commons.logging.impl.Log4JLogger in project hadoop by apache.

the class MetricsLoggerTask method makeMetricsLoggerAsync.

/**
   * Make the metrics logger async and add all pre-existing appenders to the
   * async appender.
   */
public static void makeMetricsLoggerAsync(Log metricsLog) {
    if (!(metricsLog instanceof Log4JLogger)) {
        LOG.warn("Metrics logging will not be async since " + "the logger is not log4j");
        return;
    }
    org.apache.log4j.Logger logger = ((Log4JLogger) metricsLog).getLogger();
    // Don't pollute actual logs with metrics dump
    logger.setAdditivity(false);
    @SuppressWarnings("unchecked") List<Appender> appenders = Collections.list(logger.getAllAppenders());
    // failsafe against trying to async it more than once
    if (!appenders.isEmpty() && !(appenders.get(0) instanceof AsyncAppender)) {
        AsyncAppender asyncAppender = new AsyncAppender();
        // previously configured appenders
        for (Appender appender : appenders) {
            logger.removeAppender(appender);
            asyncAppender.addAppender(appender);
        }
        logger.addAppender(asyncAppender);
    }
}
Also used : Appender(org.apache.log4j.Appender) AsyncAppender(org.apache.log4j.AsyncAppender) AsyncAppender(org.apache.log4j.AsyncAppender) Log4JLogger(org.apache.commons.logging.impl.Log4JLogger)

Example 10 with Log4JLogger

use of org.apache.commons.logging.impl.Log4JLogger in project hadoop by apache.

the class TestAdHocLogDumper method testDumpingSchedulerLogs.

@Test
public void testDumpingSchedulerLogs() throws Exception {
    Map<Appender, Priority> levels = new HashMap<>();
    String logHierarchy = TestAdHocLogDumper.class.getName();
    String logFilename = "test.log";
    Log log = LogFactory.getLog(logHierarchy);
    if (log instanceof Log4JLogger) {
        for (Enumeration appenders = Logger.getRootLogger().getAllAppenders(); appenders.hasMoreElements(); ) {
            Object obj = appenders.nextElement();
            if (obj instanceof AppenderSkeleton) {
                AppenderSkeleton appender = (AppenderSkeleton) obj;
                levels.put(appender, appender.getThreshold());
            }
        }
    }
    AdHocLogDumper dumper = new AdHocLogDumper(logHierarchy, logFilename);
    dumper.dumpLogs("DEBUG", 1000);
    LOG.debug("test message 1");
    LOG.info("test message 2");
    File logFile = new File(logFilename);
    Assert.assertTrue(logFile.exists());
    Thread.sleep(2000);
    long lastWrite = logFile.lastModified();
    Assert.assertTrue(lastWrite < Time.now());
    Assert.assertTrue(logFile.length() != 0);
    // make sure levels are set back to their original values
    if (log instanceof Log4JLogger) {
        for (Enumeration appenders = Logger.getRootLogger().getAllAppenders(); appenders.hasMoreElements(); ) {
            Object obj = appenders.nextElement();
            if (obj instanceof AppenderSkeleton) {
                AppenderSkeleton appender = (AppenderSkeleton) obj;
                Assert.assertEquals(levels.get(appender), appender.getThreshold());
            }
        }
    }
    boolean del = logFile.delete();
    if (!del) {
        LOG.info("Couldn't clean up after test");
    }
}
Also used : Appender(org.apache.log4j.Appender) AppenderSkeleton(org.apache.log4j.AppenderSkeleton) Enumeration(java.util.Enumeration) HashMap(java.util.HashMap) Log(org.apache.commons.logging.Log) Priority(org.apache.log4j.Priority) Log4JLogger(org.apache.commons.logging.impl.Log4JLogger) File(java.io.File) Test(org.junit.Test)

Aggregations

Log4JLogger (org.apache.commons.logging.impl.Log4JLogger)21 Logger (org.apache.log4j.Logger)10 Log (org.apache.commons.logging.Log)9 Appender (org.apache.log4j.Appender)8 AsyncAppender (org.apache.log4j.AsyncAppender)5 RollingFileAppender (org.apache.log4j.RollingFileAppender)5 File (java.io.File)4 Log4jWarningErrorMetricsAppender (org.apache.hadoop.yarn.util.Log4jWarningErrorMetricsAppender)4 BufferedReader (java.io.BufferedReader)3 FileReader (java.io.FileReader)3 Hamlet (org.apache.hadoop.yarn.webapp.hamlet.Hamlet)3 Test (org.junit.Test)3 HashMap (java.util.HashMap)2 LogConfigurationException (org.apache.commons.logging.LogConfigurationException)2 Configuration (org.apache.hadoop.conf.Configuration)2 LogCapturer (org.apache.hadoop.test.GenericTestUtils.LogCapturer)2 PatternLayout (org.apache.log4j.PatternLayout)2 NCSARequestLog (org.eclipse.jetty.server.NCSARequestLog)2 RequestLog (org.eclipse.jetty.server.RequestLog)2 Before (org.junit.Before)2