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


Java Logger.error方法代碼示例

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


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

示例1: integrationTestAfter5

import org.apache.logging.log4j.core.Logger; //導入方法依賴的package包/類
/**
 * Integration test that asserts events are sent to the Elastic cluster in bulk.
 *
 * @throws IOException Signals that an I/O exception has occurred.
 */
@Test
public void integrationTestAfter5() throws IOException {
	String test = "integration-test-after-5";
	Logger logger = getLogger(test);
	ElasticClient client = new ElasticClient(logger);
	String marker = getUniqueMarker();

	// Should not send until it reaches 5 events
	for (int i = 1; i < 5; i++) {
		logger.error(MarkerManager.getMarker(marker), i + "-" + test);
	}
	JsonNode doc = client.findFirstByMarker(marker);
	assertNull(doc);
	
	// But after the 5th event it should send all buffered events
	logger.error(MarkerManager.getMarker(marker), "5-" + test);
	JsonNode hits = client.findAllByMarker(marker);
	assertNotNull(hits);
	assertEquals(5, hits.size());
	for (int i = 0; i < 5; i++) {
		assertFieldValue(hits.get(i).get("_source"), "level", "ERROR");
		assertFieldValue(hits.get(i).get("_source"), "message", (i + 1) + "-" + test);
	}
}
 
開發者ID:magrossi,項目名稱:es-log4j2-appender,代碼行數:30,代碼來源:ElasticSearchRestAppenderTest.java

示例2: testSmtpAppender

import org.apache.logging.log4j.core.Logger; //導入方法依賴的package包/類
private void testSmtpAppender(Logger logger) {
    ThreadContext.put("MDC1", "mdc1");
    logger.error("the message");
    ctx.getLoggerContext().stop();
    smtpServer.stop();

    assertEquals(1, smtpServer.getReceivedEmailSize());
    final Iterator<SmtpMessage> messages = smtpServer.getReceivedEmail();
    final SmtpMessage email = messages.next();

    assertEquals("[email protected]", email.getHeaderValue("To"));
    assertEquals("[email protected]", email.getHeaderValue("From"));
    assertEquals("[mdc1]", email.getHeaderValue("Subject"));

    String body = email.getBody();
    if (!body.contains("Body:[mdc1]")) {
        fail(body);
    }
}
 
開發者ID:apache,項目名稱:logging-log4j2,代碼行數:20,代碼來源:SmtpAppenderAsyncTest.java

示例3: testNoNulCharacters

import org.apache.logging.log4j.core.Logger; //導入方法依賴的package包/類
public void testNoNulCharacters(final String message, final String expected) throws IOException {
    @SuppressWarnings("resource")
    final LoggerContext loggerContext = loggerContextRule.getLoggerContext();
    final Logger logger = loggerContext.getLogger("com.example");
    logger.error("log:", message);
    loggerContext.stop();
    final File file = new File(FILE_PATH);
    final byte[] contents = FileUtils.readFileToByteArray(file);
    int count0s = 0;
    final StringBuilder sb = new StringBuilder();
    for (int i = 0; i < contents.length; i++) {
        final byte b = contents[i];
        if (b == 0) {
            sb.append(i);
            sb.append(", ");
            count0s++;
        }
    }
    Assert.assertEquals("File contains " + count0s + " 0x00 byte at indices " + sb, 0, count0s);
    final List<String> readLines = FileUtils.readLines(file, Charset.defaultCharset());
    final String actual = readLines.get(0);
    // Assert.assertTrue(actual, actual.contains(message));
    Assert.assertEquals(actual, expected, actual);
    Assert.assertEquals(1, readLines.size());
}
 
開發者ID:apache,項目名稱:logging-log4j2,代碼行數:26,代碼來源:CsvJsonParameterLayoutFileAppenderTest.java

示例4: testLogJsonArgument

import org.apache.logging.log4j.core.Logger; //導入方法依賴的package包/類
@Test
public void testLogJsonArgument() throws InterruptedException {
    final ListAppender appender = (ListAppender) init.getAppender("List");
    appender.countDownLatch = new CountDownLatch(4);
    appender.clear();
    final Logger logger = (Logger) LogManager.getRootLogger();
    final String json = "{\"id\":10,\"name\":\"Alice\"}";
    logger.error("log:{}", json);
    // wait until background thread finished processing
    final int msgCount = 1;
    if (appender.getMessages().size() < msgCount) {
        appender.countDownLatch.await(5, TimeUnit.SECONDS);
    }
    assertEquals("Background thread did not finish processing: msg count", msgCount, appender.getMessages().size());

    // don't stop appender until background thread is done
    appender.stop();
    final List<String> list = appender.getMessages();
    final String eventStr = list.get(0).toString();
    Assert.assertTrue(eventStr, eventStr.contains(json));
}
 
開發者ID:apache,項目名稱:logging-log4j2,代碼行數:22,代碼來源:CsvParameterLayoutTest.java

示例5: integrationTest

import org.apache.logging.log4j.core.Logger; //導入方法依賴的package包/類
/**
 * Integration test that asserts events are sent to the Elastic cluster.
 *
 * @throws IOException Signals that an I/O exception has occurred.
 */
@Test
public void integrationTest() throws IOException {
	String test = "integration-test";
	Logger logger = getLogger(test);
	ElasticClient client = new ElasticClient(logger);
	String marker = getUniqueMarker();
	logger.error(MarkerManager.getMarker(marker), "Test Message");
	JsonNode doc = client.findFirstByMarker(marker);
	assertNotNull(doc);
	assertFieldValue(doc, "level", "ERROR");
	assertFieldValue(doc, "message", "Test Message");
}
 
開發者ID:magrossi,項目名稱:es-log4j2-appender,代碼行數:18,代碼來源:ElasticSearchRestAppenderTest.java

示例6: logAndCheck

import org.apache.logging.log4j.core.Logger; //導入方法依賴的package包/類
private void logAndCheck() {
    final Marker marker = MarkerManager.getMarker("HEXDUMP");
    final Logger logger = loggerContextRule.getLogger(DefaultRouteScriptAppenderTest.class);
    logger.error("Hello");
    final ListAppender listAppender = getListAppender();
    final List<LogEvent> list = listAppender.getEvents();
    assertNotNull("No events generated", list);
    assertTrue("Incorrect number of events. Expected 1, got " + list.size(), list.size() == 1);
    logger.error("World");
    assertTrue("Incorrect number of events. Expected 2, got " + list.size(), list.size() == 2);
    logger.error(marker, "DEADBEEF");
    assertTrue("Incorrect number of events. Expected 3, got " + list.size(), list.size() == 3);
}
 
開發者ID:apache,項目名稱:logging-log4j2,代碼行數:14,代碼來源:DefaultRouteScriptAppenderTest.java

示例7: logAndCheck

import org.apache.logging.log4j.core.Logger; //導入方法依賴的package包/類
private void logAndCheck() {
    final Marker marker = MarkerManager.getMarker("HEXDUMP");
    final Logger logger = loggerContextRule.getLogger(RoutesScriptAppenderTest.class);
    logger.error("Hello");
    final ListAppender listAppender = getListAppender();
    final List<LogEvent> list = listAppender.getEvents();
    assertNotNull("No events generated", list);
    assertTrue("Incorrect number of events. Expected 1, got " + list.size(), list.size() == 1);
    logger.error("World");
    assertTrue("Incorrect number of events. Expected 2, got " + list.size(), list.size() == 2);
    logger.error(marker, "DEADBEEF");
    assertTrue("Incorrect number of events. Expected 3, got " + list.size(), list.size() == 3);
}
 
開發者ID:apache,項目名稱:logging-log4j2,代碼行數:14,代碼來源:RoutesScriptAppenderTest.java

示例8: logAndCheck

import org.apache.logging.log4j.core.Logger; //導入方法依賴的package包/類
private void logAndCheck() {
    final Marker marker = MarkerManager.getMarker("HEXDUMP");
    final Logger logger = loggerContextRule.getLogger(ScriptAppenderSelectorTest.class);
    logger.error("Hello");
    final ListAppender listAppender = getListAppender();
    final List<LogEvent> list = listAppender.getEvents();
    assertNotNull("No events generated", list);
    assertTrue("Incorrect number of events. Expected 1, got " + list.size(), list.size() == 1);
    logger.error("World");
    assertTrue("Incorrect number of events. Expected 2, got " + list.size(), list.size() == 2);
    logger.error(marker, "DEADBEEF");
    assertTrue("Incorrect number of events. Expected 3, got " + list.size(), list.size() == 3);
}
 
開發者ID:apache,項目名稱:logging-log4j2,代碼行數:14,代碼來源:ScriptAppenderSelectorTest.java

示例9: 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

示例10: 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

示例11: 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.error方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。