Search in sources :

Example 1 with Log4JLogger

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

the class HttpRequestLog method getRequestLog.

public static RequestLog getRequestLog(String name) {
    String lookup = serverToComponent.get(name);
    if (lookup != null) {
        name = lookup;
    }
    String loggerName = "http.requests." + name;
    String appenderName = name + "requestlog";
    Log logger = LogFactory.getLog(loggerName);
    boolean isLog4JLogger;
    ;
    try {
        isLog4JLogger = logger instanceof Log4JLogger;
    } catch (NoClassDefFoundError err) {
        // In some dependent projects, log4j may not even be on the classpath at
        // runtime, in which case the above instanceof check will throw
        // NoClassDefFoundError.
        LOG.debug("Could not load Log4JLogger class", err);
        isLog4JLogger = false;
    }
    if (isLog4JLogger) {
        Log4JLogger httpLog4JLog = (Log4JLogger) logger;
        Logger httpLogger = httpLog4JLog.getLogger();
        Appender appender = null;
        try {
            appender = httpLogger.getAppender(appenderName);
        } catch (LogConfigurationException e) {
            LOG.warn("Http request log for " + loggerName + " could not be created");
            throw e;
        }
        if (appender == null) {
            LOG.info("Http request log for " + loggerName + " is not defined");
            return null;
        }
        if (appender instanceof HttpRequestLogAppender) {
            HttpRequestLogAppender requestLogAppender = (HttpRequestLogAppender) appender;
            NCSARequestLog requestLog = new NCSARequestLog();
            requestLog.setFilename(requestLogAppender.getFilename());
            requestLog.setRetainDays(requestLogAppender.getRetainDays());
            return requestLog;
        } else {
            LOG.warn("Jetty request log for " + loggerName + " was of the wrong class");
            return null;
        }
    } else {
        LOG.warn("Jetty request log can only be enabled using Log4j");
        return null;
    }
}
Also used : Appender(org.apache.log4j.Appender) NCSARequestLog(org.eclipse.jetty.server.NCSARequestLog) RequestLog(org.eclipse.jetty.server.RequestLog) Log(org.apache.commons.logging.Log) Log4JLogger(org.apache.commons.logging.impl.Log4JLogger) NCSARequestLog(org.eclipse.jetty.server.NCSARequestLog) LogConfigurationException(org.apache.commons.logging.LogConfigurationException) Logger(org.apache.log4j.Logger) Log4JLogger(org.apache.commons.logging.impl.Log4JLogger)

Example 2 with Log4JLogger

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

the class FSNamesystem method enableAsyncAuditLog.

private static void enableAsyncAuditLog() {
    if (!(auditLog instanceof Log4JLogger)) {
        LOG.warn("Log4j is required to enable async auditlog");
        return;
    }
    Logger logger = ((Log4JLogger) auditLog).getLogger();
    @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 : AsyncAppender(org.apache.log4j.AsyncAppender) Appender(org.apache.log4j.Appender) AsyncAppender(org.apache.log4j.AsyncAppender) Log4JLogger(org.apache.commons.logging.impl.Log4JLogger) TopAuditLogger(org.apache.hadoop.hdfs.server.namenode.top.TopAuditLogger) Logger(org.apache.log4j.Logger) Log4JLogger(org.apache.commons.logging.impl.Log4JLogger)

Example 3 with Log4JLogger

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

the class TestFsck method verifyAuditLogs.

private void verifyAuditLogs() throws IOException {
    // Turn off the logs
    Logger logger = ((Log4JLogger) FSNamesystem.auditLog).getLogger();
    logger.setLevel(Level.OFF);
    BufferedReader reader = null;
    try {
        // Audit log should contain one getfileinfo and one fsck
        reader = new BufferedReader(new FileReader(AUDITLOG_FILE));
        String line;
        //
        for (int i = 0; i < 2; i++) {
            line = reader.readLine();
            assertNotNull(line);
            assertTrue("Expected getfileinfo event not found in audit log", GET_FILE_INFO_PATTERN.matcher(line).matches());
        }
        line = reader.readLine();
        assertNotNull(line);
        assertTrue("Expected fsck event not found in audit log", FSCK_PATTERN.matcher(line).matches());
        assertNull("Unexpected event in audit log", reader.readLine());
    } finally {
        // handle after verifying the content of the file.
        if (reader != null) {
            reader.close();
        }
        if (logger != null) {
            logger.removeAllAppenders();
        }
    }
}
Also used : Log4JLogger(org.apache.commons.logging.impl.Log4JLogger) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Matchers.anyString(org.mockito.Matchers.anyString) Logger(org.apache.log4j.Logger) Log4JLogger(org.apache.commons.logging.impl.Log4JLogger)

Example 4 with Log4JLogger

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

the class TestFsck method setupAuditLogs.

/** Sets up log4j logger for auditlogs. */
private void setupAuditLogs() throws IOException {
    File file = new File(AUDITLOG_FILE);
    if (file.exists()) {
        file.delete();
    }
    Logger logger = ((Log4JLogger) FSNamesystem.auditLog).getLogger();
    logger.setLevel(Level.INFO);
    PatternLayout layout = new PatternLayout("%m%n");
    RollingFileAppender appender = new RollingFileAppender(layout, AUDITLOG_FILE);
    logger.addAppender(appender);
}
Also used : Log4JLogger(org.apache.commons.logging.impl.Log4JLogger) RollingFileAppender(org.apache.log4j.RollingFileAppender) PatternLayout(org.apache.log4j.PatternLayout) Logger(org.apache.log4j.Logger) Log4JLogger(org.apache.commons.logging.impl.Log4JLogger) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 5 with Log4JLogger

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

the class TestAuditLogs method setupCluster.

@Before
public void setupCluster() throws Exception {
    // must configure prior to instantiating the namesystem because it
    // will reconfigure the logger if async is enabled
    configureAuditLogs();
    conf = new HdfsConfiguration();
    final long precision = 1L;
    conf.setLong(DFSConfigKeys.DFS_NAMENODE_ACCESSTIME_PRECISION_KEY, precision);
    conf.setLong(DFSConfigKeys.DFS_BLOCKREPORT_INTERVAL_MSEC_KEY, 10000L);
    conf.setBoolean(DFSConfigKeys.DFS_NAMENODE_AUDIT_LOG_ASYNC_KEY, useAsyncLog);
    conf.setBoolean(DFSConfigKeys.DFS_NAMENODE_EDITS_ASYNC_LOGGING, useAsyncEdits);
    util = new DFSTestUtil.Builder().setName("TestAuditAllowed").setNumFiles(20).build();
    cluster = new MiniDFSCluster.Builder(conf).numDataNodes(4).build();
    fs = cluster.getFileSystem();
    util.createFiles(fs, fileName);
    // make sure the appender is what it's supposed to be
    Logger logger = ((Log4JLogger) FSNamesystem.auditLog).getLogger();
    @SuppressWarnings("unchecked") List<Appender> appenders = Collections.list(logger.getAllAppenders());
    assertEquals(1, appenders.size());
    assertEquals(useAsyncLog, appenders.get(0) instanceof AsyncAppender);
    fnames = util.getFileNames(fileName);
    util.waitReplication(fs, fileName, (short) 3);
    userGroupInfo = UserGroupInformation.createUserForTesting(username, groups);
}
Also used : Appender(org.apache.log4j.Appender) AsyncAppender(org.apache.log4j.AsyncAppender) RollingFileAppender(org.apache.log4j.RollingFileAppender) MiniDFSCluster(org.apache.hadoop.hdfs.MiniDFSCluster) AsyncAppender(org.apache.log4j.AsyncAppender) Log4JLogger(org.apache.commons.logging.impl.Log4JLogger) HdfsConfiguration(org.apache.hadoop.hdfs.HdfsConfiguration) Log4JLogger(org.apache.commons.logging.impl.Log4JLogger) Logger(org.apache.log4j.Logger) Before(org.junit.Before)

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