Java Code Examples for org.apache.log4j.WriterAppender#setThreshold()

The following examples show how to use org.apache.log4j.WriterAppender#setThreshold() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LogStreamServlet.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws IOException {

  WriterAppender appender =
      new WriterAppender(new PatternLayout(PATTERN), resp.getWriter());
  appender.setThreshold(Level.TRACE);

  try {
    Logger.getRootLogger().addAppender(appender);
    try {
      Thread.sleep(Integer.MAX_VALUE);
    } catch (InterruptedException e) {
      Thread.currentThread().interrupt();
    }
  } finally {
    Logger.getRootLogger().removeAppender(appender);
  }
}
 
Example 2
Source File: FbLog4jTest.java    From takes with MIT License 5 votes vote down vote up
/**
 * Helper method to set up stream.
 * @return ByteArrayOutputStream for logging
 */
private ByteArrayOutputStream setUpLoggerStream() {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final WriterAppender appender = new WriterAppender(
        new SimpleLayout(),
        baos
    );
    appender.setThreshold(Level.ERROR);
    appender.activateOptions();
    Logger.getRootLogger().addAppender(appender);
    return baos;
}
 
Example 3
Source File: TestJobMonitorAndPrint.java    From hadoop with Apache License 2.0 4 votes vote down vote up
@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"));
}
 
Example 4
Source File: NoKeyOnWireTest.java    From GDH with MIT License 4 votes vote down vote up
private void testNegotiation(int amount, TestContext context) {
    PrimaryVertex pv = new PrimaryVertex();
    GDHVertex[] verticles = new GDHVertex[amount];
    Configuration[] confs = new Configuration[amount];
    Writer writer = new StringWriter();
    for (int i = 0; i < amount; i++) {
        verticles[i] = new GDHVertex();
        confs[i] = new Configuration();
        WriterAppender app = new WriterAppender(new PatternLayout(), writer);
        app.setThreshold(Level.DEBUG);
        app.activateOptions();
        confs[i].setAppender(app);
        String port = amount + "08" + i;
        confs[i].setIP("localhost").setPort(port).setLogLevel(Level.DEBUG);
        verticles[i].setConfiguration(confs[i]);
    }
    List<GDHVertex> list = new ArrayList<>(Arrays.asList(verticles));

    Group g = new Group(confs[0], list.stream().map(y -> y.getNode()).collect(Collectors.toList()));
    verticles[0].addGroup(g);

    Async async1 = context.async(amount);
    for (int i = 0; i < amount; i++)
        pv.run(verticles[i], res -> {
            if (res.succeeded()) {
                async1.countDown();
            } else {
                res.cause().printStackTrace();
                return;
            }
        });
    async1.awaitSuccess();

    BigInteger[] keys = new BigInteger[2];
    try {
        keys[0] = verticles[0].exchange(g.getGroupId()).get();
        Assert.assertFalse(!writer.toString().isEmpty() && writer.toString().contains(keys[0].toString()));
    } catch (InterruptedException | ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Async async2 = context.async(amount);
    for (int i = 0; i < amount; i++)
        pv.kill(verticles[i], res -> {
            if (res.succeeded()) {
                async2.countDown();
            } else {
                res.cause().printStackTrace();
            }
        });
    async2.awaitSuccess();
}
 
Example 5
Source File: LoggerTest.java    From GDH with MIT License 4 votes vote down vote up
private void testNegotiation(int amount, TestContext context) {
    PrimaryVertex pv = new PrimaryVertex();
    GDHVertex[] verticles = new GDHVertex[amount];
    Configuration[] confs = new Configuration[amount];
    Writer writer = new StringWriter();
    for (int i = 0; i < amount; i++) {
        verticles[i] = new GDHVertex();
        confs[i] = new Configuration();
        WriterAppender app = new WriterAppender(new PatternLayout(), writer);
        app.setThreshold(Level.DEBUG);
        app.activateOptions();
        confs[i].setAppender(app);
        String port = amount + "08" + i;
        confs[i].setIP("localhost").setPort(port).setLogLevel(Level.DEBUG);
        verticles[i].setConfiguration(confs[i]);
    }
    List<GDHVertex> list = new ArrayList<>(Arrays.asList(verticles));

    Group g = new Group(confs[0], list.stream().map(y -> y.getNode()).collect(Collectors.toList()));
    verticles[0].addGroup(g);

    Async async1 = context.async(amount);
    for (int i = 0; i < amount; i++)
        pv.run(verticles[i], res -> {
            if (res.succeeded()) {
                async1.countDown();
            } else {
                res.cause().printStackTrace();
                return;
            }
        });
    async1.awaitSuccess();

    BigInteger[] keys = new BigInteger[1];
    try {
        keys[0] = verticles[0].exchange(g.getGroupId()).get();
        for (int j = 0; j < verticles.length; j++) {
            Assert.assertEquals(verticles[j].getKey(g.getGroupId()).get(), keys[0]);
        }
        String write = writer.toString();
        int count1 = write.length() - write.replace(Constants.LOG_IN, "0000000").length();
        int count2 = write.length() - write.replace(Constants.LOG_OUT, "0000000").length();
        Assert.assertTrue(count1 >= amount*amount-1);
        Assert.assertTrue(count2 >= amount*amount-1);
    } catch (InterruptedException | ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Async async2 = context.async(amount);
    for (int i = 0; i < amount; i++)
        pv.kill(verticles[i], res -> {
            if (res.succeeded()) {
                async2.countDown();
            } else {
                res.cause().printStackTrace();
            }
        });
    async2.awaitSuccess();
}
 
Example 6
Source File: TestJobMonitorAndPrint.java    From big-c with Apache License 2.0 4 votes vote down vote up
@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"));
}