當前位置: 首頁>>代碼示例>>Java>>正文


Java Logger.setAdditive方法代碼示例

本文整理匯總了Java中org.apache.logging.log4j.core.Logger.setAdditive方法的典型用法代碼示例。如果您正苦於以下問題:Java Logger.setAdditive方法的具體用法?Java Logger.setAdditive怎麽用?Java Logger.setAdditive使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.apache.logging.log4j.core.Logger的用法示例。


在下文中一共展示了Logger.setAdditive方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testServer

import org.apache.logging.log4j.core.Logger; //導入方法依賴的package包/類
protected void testServer(final String message1, final String message2) throws Exception {
    final Filter socketFilter = new ThreadFilter(Filter.Result.NEUTRAL, Filter.Result.DENY);
    final Filter serverFilter = new ThreadFilter(Filter.Result.DENY, Filter.Result.NEUTRAL);
    final SocketAppender appender = SocketAppender.createAppender("localhost", this.port, this.protocol, "-1", null, "Test", null,
            "false", null, socketFilter, null, null);
    appender.start();
    final ListAppender listApp = new ListAppender("Events", serverFilter, null, false, false);
    listApp.start();
    final PatternLayout layout = PatternLayout.createLayout("%m %ex%n", null, null, null, null);
    final ConsoleAppender console = ConsoleAppender.createAppender(layout, null, "SYSTEM_OUT", "Console", "false", "true");
    final Logger serverLogger = ctx.getLogger(this.getClass().getName());
    serverLogger.addAppender(console);
    serverLogger.setAdditive(false);

    // set appender on root and set level to debug
    root.addAppender(appender);
    root.addAppender(listApp);
    root.setAdditive(false);
    root.setLevel(Level.DEBUG);
    root.debug(message1);
    root.debug(message2);
    Thread.sleep(100);
    final List<LogEvent> events = listApp.getEvents();
    assertNotNull("No event retrieved", events);
    assertTrue("No events retrieved", events.size() > 0);
    assertTrue("Incorrect event", events.get(0).getMessage().getFormattedMessage().equals(message1));
    assertTrue("Incorrect number of events received: " + events.size(), events.size() == 2);
    assertTrue("Incorrect event", events.get(1).getMessage().getFormattedMessage().equals(message2));
}
 
開發者ID:OuZhencong,項目名稱:log4j2,代碼行數:30,代碼來源:AbstractSocketServerTest.java

示例2: testServer

import org.apache.logging.log4j.core.Logger; //導入方法依賴的package包/類
@Test
public void testServer() throws Exception {
    final Filter clientFilter = new MessageFilter(Filter.Result.NEUTRAL, Filter.Result.DENY);
    final Filter serverFilter = new MessageFilter(Filter.Result.DENY, Filter.Result.NEUTRAL);
    final CompositeFilter clientFilters = CompositeFilter.createFilters(new Filter[]{clientFilter});
    final JMSQueueAppender appender = JMSQueueAppender.createAppender("Test", null, null, null, null, null, FACTORY_NAME,
            QUEUE_NAME, null, null, null, clientFilters, "true");
    appender.start();
    final CompositeFilter serverFilters = CompositeFilter.createFilters(new Filter[]{serverFilter});
    final ListAppender listApp = new ListAppender("Events", serverFilters, null, false, false);
    listApp.start();
    final PatternLayout layout = PatternLayout.createLayout("%m %ex%n", null, null, null, null);
    final ConsoleAppender console = ConsoleAppender.createAppender(layout, null, "SYSTEM_OUT", "Console", "false", "true");
    console.start();
    final Logger serverLogger = ctx.getLogger(JMSTopicReceiver.class.getName());
    serverLogger.addAppender(console);
    serverLogger.setAdditive(false);


    // set appender on root and set level to debug
    root.addAppender(listApp);
    root.addAppender(appender);
    root.setAdditive(false);
    root.setLevel(Level.DEBUG);
    root.debug("This is a test message");
    Thread.sleep(100);
    final List<LogEvent> events = listApp.getEvents();
    assertNotNull("No event retrieved", events);
    assertTrue("No events retrieved", events.size() > 0);
    assertTrue("Incorrect event", events.get(0).getMessage().getFormattedMessage().equals("This is a test message"));
}
 
開發者ID:OuZhencong,項目名稱:log4j2,代碼行數:32,代碼來源:JMSQueueTest.java

示例3: testServer

import org.apache.logging.log4j.core.Logger; //導入方法依賴的package包/類
@Test
public void testServer() throws Exception {
    final Filter clientFilter = new MessageFilter(Filter.Result.NEUTRAL, Filter.Result.DENY);
    final Filter serverFilter = new MessageFilter(Filter.Result.DENY, Filter.Result.NEUTRAL);
    final CompositeFilter clientFilters = CompositeFilter.createFilters(new Filter[]{clientFilter});
    final JMSTopicAppender appender = JMSTopicAppender.createAppender("Test", null, null, null, null, null, FACTORY_NAME,
            TOPIC_NAME, null, null, null, clientFilters, "true");
    appender.start();
    final CompositeFilter serverFilters = CompositeFilter.createFilters(new Filter[]{serverFilter});
    final ListAppender listApp = new ListAppender("Events", serverFilters, null, false, false);
    listApp.start();
    final PatternLayout layout = PatternLayout.createLayout("%m %ex%n", null, null, null, null);
    final ConsoleAppender console =
            ConsoleAppender.createAppender(layout, null, "SYSTEM_OUT", "Console", "false", "true");
    console.start();
    final Logger serverLogger = ctx.getLogger(JMSTopicReceiver.class.getName());
    serverLogger.addAppender(console);
    serverLogger.setAdditive(false);


    // set appender on root and set level to debug
    root.addAppender(listApp);
    root.addAppender(appender);
    root.setAdditive(false);
    root.setLevel(Level.DEBUG);
    root.debug("This is a test message");
    Thread.sleep(100);
    final List<LogEvent> events = listApp.getEvents();
    assertNotNull("No event retrieved", events);
    assertTrue("No events retrieved", events.size() > 0);
    assertTrue("Incorrect event", events.get(0).getMessage().getFormattedMessage().equals("This is a test message"));
}
 
開發者ID:OuZhencong,項目名稱:log4j2,代碼行數:33,代碼來源:JMSTopicTest.java

示例4: testDelivery

import org.apache.logging.log4j.core.Logger; //導入方法依賴的package包/類
@Test
public void testDelivery() {
    final SMTPAppender appender = SMTPAppender.createAppender("Test",
            "[email protected]", "[email protected]", "[email protected]",
            "[email protected]", "[email protected]", "Subject", null,
            HOST, PORT, null, null, "false", "3", null, null, "true");
    appender.start();

    final LoggerContext context = (LoggerContext) LogManager.getContext();
    final Logger root = context.getLogger("SMTPAppenderTest");
    root.addAppender(appender);
    root.setAdditive(false);
    root.setLevel(Level.DEBUG);

    final SimpleSmtpServer server = SimpleSmtpServer.start(PORTNUM);

    root.debug("Debug message #1");
    root.debug("Debug message #2");
    root.debug("Debug message #3");
    root.debug("Debug message #4");
    root.error("Error with exception", new RuntimeException(
            "Exception message"));
    root.error("Error message #2");

    server.stop();
    assertTrue(server.getReceivedEmailSize() == 2);
    final Iterator<SmtpMessage> messages = server.getReceivedEmail();
    final SmtpMessage email = messages.next();

    assertEquals("[email protected]", email.getHeaderValue("To"));
    assertEquals("[email protected]", email.getHeaderValue("Cc"));
    // assertEquals("[email protected]", email.getHeaderValue("Bcc")); // BCC
    // can't be tested with Dumpster 1.6
    assertEquals("[email protected]", email.getHeaderValue("From"));
    assertEquals("[email protected]", email.getHeaderValue("Reply-To"));
    assertEquals("Subject", email.getHeaderValue("Subject"));

    final String body = email.getBody();
    assertFalse(body.contains("Debug message #1"));
    assertTrue(body.contains("Debug message #2"));
    assertTrue(body.contains("Debug message #3"));
    assertTrue(body.contains("Debug message #4"));
    assertTrue(body.contains("Error with exception"));
    assertTrue(body.contains("RuntimeException"));
    assertTrue(body.contains("Exception message"));
    assertFalse(body.contains("Error message #2"));

    final SmtpMessage email2 = (SmtpMessage) messages.next();
    final String body2 = email2.getBody();
    assertFalse(body2.contains("Debug message #4"));
    assertFalse(body2.contains("Error with exception"));
    assertTrue(body2.contains("Error message #2"));
}
 
開發者ID:OuZhencong,項目名稱:log4j2,代碼行數:54,代碼來源:SMTPAppenderTest.java

示例5: testDelivery

import org.apache.logging.log4j.core.Logger; //導入方法依賴的package包/類
@Test
public void testDelivery() {
    final String subjectKey = getClass().getName();
    final String subjectValue = "SubjectValue1";
    ThreadContext.put(subjectKey, subjectValue);
    final SmtpAppender appender = SmtpAppender.createAppender(null, "Test", "[email protected]", "[email protected]",
            "[email protected]", "[email protected]", "[email protected]", "Subject Pattern %X{" + subjectKey + "}",
            null, HOST, PORT, null, null, "false", "3", null, null, "true");
    appender.start();

    final LoggerContext context = LoggerContext.getContext();
    final Logger root = context.getLogger("SMTPAppenderTest");
    root.addAppender(appender);
    root.setAdditive(false);
    root.setLevel(Level.DEBUG);

    final SimpleSmtpServer server = SimpleSmtpServer.start(PORTNUM);

    root.debug("Debug message #1");
    root.debug("Debug message #2");
    root.debug("Debug message #3");
    root.debug("Debug message #4");
    root.error("Error with exception", new RuntimeException("Exception message"));
    root.error("Error message #2");

    server.stop();
    assertTrue(server.getReceivedEmailSize() == 2);
    final Iterator<SmtpMessage> messages = server.getReceivedEmail();
    final SmtpMessage email = messages.next();

    assertEquals("[email protected]", email.getHeaderValue("To"));
    assertEquals("[email protected]", email.getHeaderValue("Cc"));
    // assertEquals("[email protected]", email.getHeaderValue("Bcc")); // BCC
    // can't be tested with Dumpster 1.6
    assertEquals("[email protected]", email.getHeaderValue("From"));
    assertEquals("[email protected]", email.getHeaderValue("Reply-To"));
    final String headerValue = email.getHeaderValue("Subject");
    assertEquals(headerValue, "Subject Pattern " + subjectValue);

    final String body = email.getBody();
    assertFalse(body.contains("Debug message #1"));
    assertTrue(body.contains("Debug message #2"));
    assertTrue(body.contains("Debug message #3"));
    assertTrue(body.contains("Debug message #4"));
    assertTrue(body.contains("Error with exception"));
    assertTrue(body.contains("RuntimeException"));
    assertTrue(body.contains("Exception message"));
    assertFalse(body.contains("Error message #2"));

    final SmtpMessage email2 = messages.next();
    final String body2 = email2.getBody();
    assertFalse(body2.contains("Debug message #4"));
    assertFalse(body2.contains("Error with exception"));
    assertTrue(body2.contains("Error message #2"));
}
 
開發者ID:apache,項目名稱:logging-log4j2,代碼行數:56,代碼來源:SmtpAppenderTest.java

示例6: testTcpAppender

import org.apache.logging.log4j.core.Logger; //導入方法依賴的package包/類
static void testTcpAppender(final TcpSocketTestServer tcpTestServer, final Logger logger, final int bufferSize)
        throws Exception {
    // @formatter:off
    final SocketAppender appender = SocketAppender.newBuilder()
            .withHost("localhost")
            .withPort(tcpTestServer.getLocalPort())
            .withReconnectDelayMillis(-1)
            .withName("test")
            .withImmediateFail(false)
            .withBufferSize(bufferSize)
            .withLayout(JsonLayout.newBuilder().setProperties(true).build())
            .build();
    // @formatter:on
    appender.start();
    Assert.assertEquals(bufferSize, appender.getManager().getByteBuffer().capacity());

    // set appender on root and set level to debug
    logger.addAppender(appender);
    logger.setAdditive(false);
    logger.setLevel(Level.DEBUG);
    final String tcKey = "UUID";
    final String expectedUuidStr = UUID.randomUUID().toString();
    ThreadContext.put(tcKey, expectedUuidStr);
    ThreadContext.push(expectedUuidStr);
    final String expectedExMsg = "This is a test";
    try {
        logger.debug("This is a test message");
        final Throwable child = new LoggingException(expectedExMsg);
        logger.error("Throwing an exception", child);
        logger.debug("This is another test message");
    } finally {
        ThreadContext.remove(tcKey);
        ThreadContext.pop();
    }
    Thread.sleep(250);
    LogEvent event = tcpTestServer.getQueue().poll(3, TimeUnit.SECONDS);
    assertNotNull("No event retrieved", event);
    assertTrue("Incorrect event", event.getMessage().getFormattedMessage().equals("This is a test message"));
    assertTrue("Message not delivered via TCP", tcpTestServer.getCount() > 0);
    assertEquals(expectedUuidStr, event.getContextData().getValue(tcKey));
    event = tcpTestServer.getQueue().poll(3, TimeUnit.SECONDS);
    assertNotNull("No event retrieved", event);
    assertTrue("Incorrect event", event.getMessage().getFormattedMessage().equals("Throwing an exception"));
    assertTrue("Message not delivered via TCP", tcpTestServer.getCount() > 1);
    assertEquals(expectedUuidStr, event.getContextStack().pop());
    assertNotNull(event.getThrownProxy());
    assertEquals(expectedExMsg, event.getThrownProxy().getMessage());
}
 
開發者ID:apache,項目名稱:logging-log4j2,代碼行數:49,代碼來源:SocketAppenderTest.java


注:本文中的org.apache.logging.log4j.core.Logger.setAdditive方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。