Search in sources :

Example 1 with PatternLayout

use of org.apache.log4j.PatternLayout in project hadoop by apache.

the class TestTaskLogAppender method testTaskLogAppender.

/**
 * test TaskLogAppender 
 */
@SuppressWarnings("deprecation")
@Test(timeout = 5000)
public void testTaskLogAppender() {
    TaskLogAppender appender = new TaskLogAppender();
    System.setProperty(TaskLogAppender.TASKID_PROPERTY, "attempt_01_02_m03_04_001");
    System.setProperty(TaskLogAppender.LOGSIZE_PROPERTY, "1003");
    appender.activateOptions();
    assertEquals(appender.getTaskId(), "attempt_01_02_m03_04_001");
    assertEquals(appender.getTotalLogFileSize(), 1000);
    assertEquals(appender.getIsCleanup(), false);
    // test writer   
    Writer writer = new StringWriter();
    appender.setWriter(writer);
    Layout layout = new PatternLayout("%-5p [%t]: %m%n");
    appender.setLayout(layout);
    Category logger = Logger.getLogger(getClass().getName());
    LoggingEvent event = new LoggingEvent("fqnOfCategoryClass", logger, Priority.INFO, "message", new Throwable());
    appender.append(event);
    appender.flush();
    appender.close();
    assertTrue(writer.toString().length() > 0);
    // test cleanup should not changed 
    appender = new TaskLogAppender();
    appender.setIsCleanup(true);
    appender.activateOptions();
    assertEquals(appender.getIsCleanup(), true);
}
Also used : LoggingEvent(org.apache.log4j.spi.LoggingEvent) Category(org.apache.log4j.Category) StringWriter(java.io.StringWriter) Layout(org.apache.log4j.Layout) PatternLayout(org.apache.log4j.PatternLayout) PatternLayout(org.apache.log4j.PatternLayout) StringWriter(java.io.StringWriter) Writer(java.io.Writer) Test(org.junit.Test)

Example 2 with PatternLayout

use of org.apache.log4j.PatternLayout in project hadoop by apache.

the class FairSchedulerEventLog method init.

boolean init(FairSchedulerConfiguration conf) {
    if (conf.isEventLogEnabled()) {
        try {
            logDir = conf.getEventlogDir();
            File logDirFile = new File(logDir);
            if (!logDirFile.exists()) {
                if (!logDirFile.mkdirs()) {
                    throw new IOException("Mkdirs failed to create " + logDirFile.toString());
                }
            }
            String username = System.getProperty("user.name");
            logFile = String.format("%s%shadoop-%s-fairscheduler.log", logDir, File.separator, username);
            logDisabled = false;
            PatternLayout layout = new PatternLayout("%d{ISO8601}\t%m%n");
            appender = new DailyRollingFileAppender(layout, logFile, "'.'yyyy-MM-dd");
            appender.activateOptions();
            LOG.info("Initialized fair scheduler event log, logging to " + logFile);
        } catch (IOException e) {
            LOG.error("Failed to initialize fair scheduler event log. Disabling it.", e);
            logDisabled = true;
        }
    } else {
        logDisabled = true;
    }
    return !(logDisabled);
}
Also used : PatternLayout(org.apache.log4j.PatternLayout) IOException(java.io.IOException) File(java.io.File) DailyRollingFileAppender(org.apache.log4j.DailyRollingFileAppender)

Example 3 with PatternLayout

use of org.apache.log4j.PatternLayout 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 4 with PatternLayout

use of org.apache.log4j.PatternLayout 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 PatternLayout

use of org.apache.log4j.PatternLayout in project cpsolver by UniTime.

the class Test method setupLogging.

/**
     * Setup log4j logging
     * 
     * @param logFile  log file
     * @param debug true if debug messages should be logged (use -Ddebug=true to enable debug message)
     */
public static void setupLogging(File logFile, boolean debug) {
    Logger root = Logger.getRootLogger();
    ConsoleAppender console = new ConsoleAppender(new PatternLayout("[%t] %m%n"));
    console.setThreshold(Level.INFO);
    root.addAppender(console);
    if (logFile != null) {
        try {
            FileAppender file = new FileAppender(new PatternLayout("%d{dd-MMM-yy HH:mm:ss.SSS} [%t] %-5p %c{2}> %m%n"), logFile.getPath(), false);
            file.setThreshold(Level.DEBUG);
            root.addAppender(file);
        } catch (IOException e) {
            sLogger.fatal("Unable to configure logging, reason: " + e.getMessage(), e);
        }
    }
    if (!debug)
        root.setLevel(Level.INFO);
}
Also used : ConsoleAppender(org.apache.log4j.ConsoleAppender) FileAppender(org.apache.log4j.FileAppender) PatternLayout(org.apache.log4j.PatternLayout) IOException(java.io.IOException) Logger(org.apache.log4j.Logger)

Aggregations

PatternLayout (org.apache.log4j.PatternLayout)102 ConsoleAppender (org.apache.log4j.ConsoleAppender)51 Logger (org.apache.log4j.Logger)27 IOException (java.io.IOException)21 Appender (org.apache.log4j.Appender)18 FileAppender (org.apache.log4j.FileAppender)17 Layout (org.apache.log4j.Layout)15 File (java.io.File)14 RollingFileAppender (org.apache.log4j.RollingFileAppender)13 Test (org.junit.Test)8 CommandLineParser (org.apache.commons.cli.CommandLineParser)7 GnuParser (org.apache.commons.cli.GnuParser)7 ParseException (org.apache.commons.cli.ParseException)7 FileInputStream (java.io.FileInputStream)6 Properties (java.util.Properties)6 CommandLine (org.apache.commons.cli.CommandLine)6 Option (org.apache.commons.cli.Option)6 Options (org.apache.commons.cli.Options)6 DailyRollingFileAppender (org.apache.log4j.DailyRollingFileAppender)6 WriterAppender (org.apache.log4j.WriterAppender)6