Search in sources :

Example 1 with WriterAppender

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

the class TestJobMonitorAndPrint method testJobMonitorAndPrint.

@Test
public void testJobMonitorAndPrint() throws Exception {
    JobStatus jobStatus_1 = new JobStatus(new JobID("job_000", 1), 1f, 0.1f, 0.1f, 0f, State.RUNNING, JobPriority.HIGH, "tmp-user", "tmp-jobname", "tmp-queue", "tmp-jobfile", "tmp-url", true);
    JobStatus jobStatus_2 = new JobStatus(new JobID("job_000", 1), 1f, 1f, 1f, 1f, State.SUCCEEDED, JobPriority.HIGH, "tmp-user", "tmp-jobname", "tmp-queue", "tmp-jobfile", "tmp-url", true);
    doAnswer(new Answer<TaskCompletionEvent[]>() {

        @Override
        public TaskCompletionEvent[] answer(InvocationOnMock invocation) throws Throwable {
            return new TaskCompletionEvent[0];
        }
    }).when(job).getTaskCompletionEvents(anyInt(), anyInt());
    doReturn(new TaskReport[5]).when(job).getTaskReports(isA(TaskType.class));
    when(clientProtocol.getJobStatus(any(JobID.class))).thenReturn(jobStatus_1, jobStatus_2);
    // setup the logger to capture all logs
    Layout layout = Logger.getRootLogger().getAppender("stdout").getLayout();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    WriterAppender appender = new WriterAppender(layout, os);
    appender.setThreshold(Level.ALL);
    Logger qlogger = Logger.getLogger(Job.class);
    qlogger.addAppender(appender);
    job.monitorAndPrintJob();
    qlogger.removeAppender(appender);
    LineNumberReader r = new LineNumberReader(new StringReader(os.toString()));
    String line;
    boolean foundHundred = false;
    boolean foundComplete = false;
    boolean foundUber = false;
    String uberModeMatch = "uber mode : true";
    String progressMatch = "map 100% reduce 100%";
    String completionMatch = "completed successfully";
    while ((line = r.readLine()) != null) {
        if (line.contains(uberModeMatch)) {
            foundUber = true;
        }
        foundHundred = line.contains(progressMatch);
        if (foundHundred)
            break;
    }
    line = r.readLine();
    foundComplete = line.contains(completionMatch);
    assertTrue(foundUber);
    assertTrue(foundHundred);
    assertTrue(foundComplete);
    System.out.println("The output of job.toString() is : \n" + job.toString());
    assertTrue(job.toString().contains("Number of maps: 5\n"));
    assertTrue(job.toString().contains("Number of reduces: 5\n"));
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) WriterAppender(org.apache.log4j.WriterAppender) Logger(org.apache.log4j.Logger) LineNumberReader(java.io.LineNumberReader) Layout(org.apache.log4j.Layout) InvocationOnMock(org.mockito.invocation.InvocationOnMock) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 2 with WriterAppender

use of org.apache.log4j.WriterAppender in project zookeeper by apache.

the class QuorumPeerMainTest method testBadPeerAddressInQuorum.

/**
     * Verify handling of bad quorum address
     */
@Test
public void testBadPeerAddressInQuorum() throws Exception {
    ClientBase.setupTestEnv();
    // setup the logger to capture all logs
    Layout layout = Logger.getRootLogger().getAppender("CONSOLE").getLayout();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    WriterAppender appender = new WriterAppender(layout, os);
    appender.setThreshold(Level.WARN);
    Logger qlogger = Logger.getLogger("org.apache.zookeeper.server.quorum");
    qlogger.addAppender(appender);
    try {
        final int CLIENT_PORT_QP1 = PortAssignment.unique();
        final int CLIENT_PORT_QP2 = PortAssignment.unique();
        String quorumCfgSection = "server.1=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP1 + "\nserver.2=fee.fii.foo.fum:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP2;
        MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection);
        q1.start();
        boolean isup = ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, 30000);
        Assert.assertFalse("Server never came up", isup);
        q1.shutdown();
        Assert.assertTrue("waiting for server 1 down", ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP1, ClientBase.CONNECTION_TIMEOUT));
    } finally {
        qlogger.removeAppender(appender);
    }
    LineNumberReader r = new LineNumberReader(new StringReader(os.toString()));
    String line;
    boolean found = false;
    Pattern p = Pattern.compile(".*Cannot open channel to .* at election address .*");
    while ((line = r.readLine()) != null) {
        found = p.matcher(line).matches();
        if (found) {
            break;
        }
    }
    Assert.assertTrue("complains about host", found);
}
Also used : Pattern(java.util.regex.Pattern) Layout(org.apache.log4j.Layout) StringReader(java.io.StringReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WriterAppender(org.apache.log4j.WriterAppender) Logger(org.apache.log4j.Logger) LineNumberReader(java.io.LineNumberReader) Test(org.junit.Test)

Example 3 with WriterAppender

use of org.apache.log4j.WriterAppender in project zookeeper by apache.

the class ReadOnlyModeTest method testSeekForRwServer.

/**
     * Ensures that client seeks for r/w servers while it's connected to r/o
     * server.
     */
@SuppressWarnings("deprecation")
@Test(timeout = 90000)
public void testSeekForRwServer() throws Exception {
    // setup the logger to capture all logs
    Layout layout = Logger.getRootLogger().getAppender("CONSOLE").getLayout();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    WriterAppender appender = new WriterAppender(layout, os);
    appender.setImmediateFlush(true);
    appender.setThreshold(Level.INFO);
    Logger zlogger = Logger.getLogger("org.apache.zookeeper");
    zlogger.addAppender(appender);
    try {
        qu.shutdown(2);
        CountdownWatcher watcher = new CountdownWatcher();
        ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, watcher, true);
        watcher.waitForConnected(CONNECTION_TIMEOUT);
        // if we don't suspend a peer it will rejoin a quorum
        qu.getPeer(1).peer.suspend();
        // start two servers to form a quorum; client should detect this and
        // connect to one of them
        watcher.reset();
        qu.start(2);
        qu.start(3);
        ClientBase.waitForServerUp(qu.getConnString(), 2000);
        watcher.waitForConnected(CONNECTION_TIMEOUT);
        zk.create("/test", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        // resume poor fellow
        qu.getPeer(1).peer.resume();
    } finally {
        zlogger.removeAppender(appender);
    }
    os.close();
    LineNumberReader r = new LineNumberReader(new StringReader(os.toString()));
    String line;
    Pattern p = Pattern.compile(".*Majority server found.*");
    boolean found = false;
    while ((line = r.readLine()) != null) {
        if (p.matcher(line).matches()) {
            found = true;
            break;
        }
    }
    Assert.assertTrue("Majority server wasn't found while connected to r/o server", found);
}
Also used : Pattern(java.util.regex.Pattern) ZooKeeper(org.apache.zookeeper.ZooKeeper) Layout(org.apache.log4j.Layout) CountdownWatcher(org.apache.zookeeper.test.ClientBase.CountdownWatcher) StringReader(java.io.StringReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WriterAppender(org.apache.log4j.WriterAppender) Logger(org.apache.log4j.Logger) LineNumberReader(java.io.LineNumberReader) Test(org.junit.Test)

Example 4 with WriterAppender

use of org.apache.log4j.WriterAppender in project ddf by codice.

the class ReliableResourceInputStreamTest method testInputStreamReadRetry.

@Test
public void testInputStreamReadRetry() throws Exception {
    LOGGER.info("Testing testInputStreamReadTwice()");
    ReliableResourceInputStream is = new ReliableResourceInputStream(fbos, countingFbos, downloadState, downloadIdentifier, resourceResponse);
    is.setCallableAndItsFuture(reliableResourceCallable, downloadFuture);
    org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(is.getClass());
    logger.setLevel(Level.TRACE);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Layout layout = new SimpleLayout();
    Appender appender = new WriterAppender(layout, out);
    logger.addAppender(appender);
    //downloadState.setDownloadState(DownloadManagerState.DownloadState.IN_PROGRESS);
    try {
        // Write zero bytes to FileBackedOutputStream
        byte[] bytes = new String("").getBytes();
        countingFbos.write(bytes, 0, bytes.length);
        // Attempt to read from FileBackedOutputStream
        final byte[] buffer = new byte[50];
        int numBytesRead = is.read(buffer, 0, 50);
        // Verify bytes read is -1
        assertThat(numBytesRead, is(-1));
        // Verify read inputstream performed twice
        String logMsg = out.toString();
        assertThat(logMsg, is(notNullValue()));
        assertThat(logMsg, containsString("First time reading inputstream"));
    //assertThat(logMsg, containsString("Retry reading inputstream"));
    } finally {
        logger.removeAppender(appender);
    }
}
Also used : Appender(org.apache.log4j.Appender) WriterAppender(org.apache.log4j.WriterAppender) SimpleLayout(org.apache.log4j.SimpleLayout) SimpleLayout(org.apache.log4j.SimpleLayout) Layout(org.apache.log4j.Layout) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WriterAppender(org.apache.log4j.WriterAppender) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 5 with WriterAppender

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

Aggregations

WriterAppender (org.apache.log4j.WriterAppender)11 Layout (org.apache.log4j.Layout)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 Logger (org.apache.log4j.Logger)7 Test (org.junit.Test)7 LineNumberReader (java.io.LineNumberReader)5 StringReader (java.io.StringReader)5 Pattern (java.util.regex.Pattern)4 SimpleLayout (org.apache.log4j.SimpleLayout)4 Appender (org.apache.log4j.Appender)3 StringWriter (java.io.StringWriter)2 ApplicationSubmissionContext (org.apache.hadoop.yarn.api.records.ApplicationSubmissionContext)1 Ignite (org.apache.ignite.Ignite)1 PatternLayout (org.apache.log4j.PatternLayout)1 Filter (org.apache.log4j.spi.Filter)1 LoggingEvent (org.apache.log4j.spi.LoggingEvent)1 ZooKeeper (org.apache.zookeeper.ZooKeeper)1 CountdownWatcher (org.apache.zookeeper.test.ClientBase.CountdownWatcher)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Before (org.junit.Before)1