Search in sources :

Example 6 with WriterAppender

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

the class QuorumPeerMainTest method testInconsistentPeerType.

/**
     * Verify handling of inconsistent peer type
     */
@Test
public void testInconsistentPeerType() 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.INFO);
    Logger qlogger = Logger.getLogger("org.apache.zookeeper.server.quorum");
    qlogger.addAppender(appender);
    // servers list, but there's no "peerType=observer" token in config
    try {
        final int CLIENT_PORT_QP1 = PortAssignment.unique();
        final int CLIENT_PORT_QP2 = PortAssignment.unique();
        final int CLIENT_PORT_QP3 = PortAssignment.unique();
        String quorumCfgSection = "server.1=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP1 + "\nserver.2=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP2 + "\nserver.3=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ":observer" + ";" + CLIENT_PORT_QP3;
        MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection);
        MainThread q2 = new MainThread(2, CLIENT_PORT_QP2, quorumCfgSection);
        MainThread q3 = new MainThread(3, CLIENT_PORT_QP3, quorumCfgSection);
        q1.start();
        q2.start();
        q3.start();
        Assert.assertTrue("waiting for server 1 being up", ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, CONNECTION_TIMEOUT));
        Assert.assertTrue("waiting for server 2 being up", ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP2, CONNECTION_TIMEOUT));
        Assert.assertTrue("waiting for server 3 being up", ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP3, CONNECTION_TIMEOUT));
        q1.shutdown();
        q2.shutdown();
        q3.shutdown();
        Assert.assertTrue("waiting for server 1 down", ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP1, ClientBase.CONNECTION_TIMEOUT));
        Assert.assertTrue("waiting for server 2 down", ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP2, ClientBase.CONNECTION_TIMEOUT));
        Assert.assertTrue("waiting for server 3 down", ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP3, ClientBase.CONNECTION_TIMEOUT));
    } finally {
        qlogger.removeAppender(appender);
    }
    LineNumberReader r = new LineNumberReader(new StringReader(os.toString()));
    String line;
    boolean warningPresent = false;
    boolean defaultedToObserver = false;
    Pattern pWarn = Pattern.compile(".*Peer type from servers list.* doesn't match peerType.*");
    Pattern pObserve = Pattern.compile(".*OBSERVING.*");
    while ((line = r.readLine()) != null) {
        if (pWarn.matcher(line).matches()) {
            warningPresent = true;
        }
        if (pObserve.matcher(line).matches()) {
            defaultedToObserver = true;
        }
        if (warningPresent && defaultedToObserver) {
            break;
        }
    }
    Assert.assertTrue("Should warn about inconsistent peer type", warningPresent && defaultedToObserver);
}
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 7 with WriterAppender

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

the class QuorumPeerMainTest method testQuorumDefaults.

/**
     * Verify handling of quorum defaults
     * * default electionAlg is fast leader election
     */
@Test
public void testQuorumDefaults() 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.setImmediateFlush(true);
    appender.setThreshold(Level.INFO);
    Logger zlogger = Logger.getLogger("org.apache.zookeeper");
    zlogger.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=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP2;
        MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection);
        MainThread q2 = new MainThread(2, CLIENT_PORT_QP2, quorumCfgSection);
        q1.start();
        q2.start();
        Assert.assertTrue("waiting for server 1 being up", ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, CONNECTION_TIMEOUT));
        Assert.assertTrue("waiting for server 2 being up", ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP2, CONNECTION_TIMEOUT));
        q1.shutdown();
        q2.shutdown();
        Assert.assertTrue("waiting for server 1 down", ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP1, ClientBase.CONNECTION_TIMEOUT));
        Assert.assertTrue("waiting for server 2 down", ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP2, ClientBase.CONNECTION_TIMEOUT));
    } finally {
        zlogger.removeAppender(appender);
    }
    os.close();
    LineNumberReader r = new LineNumberReader(new StringReader(os.toString()));
    String line;
    boolean found = false;
    Pattern p = Pattern.compile(".*FastLeaderElection.*");
    while ((line = r.readLine()) != null) {
        found = p.matcher(line).matches();
        if (found) {
            break;
        }
    }
    Assert.assertTrue("fastleaderelection used", 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 8 with WriterAppender

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

the class TestLog4Json method createLogger.

public Logger createLogger(Writer writer) {
    TestLoggerRepository repo = new TestLoggerRepository();
    Logger logger = repo.getLogger("test");
    Log4Json layout = new Log4Json();
    WriterAppender appender = new WriterAppender(layout, writer);
    logger.addAppender(appender);
    return logger;
}
Also used : WriterAppender(org.apache.log4j.WriterAppender) Logger(org.apache.log4j.Logger)

Example 9 with WriterAppender

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

the class TestYARNRunner method testWarnCommandOpts.

@Test(timeout = 20000)
public void testWarnCommandOpts() throws Exception {
    Logger logger = Logger.getLogger(YARNRunner.class);
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Layout layout = new SimpleLayout();
    Appender appender = new WriterAppender(layout, bout);
    logger.addAppender(appender);
    JobConf jobConf = new JobConf();
    jobConf.set(MRJobConfig.MR_AM_ADMIN_COMMAND_OPTS, "-Djava.net.preferIPv4Stack=true -Djava.library.path=foo");
    jobConf.set(MRJobConfig.MR_AM_COMMAND_OPTS, "-Xmx1024m -Djava.library.path=bar");
    YARNRunner yarnRunner = new YARNRunner(jobConf);
    @SuppressWarnings("unused") ApplicationSubmissionContext submissionContext = buildSubmitContext(yarnRunner, jobConf);
    String logMsg = bout.toString();
    assertTrue(logMsg.contains("WARN - Usage of -Djava.library.path in " + "yarn.app.mapreduce.am.admin-command-opts can cause programs to no " + "longer function if hadoop native libraries are used. These values " + "should be set as part of the LD_LIBRARY_PATH in the app master JVM " + "env using yarn.app.mapreduce.am.admin.user.env config settings."));
    assertTrue(logMsg.contains("WARN - Usage of -Djava.library.path in " + "yarn.app.mapreduce.am.command-opts can cause programs to no longer " + "function if hadoop native libraries are used. These values should " + "be set as part of the LD_LIBRARY_PATH in the app master JVM env " + "using yarn.app.mapreduce.am.env config settings."));
}
Also used : WriterAppender(org.apache.log4j.WriterAppender) Appender(org.apache.log4j.Appender) SimpleLayout(org.apache.log4j.SimpleLayout) SimpleLayout(org.apache.log4j.SimpleLayout) Layout(org.apache.log4j.Layout) ApplicationSubmissionContext(org.apache.hadoop.yarn.api.records.ApplicationSubmissionContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WriterAppender(org.apache.log4j.WriterAppender) Logger(org.apache.log4j.Logger) Test(org.junit.Test)

Example 10 with WriterAppender

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

the class GridNodeMetricsLogSelfTest method testNodeMetricsLog.

/**
     * @throws Exception If failed.
     */
public void testNodeMetricsLog() throws Exception {
    // Log to string, to check log content
    Layout layout = new SimpleLayout();
    StringWriter strWr = new StringWriter();
    WriterAppender app = new WriterAppender(layout, strWr);
    Logger.getRootLogger().addAppender(app);
    Ignite g1 = startGrid(1);
    IgniteCache<Integer, String> cache1 = g1.createCache("TestCache1");
    cache1.put(1, "one");
    Ignite g2 = startGrid(2);
    IgniteCache<Integer, String> cache2 = g2.createCache("TestCache2");
    cache2.put(2, "two");
    Thread.sleep(10_000);
    //Check that nodes are alie
    assertEquals("one", cache1.get(1));
    assertEquals("two", cache2.get(2));
    String fullLog = strWr.toString();
    Logger.getRootLogger().removeAppender(app);
    assert fullLog.contains("Metrics for local node");
    assert fullLog.contains("uptime=");
    assert fullLog.contains("Non heap");
    assert fullLog.contains("Outbound messages queue");
}
Also used : SimpleLayout(org.apache.log4j.SimpleLayout) StringWriter(java.io.StringWriter) SimpleLayout(org.apache.log4j.SimpleLayout) Layout(org.apache.log4j.Layout) Ignite(org.apache.ignite.Ignite) 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