org.apache.logging.log4j.core.Appender.append()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(10.6k)|赞(0)|评价(0)|浏览(146)

本文整理了Java中org.apache.logging.log4j.core.Appender.append()方法的一些代码示例,展示了Appender.append()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Appender.append()方法的具体详情如下:
包路径:org.apache.logging.log4j.core.Appender
类名称:Appender
方法名:append

Appender.append介绍

[英]Logs a LogEvent using whatever logic this Appender wishes to use. It is typically recommended to use a bridge pattern not only for the benefits from decoupling an Appender from its implementation, but it is also handy for sharing resources which may require some form of locking.
[中]使用此附加程序希望使用的任何逻辑记录LogEvent。通常建议使用桥接模式,这不仅有助于将Appender与其实现分离,而且还便于共享可能需要某种形式锁定的资源。

代码示例

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@Test
public void testAppendWithKeyLookup() throws Exception {
  final Appender appender = ctx.getRequiredAppender("KafkaAppenderWithKeyLookup");
  final LogEvent logEvent = createLogEvent();
  Date date = new Date();
  SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
  appender.append(logEvent);
  final List<ProducerRecord<byte[], byte[]>> history = kafka.history();
  assertEquals(1, history.size());
  final ProducerRecord<byte[], byte[]> item = history.get(0);
  assertNotNull(item);
  assertEquals(TOPIC_NAME, item.topic());
  byte[] keyValue = format.format(date).getBytes(StandardCharsets.UTF_8);
  assertArrayEquals(item.key(), keyValue);
  assertEquals(LOG_MESSAGE, new String(item.value(), StandardCharsets.UTF_8));
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@Test
public void testAppendWithKey() throws Exception {
  final Appender appender = ctx.getRequiredAppender("KafkaAppenderWithKey");
  final LogEvent logEvent = createLogEvent();
  appender.append(logEvent);
  final List<ProducerRecord<byte[], byte[]>> history = kafka.history();
  assertEquals(1, history.size());
  final ProducerRecord<byte[], byte[]> item = history.get(0);
  assertNotNull(item);
  assertEquals(TOPIC_NAME, item.topic());
  String msgKey = item.key().toString();
  byte[] keyValue = "key".getBytes(StandardCharsets.UTF_8);
  assertArrayEquals(item.key(), keyValue);
  assertEquals(LOG_MESSAGE, new String(item.value(), StandardCharsets.UTF_8));
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@Test
public void testAsyncAppend() throws Exception {
  final Appender appender = ctx.getRequiredAppender("AsyncKafkaAppender");
  appender.append(createLogEvent());
  final List<ProducerRecord<byte[], byte[]>> history = kafka.history();
  assertEquals(1, history.size());
  final ProducerRecord<byte[], byte[]> item = history.get(0);
  assertNotNull(item);
  assertEquals(TOPIC_NAME, item.topic());
  assertNull(item.key());
  assertEquals(LOG_MESSAGE, new String(item.value(), StandardCharsets.UTF_8));
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@Test
public void testAppendWithLayout() throws Exception {
  final Appender appender = ctx.getRequiredAppender("KafkaAppenderWithLayout");
  appender.append(createLogEvent());
  final List<ProducerRecord<byte[], byte[]>> history = kafka.history();
  assertEquals(1, history.size());
  final ProducerRecord<byte[], byte[]> item = history.get(0);
  assertNotNull(item);
  assertEquals(TOPIC_NAME, item.topic());
  assertNull(item.key());
  assertEquals("[" + LOG_MESSAGE + "]", new String(item.value(), StandardCharsets.UTF_8));
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@Test
public void testAppendWithSerializedLayout() throws Exception {
  final Appender appender = ctx.getRequiredAppender("KafkaAppenderWithSerializedLayout");
  final LogEvent logEvent = createLogEvent();
  appender.append(logEvent);
  final List<ProducerRecord<byte[], byte[]>> history = kafka.history();
  assertEquals(1, history.size());
  final ProducerRecord<byte[], byte[]> item = history.get(0);
  assertNotNull(item);
  assertEquals(TOPIC_NAME, item.topic());
  assertNull(item.key());
  assertEquals(LOG_MESSAGE, deserializeLogEvent(item.value()).getMessage().getFormattedMessage());
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@Test(expected = AppenderLoggingException.class)
public void testAppendConnectError() throws Exception {
  final Appender appender = HttpAppender.newBuilder()
    .withName("Http")
    .withLayout(JsonLayout.createDefaultLayout())
    .setConfiguration(ctx.getConfiguration())
    .withIgnoreExceptions(false)
    .setUrl(new URL("http://localhost:"+(wireMockRule.port()+1)+"/test/log4j/"))
    .build();
  appender.append(createLogEvent());
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@Test(expected = AppenderLoggingException.class)
public void testAppendError() throws Exception {
  wireMockRule.stubFor(post(urlEqualTo("/test/log4j/"))
    .willReturn(FAILURE_RESPONSE));
  final Appender appender = HttpAppender.newBuilder()
    .withName("Http")
    .withLayout(JsonLayout.createDefaultLayout())
    .setConfiguration(ctx.getConfiguration())
    .withIgnoreExceptions(false)
    .setUrl(new URL("http://localhost:" + wireMockRule.port() + "/test/log4j/"))
    .build();
  appender.append(createLogEvent());
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@Test
public void testAppend() throws Exception {
  wireMockRule.stubFor(post(urlEqualTo("/test/log4j/"))
    .willReturn(SUCCESS_RESPONSE));
  final Appender appender = HttpAppender.newBuilder()
    .withName("Http")
    .withLayout(JsonLayout.createDefaultLayout())
    .setConfiguration(ctx.getConfiguration())
    .setUrl(new URL("http://localhost:" + wireMockRule.port() + "/test/log4j/"))
    .build();
  appender.append(createLogEvent());
  wireMockRule.verify(postRequestedFor(urlEqualTo("/test/log4j/"))
    .withHeader("Host", containing("localhost"))
    .withHeader("Content-Type", containing("application/json"))
    .withRequestBody(containing("\"message\" : \"" + LOG_MESSAGE + "\"")));
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@Test
public void testAppendMethodPut() throws Exception {
  wireMockRule.stubFor(put(urlEqualTo("/test/log4j/1234"))
    .willReturn(SUCCESS_RESPONSE));
  final Appender appender = HttpAppender.newBuilder()
    .withName("Http")
    .withLayout(JsonLayout.createDefaultLayout())
    .setConfiguration(ctx.getConfiguration())
    .setMethod("PUT")
    .setUrl(new URL("http://localhost:" + wireMockRule.port() + "/test/log4j/1234"))
    .build();
  appender.append(createLogEvent());
  wireMockRule.verify(putRequestedFor(urlEqualTo("/test/log4j/1234"))
    .withHeader("Host", containing("localhost"))
    .withHeader("Content-Type", containing("application/json"))
    .withRequestBody(containing("\"message\" : \"" + LOG_MESSAGE + "\"")));
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

.setUrl(new URL("http://localhost:" + wireMockRule.port() + "/test/log4j/"))
  .build();
appender.append(createLogEvent());

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@Test
public void testAppendHttps() throws Exception {
  wireMockRule.stubFor(post(urlEqualTo("/test/log4j/"))
    .willReturn(SUCCESS_RESPONSE));
  final Appender appender = HttpAppender.newBuilder()
    .withName("Http")
    .withLayout(JsonLayout.createDefaultLayout())
    .setConfiguration(ctx.getConfiguration())
    .setUrl(new URL("https://localhost:" + wireMockRule.httpsPort() + "/test/log4j/"))
    .setSslConfiguration(SslConfiguration.createSSLConfiguration(null,
      KeyStoreConfiguration.createKeyStoreConfiguration(TestConstants.KEYSTORE_FILE, TestConstants.KEYSTORE_PWD(), TestConstants.KEYSTORE_TYPE, null),
      TrustStoreConfiguration.createKeyStoreConfiguration(TestConstants.TRUSTSTORE_FILE, TestConstants.TRUSTSTORE_PWD(), TestConstants.TRUSTSTORE_TYPE, null)))
    .setVerifyHostname(false)
    .build();
  appender.append(createLogEvent());
  wireMockRule.verify(postRequestedFor(urlEqualTo("/test/log4j/"))
    .withHeader("Host", containing("localhost"))
    .withHeader("Content-Type", containing("application/json"))
    .withRequestBody(containing("\"message\" : \"" + LOG_MESSAGE + "\"")));
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@Test
public void testAppendCustomHeader() throws Exception {
  wireMockRule.stubFor(post(urlEqualTo("/test/log4j/"))
    .willReturn(SUCCESS_RESPONSE));
  final Appender appender = HttpAppender.newBuilder()
    .withName("Http")
    .withLayout(JsonLayout.createDefaultLayout())
    .setConfiguration(ctx.getConfiguration())
    .setUrl(new URL("http://localhost:" + wireMockRule.port() + "/test/log4j/"))
    .setHeaders(new Property[] {
      Property.createProperty("X-Test", "header value"),
      Property.createProperty("X-Runtime", "${java:runtime}")
    })
    .build();
  appender.append(createLogEvent());
  wireMockRule.verify(postRequestedFor(urlEqualTo("/test/log4j/"))
    .withHeader("Host", containing("localhost"))
    .withHeader("X-Test", equalTo("header value"))
    .withHeader("X-Runtime", equalTo(JAVA_LOOKUP.getRuntime()))
    .withHeader("Content-Type", containing("application/json"))
    .withRequestBody(containing("\"message\" : \"" + LOG_MESSAGE + "\"")));
}

代码示例来源:origin: ops4j/org.ops4j.pax.logging

private void tryCallAppender(final LogEvent event) {
  try {
    appender.append(event);
  } catch (final RuntimeException ex) {
    handleAppenderError(ex);
  } catch (final Exception ex) {
    handleAppenderError(new AppenderLoggingException(ex));
  }
}

代码示例来源:origin: rfoltyns/log4j2-elasticsearch

@Test
public void deliversToAppenderRef() {
  // given
  Appender appender = mock(Appender.class);
  when(appender.isStarted()).thenReturn(true);
  Configuration configuration = mock(Configuration.class);
  String testAppenderRef = "testAppenderRef";
  when(configuration.getAppender(testAppenderRef)).thenReturn(appender);
  FailoverPolicy<String> failoverPolicy = createTestFailoverPolicy(testAppenderRef, configuration);
  String failedMessage = "test failed message";
  // when
  failoverPolicy.deliver(failedMessage);
  // then
  verify(appender, times(1)).append(any(LogEvent.class));
}

代码示例来源:origin: rfoltyns/log4j2-elasticsearch

@Test
public void resolvesAppenderRefOnlyOnce() {
  // given
  Appender appender = mock(Appender.class);
  when(appender.isStarted()).thenReturn(true);
  Configuration configuration = mock(Configuration.class);
  String testAppenderRef = "testAppenderRef";
  when(configuration.getAppender(testAppenderRef)).thenReturn(appender);
  FailoverPolicy<String> failoverPolicy = createTestFailoverPolicy(testAppenderRef, configuration);
  String failedMessage = "test failed message";
  // when
  failoverPolicy.deliver(failedMessage);
  failoverPolicy.deliver(failedMessage);
  // then
  verify(configuration, times(1)).getAppender(anyString());
  verify(appender, times(2)).append(any(LogEvent.class));
}

代码示例来源:origin: kptfh/feign-reactive

Mockito.verify(appender, never()).append(argumentCaptor.capture());
Mockito.verify(appender, times(7)).append(argumentCaptor.capture());

相关文章

微信公众号

最新文章

更多