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


Java LogEvent類代碼示例

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


LogEvent類屬於org.apache.logging.log4j.core包,在下文中一共展示了LogEvent類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: test

import org.apache.logging.log4j.core.LogEvent; //導入依賴的package包/類
@Test
public void test() {
    {
        Logger logger = LogManager.getLogger("test.logger");
        logger.error("This is an error!");
        logger.error("This is another error!");
        logger.error("This is a third error!");
    }

    assertThat(collector.getLogs()).hasSize(3)
            .contains("This is an error!", "This is another error!", "This is a third error!");

    List<LogEvent> rawLogs = (List<LogEvent>) collector.getRawLogs();
    assertThat(rawLogs).hasSize(3);

    assertTrue(rawLogs.stream().allMatch(l -> l.getLevel() == Level.ERROR));
}
 
開發者ID:haasted,項目名稱:TestLogCollectors,代碼行數:18,代碼來源:Log4j2Test.java

示例2: prepareLogEvent

import org.apache.logging.log4j.core.LogEvent; //導入依賴的package包/類
/**
 * Prepare log event log event.
 *
 * @param logEvent the log event
 * @return the log event
 */
public static LogEvent prepareLogEvent(final LogEvent logEvent) {
    final String messageModified = TicketIdSanitizationUtils.sanitize(logEvent.getMessage().getFormattedMessage());
    final Message message = new SimpleMessage(messageModified);
    final LogEvent newLogEvent = Log4jLogEvent.newBuilder()
            .setLevel(logEvent.getLevel())
            .setLoggerName(logEvent.getLoggerName())
            .setLoggerFqcn(logEvent.getLoggerFqcn())
            .setContextMap(logEvent.getContextMap())
            .setContextStack(logEvent.getContextStack())
            .setEndOfBatch(logEvent.isEndOfBatch())
            .setIncludeLocation(logEvent.isIncludeLocation())
            .setMarker(logEvent.getMarker())
            .setMessage(message)
            .setNanoTime(logEvent.getNanoTime())
            .setSource(logEvent.getSource())
            .setThreadName(logEvent.getThreadName())
            .setThrownProxy(logEvent.getThrownProxy())
            .setThrown(logEvent.getThrown())
            .setTimeMillis(logEvent.getTimeMillis())
            .build();
    return newLogEvent;
}
 
開發者ID:mrluo735,項目名稱:cas-5.1.0,代碼行數:29,代碼來源:LoggingUtils.java

示例3: appenderUsesProvidedLayoutWhenMessageOnlyIsSetToFalse

import org.apache.logging.log4j.core.LogEvent; //導入依賴的package包/類
@Test
public void appenderUsesProvidedLayoutWhenMessageOnlyIsSetToFalse() throws IOException, NoSuchMethodException, SecurityException {

    // given
    AbstractStringLayout layout = PowerMockito.mock(AbstractStringLayout.class);

    ElasticsearchAppender.Builder builder = ElasticsearchAppenderTest.createTestElasticsearchAppenderBuilder();
    builder.withMessageOnly(false);
    builder.withLayout(layout);

    LogEvent logEvent = mock(LogEvent.class);

    ElasticsearchAppender appender = builder.build();

    // when
    appender.append(logEvent);

    // then
    ArgumentCaptor<LogEvent> captor = ArgumentCaptor.forClass(LogEvent.class);
    verify(layout, times(1)).toSerializable(captor.capture());
    assertEquals(logEvent, captor.getValue());
}
 
開發者ID:rfoltyns,項目名稱:log4j2-elasticsearch,代碼行數:23,代碼來源:ElasticsearchAppenderTest.java

示例4: resolvesAppenderRefOnlyOnce

import org.apache.logging.log4j.core.LogEvent; //導入依賴的package包/類
@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));
}
 
開發者ID:rfoltyns,項目名稱:log4j2-elasticsearch,代碼行數:23,代碼來源:AppenderRefFailoverPolicyTest.java

示例5: append

import org.apache.logging.log4j.core.LogEvent; //導入依賴的package包/類
@Override
public void append(LogEvent event) {
    Level level = event.getLevel();
    if (TRACE.equals(level)) {
        TRACE_LABEL.inc();
    } else if (DEBUG.equals(level)) {
        DEBUG_LABEL.inc();
    } else if (INFO.equals(level)) {
        INFO_LABEL.inc();
    } else if (WARN.equals(level)) {
        WARN_LABEL.inc();
    } else if (ERROR.equals(level)) {
        ERROR_LABEL.inc();
    } else if (FATAL.equals(level)) {
        FATAL_LABEL.inc();
    }
}
 
開發者ID:AndreyVMarkelov,項目名稱:jira-prometheus-exporter,代碼行數:18,代碼來源:InstrumentedAppender.java

示例6: append

import org.apache.logging.log4j.core.LogEvent; //導入依賴的package包/類
@Override
public void append(LogEvent event) {
	lock.lock();
    try {
    	String json = new String(getLayout().toByteArray(event));
    	buffered.add(getBulkItem(json));
    	this.check();        	
    } catch (Exception ex) {
        if (!ignoreExceptions()) {
            throw new AppenderLoggingException(ex);
        } else {
        	LOGGER.error("Failed to process event.", ex);
        }
    } finally {
    	lock.unlock();
    }
}
 
開發者ID:magrossi,項目名稱:es-log4j2-appender,代碼行數:18,代碼來源:ElasticSearchRestAppender.java

示例7: append

import org.apache.logging.log4j.core.LogEvent; //導入依賴的package包/類
@Override
public synchronized void append(final LogEvent event) {
    final Layout<? extends Serializable> layout = getLayout();
    if (layout == null) {
        if (event instanceof MutableLogEvent) {
            // must take snapshot or subsequent calls to logger.log() will modify this event
            events.add(((MutableLogEvent) event).createMemento());
        } else {
            events.add(event);
        }
    } else if (layout instanceof SerializedLayout) {
        final byte[] header = layout.getHeader();
        final byte[] content = layout.toByteArray(event);
        final byte[] record = new byte[header.length + content.length];
        System.arraycopy(header, 0, record, 0, header.length);
        System.arraycopy(content, 0, record, header.length, content.length);
        data.add(record);
    } else {
        write(layout.toByteArray(event));
    }
    if (countDownLatch != null) {
        countDownLatch.countDown();
    }
}
 
開發者ID:savantly-net,項目名稱:log4j2-extended-jsonlayout,代碼行數:25,代碼來源:ListAppender.java

示例8: watch

import org.apache.logging.log4j.core.LogEvent; //導入依賴的package包/類
public void watch(Class<?> loggerClass, Level level) {
    this.loggerClass = loggerClass;
    Appender appender = new AbstractAppender(APPENDER_NAME, null, PatternLayout.createDefaultLayout()) {
        @Override
        public void append(LogEvent event) {
            logEvents.add(event);
        }
    };
    appender.start();
    final LoggerContext ctx = getLoggerContext();
    LoggerConfig loggerConfig = ctx.getConfiguration().getLoggerConfig(loggerClass.getName());
    oldLevel = loggerConfig.getLevel();
    loggerConfig.setLevel(level);
    loggerConfig.addAppender(appender, level, null);
    ctx.updateLoggers();
}
 
開發者ID:TNG,項目名稱:ArchUnit,代碼行數:17,代碼來源:LogTestRule.java

示例9: logEventShouldMatchCustomConfig

import org.apache.logging.log4j.core.LogEvent; //導入依賴的package包/類
@Test
public void logEventShouldMatchCustomConfig() throws Exception {
  String logLogger = getClass().getName();
  Level logLevel = Level.DEBUG;
  String logMessage = "this is a log statement";

  Logger logger = LogService.getLogger();
  logger.debug(logMessage);

  BasicAppender appender = BasicAppender.getInstance();
  assertThat(appender).isNotNull();
  assertThat(appender.events()).hasSize(1);

  LogEvent event = appender.events().get(0);
  assertThat(event.getLoggerName()).isEqualTo(logLogger);
  assertThat(event.getLevel()).isEqualTo(logLevel);
  assertThat(event.getMessage().getFormattedMessage()).isEqualTo(logMessage);

  assertThat(systemOutRule.getLog()).contains(logLevel.name());
  assertThat(systemOutRule.getLog()).contains(logMessage);
  assertThat(systemOutRule.getLog()).contains(CONFIG_LAYOUT_PREFIX);
  assertThat(systemOutRule.getLog()).matches(defineLogStatementRegex(logLevel, logMessage));
}
 
開發者ID:ampool,項目名稱:monarch,代碼行數:24,代碼來源:CustomConfigWithLogServiceIntegrationTest.java

示例10: append

import org.apache.logging.log4j.core.LogEvent; //導入依賴的package包/類
@Override
public void append(LogEvent event)
{
    if (reader != null)
    {
        try
        {
            Writer out = reader.getOutput();
            out.write(RESET_LINE);
            out.write(formatEvent(event));

            reader.drawLine();
            reader.flush();
        }
        catch (IOException ignored)
        {
        }
    }
    else
    {
        out.print(formatEvent(event));
    }
}
 
開發者ID:F1r3w477,項目名稱:CustomWorldGen,代碼行數:24,代碼來源:TerminalConsoleAppender.java

示例11: deliversToAppenderRef

import org.apache.logging.log4j.core.LogEvent; //導入依賴的package包/類
@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));
}
 
開發者ID:rfoltyns,項目名稱:log4j2-elasticsearch,代碼行數:21,代碼來源:AppenderRefFailoverPolicyTest.java

示例12: resolve

import org.apache.logging.log4j.core.LogEvent; //導入依賴的package包/類
@Override
public JsonNode resolve(TemplateResolverContext context, LogEvent logEvent, String key) {
    ThreadContext.ContextStack contextStack = logEvent.getContextStack();
    if (contextStack.getDepth() == 0) {
        return null;
    }
    Pattern itemPattern = context.getNdcPattern();
    ArrayNode contextStackNode = context.getObjectMapper().createArrayNode();
    for (String contextStackItem : contextStack.asList()) {
        boolean matches = itemPattern == null || itemPattern.matcher(contextStackItem).matches();
        if (matches) {
            contextStackNode.add(contextStackItem);
        }
    }
    return contextStackNode;
}
 
開發者ID:vy,項目名稱:log4j2-logstash-layout,代碼行數:17,代碼來源:ContextStackResolver.java

示例13: checkLogEvent

import org.apache.logging.log4j.core.LogEvent; //導入依賴的package包/類
private void checkLogEvent(Configuration config, LogEvent logEvent, String lookupTestKey, String lookupTestVal) throws IOException {
    Set<String> mdcKeys = logEvent.getContextData().toMap().keySet();
    String firstMdcKey = mdcKeys.iterator().next();
    String firstMdcKeyExcludingRegex = mdcKeys.isEmpty() ? null : String.format("^(?!%s).*$", Pattern.quote(firstMdcKey));
    List<String> ndcItems = logEvent.getContextStack().asList();
    String firstNdcItem = ndcItems.get(0);
    String firstNdcItemExcludingRegex = ndcItems.isEmpty() ? null : String.format("^(?!%s).*$", Pattern.quote(firstNdcItem));
    LogstashLayout layout = LogstashLayout
            .newBuilder()
            .setConfiguration(config)
            .setTemplateUri("classpath:LogstashTestLayout.json")
            .setStackTraceEnabled(true)
            .setLocationInfoEnabled(true)
            .setMdcKeyPattern(firstMdcKeyExcludingRegex)
            .setNdcPattern(firstNdcItemExcludingRegex)
            .build();
    String serializedLogEvent = layout.toSerializable(logEvent);
    JsonNode rootNode = OBJECT_MAPPER.readTree(serializedLogEvent);
    checkConstants(rootNode);
    checkBasicFields(logEvent, rootNode);
    checkSource(logEvent, rootNode);
    checkException(logEvent, rootNode);
    checkContextData(logEvent, firstMdcKeyExcludingRegex, rootNode);
    checkContextStack(logEvent, firstNdcItemExcludingRegex, rootNode);
    checkLookupTest(lookupTestKey, lookupTestVal, rootNode);
}
 
開發者ID:vy,項目名稱:log4j2-logstash-layout,代碼行數:27,代碼來源:LogstashLayoutTest.java

示例14: test_lineSeparator_suffix

import org.apache.logging.log4j.core.LogEvent; //導入依賴的package包/類
@Test
public void test_lineSeparator_suffix() {

    // Create the log event.
    SimpleMessage message = new SimpleMessage("Hello, World!");
    LogEvent logEvent = Log4jLogEvent
            .newBuilder()
            .setLoggerName(LogstashLayoutTest.class.getSimpleName())
            .setLevel(Level.INFO)
            .setMessage(message)
            .build();

    // Check line separators.
    SoftAssertions assertions = new SoftAssertions();
    test_lineSeparator_suffix(logEvent, true, assertions);
    test_lineSeparator_suffix(logEvent, false, assertions);
    assertions.assertAll();

}
 
開發者ID:vy,項目名稱:log4j2-logstash-layout,代碼行數:20,代碼來源:LogstashLayoutTest.java

示例15: format

import org.apache.logging.log4j.core.LogEvent; //導入依賴的package包/類
@Override
public final String format(LogEvent event) {
    long eventTimeInMillis = event.getTimeMillis();

    // handle "old" events that arrived in separate threads after rollover
    if (eventTimeInMillis < currentFileTime) {
        return doFormat(indexName, eventTimeInMillis);
    }

    // rollover
    if (eventTimeInMillis >= nextRolloverTime && rollingOver.compareAndSet(false, true)) {
        rollover(indexName, eventTimeInMillis);
        rollingOver.set(false);
    }

    // happy path - have to check for pending rollover to avoid race conditions
    if (!rollingOver.get()) {
        return currentName;
    }

    // fail-safe for pending rollover
    return doFormat(indexName, eventTimeInMillis);
}
 
開發者ID:rfoltyns,項目名稱:log4j2-elasticsearch,代碼行數:24,代碼來源:RollingIndexNameFormatter.java


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