当前位置: 首页>>代码示例>>Java>>正文


Java Log4jLogEvent类代码示例

本文整理汇总了Java中org.apache.logging.log4j.core.impl.Log4jLogEvent的典型用法代码示例。如果您正苦于以下问题:Java Log4jLogEvent类的具体用法?Java Log4jLogEvent怎么用?Java Log4jLogEvent使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


Log4jLogEvent类属于org.apache.logging.log4j.core.impl包,在下文中一共展示了Log4jLogEvent类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: prepareLogEvent

import org.apache.logging.log4j.core.impl.Log4jLogEvent; //导入依赖的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

示例2: test_lineSeparator_suffix

import org.apache.logging.log4j.core.impl.Log4jLogEvent; //导入依赖的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

示例3: newEvent

import org.apache.logging.log4j.core.impl.Log4jLogEvent; //导入依赖的package包/类
@Override
protected Object newEvent(LogDetails logDetails) {
    final Message message = new SimpleMessage(logDetails.getLogMessage());
    final StringMap contextData;
    if (!logDetails.getMdc().isEmpty()) {
        contextData = ContextDataFactory.createContextData();
        logDetails.getMdc().entrySet().forEach(it -> contextData.putValue(it.getKey(), it.getValue()));
    } else {
        contextData = null;
    }
    Log4jLogEvent.Builder builder = Log4jLogEvent.newBuilder().setLoggerName(logDetails.getClassName())
            .setTimeMillis(logDetails.getTimeMillis()).setLevel(Level.DEBUG).setContextData(contextData)
            .setIncludeLocation(logDetails.isLocationInfo()).setLoggerFqcn(logDetails.getClassName()).setMessage(message);
    if (logDetails.isLocationInfo()) {
        builder.setSource(new StackTraceElement(logDetails.getClassName(), logDetails.getMethodName(),
                logDetails.getFileName(), logDetails.getLineNumber()));
    }
    if (logDetails.getException() != null) {
        builder.setThrown(logDetails.getException());
    }
    return builder.build();
}
 
开发者ID:Talend,项目名称:daikon,代码行数:23,代码来源:Log4j2JSONLayoutTest.java

示例4: testKinesisPutRecords

import org.apache.logging.log4j.core.impl.Log4jLogEvent; //导入依赖的package包/类
@Test(enabled = false)
public void testKinesisPutRecords() throws Exception {
    List<LogEvent> logEventList = Collections.singletonList(
        Log4jLogEvent.createEvent("foobar", null, "foo.bar", Level.ERROR, null, null, null, null, null, "tname", null, 12345));

    Collection<PutRecordsRequestEntry> records = new ArrayList<>();
    for (LogEvent logEvent : logEventList) {
        String toJson = objectMapper.writeValueAsString(logEvent);
        PutRecordsRequestEntry putRecordsRequestEntry = new PutRecordsRequestEntry()
            .withData(ByteBuffer.wrap(toJson.getBytes()))
            .withPartitionKey("testKinesisPutRecords");
        records.add(putRecordsRequestEntry);
    }

    PutRecordsRequest putRecordsRequest = new PutRecordsRequest();
    putRecordsRequest.setRecords(records);
    putRecordsRequest.setStreamName(awsStreamName);
    PutRecordsResult putRecordsResult = client.putRecords(putRecordsRequest);
    System.out.println(putRecordsResult.toString());
}
 
开发者ID:jivesoftware,项目名称:miru,代码行数:21,代码来源:HttpKinesisLogAppenderTest.java

示例5: testAppend_Simple

import org.apache.logging.log4j.core.impl.Log4jLogEvent; //导入依赖的package包/类
@Test
public void testAppend_Simple() {
    SystemdJournalAppender journalAppender = new SystemdJournalAppender("Journal", null, null, false, journalLibrary,
            false, false, false, false, false, false, null, null);

    Message message = mock(Message.class);
    when(message.getFormattedMessage()).thenReturn("some message");
    LogEvent event = new Log4jLogEvent.Builder().setMessage(message).setLevel(Level.INFO).build();

    journalAppender.append(event);

    List<Object> expectedArgs = new ArrayList<>();
    expectedArgs.add("some message");
    expectedArgs.add("PRIORITY=%d");
    expectedArgs.add(6);
    expectedArgs.add(null);

    verify(journalLibrary).sd_journal_send("MESSAGE=%s", expectedArgs.toArray());
}
 
开发者ID:bwaldvogel,项目名称:log4j-systemd-journal-appender,代码行数:20,代码来源:SystemdJournalAppenderTest.java

示例6: testAppend_DoNotLogException

import org.apache.logging.log4j.core.impl.Log4jLogEvent; //导入依赖的package包/类
@Test
public void testAppend_DoNotLogException() {

    SystemdJournalAppender journalAppender = new SystemdJournalAppender("Journal", null, null, false, journalLibrary,
            false, false, false, false, false, false, null, null);

    Message message = mock(Message.class);
    when(message.getFormattedMessage()).thenReturn("some message");

    LogEvent event = new Log4jLogEvent.Builder() //
            .setMessage(message)//
            .setLoggerFqcn(journalAppender.getClass().getName())//
            .setThrown(new Throwable()) //
            .setLevel(Level.INFO).build();
    event.setIncludeLocation(true);

    journalAppender.append(event);

    List<Object> expectedArgs = new ArrayList<>();
    expectedArgs.add("some message");
    expectedArgs.add("PRIORITY=%d");
    expectedArgs.add(6);
    expectedArgs.add(null);

    verify(journalLibrary).sd_journal_send("MESSAGE=%s", expectedArgs.toArray());
}
 
开发者ID:bwaldvogel,项目名称:log4j-systemd-journal-appender,代码行数:27,代码来源:SystemdJournalAppenderTest.java

示例7: testThresholds

import org.apache.logging.log4j.core.impl.Log4jLogEvent; //导入依赖的package包/类
@Test
public void testThresholds() throws Exception {
    RegexFilter filter = RegexFilter.createFilter(".* test .*", null, false, null, null);
    filter.start();
    assertTrue(filter.isStarted());
    assertSame(Filter.Result.NEUTRAL,
            filter.filter(null, Level.DEBUG, null, (Object) "This is a test message", (Throwable) null));
    assertSame(Filter.Result.DENY, filter.filter(null, Level.ERROR, null, (Object) "This is not a test",
            (Throwable) null));
    LogEvent event = Log4jLogEvent.newBuilder() //
            .setLevel(Level.DEBUG) //
            .setMessage(new SimpleMessage("Another test message")) //
            .build();
    assertSame(Filter.Result.NEUTRAL, filter.filter(event));
    event = Log4jLogEvent.newBuilder() //
            .setLevel(Level.ERROR) //
            .setMessage(new SimpleMessage("test")) //
            .build();
    assertSame(Filter.Result.DENY, filter.filter(event));
    filter = RegexFilter.createFilter(null, null, false, null, null);
    assertNull(filter);
}
 
开发者ID:apache,项目名称:logging-log4j2,代码行数:23,代码来源:RegexFilterTest.java

示例8: testSuffixFromNormalPattern

import org.apache.logging.log4j.core.impl.Log4jLogEvent; //导入依赖的package包/类
@Test
public void testSuffixFromNormalPattern() {
    final String suffix = "suffix(%mdc{key})";
    ThreadContext.put("key", "test suffix ");
    final String[] options = {suffix};
    final ExtendedThrowablePatternConverter converter = ExtendedThrowablePatternConverter.newInstance(null, options);
    final Throwable cause = new NullPointerException("null pointer");
    final Throwable parent = new IllegalArgumentException("IllegalArgument", cause);
    final LogEvent event = Log4jLogEvent.newBuilder() //
            .setLoggerName("testLogger") //
            .setLoggerFqcn(this.getClass().getName()) //
            .setLevel(Level.DEBUG) //
            .setMessage(new SimpleMessage("test exception")) //
            .setThrown(parent).build();
    final StringBuilder sb = new StringBuilder();
    converter.format(event, sb);
    final String result = sb.toString();
    assertTrue("No suffix", result.contains("test suffix"));
}
 
开发者ID:apache,项目名称:logging-log4j2,代码行数:20,代码来源:ExtendedThrowablePatternConverterTest.java

示例9: testReplacement

import org.apache.logging.log4j.core.impl.Log4jLogEvent; //导入依赖的package包/类
@Test
public void testReplacement() {
    ThreadContext.put("MyKey", "Apache");
    final LogEvent event = new Log4jLogEvent(RegexReplacementConverterTest.class.getName(), null, null,
        Level.DEBUG, new SimpleMessage("This is a test"), null);
    final StringBuilder sb = new StringBuilder();
    final LoggerContext ctx = (LoggerContext) LogManager.getContext();
    final String[] options = new String[] {
        "%logger %msg%n", "\\.", "/"
    };
    final RegexReplacementConverter converter = RegexReplacementConverter.newInstance(ctx.getConfiguration(),
        options);
    converter.format(event, sb);
    assertEquals("org/apache/logging/log4j/core/pattern/RegexReplacementConverterTest This is a test" +
        Constants.LINE_SEP, sb.toString());
}
 
开发者ID:OuZhencong,项目名称:log4j2,代码行数:17,代码来源:RegexReplacementConverterTest.java

示例10: testCustomPattern

import org.apache.logging.log4j.core.impl.Log4jLogEvent; //导入依赖的package包/类
/**
 * Test the custom pattern
 */
@Test
public void testCustomPattern() {
    final List<PatternFormatter> formatters = parser.parse(customPattern);
    assertNotNull(formatters);
    final Map<String, String> mdc = new HashMap<String, String>();
    mdc.put("loginId", "Fred");
    final Throwable t = new Throwable();
    final StackTraceElement[] elements = t.getStackTrace();
    final LogEvent event = new Log4jLogEvent("org.apache.logging.log4j.PatternParserTest", MarkerManager.getMarker("TEST"),
        Logger.class.getName(), Level.INFO, new SimpleMessage("Hello, world"), null,
        mdc, null, "Thread1", elements[0], System.currentTimeMillis());
    final StringBuilder buf = new StringBuilder();
    for (final PatternFormatter formatter : formatters) {
        formatter.format(event, buf);
    }
    final String str = buf.toString();
    final String expected = "INFO  [PatternParserTest        :96  ] - Hello, world" + Constants.LINE_SEP;
    assertTrue("Expected to end with: " + expected + ". Actual: " + str, str.endsWith(expected));
}
 
开发者ID:OuZhencong,项目名称:log4j2,代码行数:23,代码来源:PatternParserTest.java

示例11: testNestedPattern

import org.apache.logging.log4j.core.impl.Log4jLogEvent; //导入依赖的package包/类
@Test
public void testNestedPattern() {
    final List<PatternFormatter> formatters = parser.parse(nestedPattern);
    assertNotNull(formatters);
    final Throwable t = new Throwable();
    final StackTraceElement[] elements = t.getStackTrace();
    final LogEvent event = new Log4jLogEvent("org.apache.logging.log4j.PatternParserTest", MarkerManager.getMarker("TEST"),
            Logger.class.getName(), Level.INFO, new SimpleMessage("Hello, world"), null, null, null, "Thread1", elements[0],
            System.currentTimeMillis());
    final StringBuilder buf = new StringBuilder();
    for (final PatternFormatter formatter : formatters) {
        formatter.format(event, buf);
    }
    final String str = buf.toString();
    final String expected = String.format("] INFO : Hello, world%s\u001B[m", Constants.LINE_SEP);
    assertTrue(" Expected to end with: " + expected + ". Actual: " + str, str.endsWith(expected));
}
 
开发者ID:OuZhencong,项目名称:log4j2,代码行数:18,代码来源:PatternParserTest.java

示例12: testSuffix

import org.apache.logging.log4j.core.impl.Log4jLogEvent; //导入依赖的package包/类
@Test
public void testSuffix() {
    final String suffix = "suffix(test suffix)";
    final String[] options = {suffix};
    final ExtendedThrowablePatternConverter converter = ExtendedThrowablePatternConverter.newInstance(null, options);
    final Throwable cause = new NullPointerException("null pointer");
    final Throwable parent = new IllegalArgumentException("IllegalArgument", cause);
    final LogEvent event = Log4jLogEvent.newBuilder() //
            .setLoggerName("testLogger") //
            .setLoggerFqcn(this.getClass().getName()) //
            .setLevel(Level.DEBUG) //
            .setMessage(new SimpleMessage("test exception")) //
            .setThrown(parent).build();
    final StringBuilder sb = new StringBuilder();
    converter.format(event, sb);
    final String result = sb.toString();
    assertTrue("No suffix", result.contains("test suffix"));
}
 
开发者ID:apache,项目名称:logging-log4j2,代码行数:19,代码来源:ExtendedThrowablePatternConverterTest.java

示例13: testConverter

import org.apache.logging.log4j.core.impl.Log4jLogEvent; //导入依赖的package包/类
@Test
public void testConverter() {

    final MapMessage msg = new MapMessage();
    msg.put("subject", "I");
    msg.put("verb", "love");
    msg.put("object", "Log4j");
    final MapPatternConverter converter = MapPatternConverter.newInstance(null);
    final LogEvent event = new Log4jLogEvent("MyLogger", null, null, Level.DEBUG, msg, null);
    final StringBuilder sb = new StringBuilder();
    converter.format(event, sb);
    final String str = sb.toString();
    String expected = "subject=I";
    assertTrue("Missing or incorrect subject. Expected " + expected + ", actual " + str, str.contains(expected));
    expected = "verb=love";
    assertTrue("Missing or incorrect verb", str.contains(expected));
    expected = "object=Log4j";
    assertTrue("Missing or incorrect object", str.contains(expected));
}
 
开发者ID:OuZhencong,项目名称:log4j2,代码行数:20,代码来源:MapPatternConverterTest.java

示例14: testSuffixWillIgnoreThrowablePattern

import org.apache.logging.log4j.core.impl.Log4jLogEvent; //导入依赖的package包/类
@Test
public void testSuffixWillIgnoreThrowablePattern() {
    final String suffix = "suffix(%xEx{suffix(inner suffix)})";
    final String[] options = {suffix};
    final RootThrowablePatternConverter converter = RootThrowablePatternConverter.newInstance(null, options);
    final Throwable cause = new NullPointerException("null pointer");
    final Throwable parent = new IllegalArgumentException("IllegalArgument", cause);
    final LogEvent event = Log4jLogEvent.newBuilder() //
            .setLoggerName("testLogger") //
            .setLoggerFqcn(this.getClass().getName()) //
            .setLevel(Level.DEBUG) //
            .setMessage(new SimpleMessage("test exception")) //
            .setThrown(parent).build();
    final StringBuilder sb = new StringBuilder();
    converter.format(event, sb);
    final String result = sb.toString();
    assertFalse("Has unexpected suffix", result.contains("inner suffix"));
}
 
开发者ID:apache,项目名称:logging-log4j2,代码行数:19,代码来源:RootThrowablePatternConverterTest.java

示例15: testFull

import org.apache.logging.log4j.core.impl.Log4jLogEvent; //导入依赖的package包/类
@Test
public void testFull() {
    final ExtendedThrowablePatternConverter converter = ExtendedThrowablePatternConverter.newInstance(null);
    final Throwable cause = new NullPointerException("null pointer");
    final Throwable parent = new IllegalArgumentException("IllegalArgument", cause);
    final LogEvent event = new Log4jLogEvent("testLogger", null, this.getClass().getName(), Level.DEBUG,
        new SimpleMessage("test exception"), parent);
    final StringBuilder sb = new StringBuilder();
    converter.format(event, sb);
    final StringWriter sw = new StringWriter();
    final PrintWriter pw = new PrintWriter(sw);
    parent.printStackTrace(pw);
    String result = sb.toString();
    result = result.replaceAll(" ~?\\[.*\\]", "");
    final String expected = sw.toString().replaceAll("\r", "");
    assertEquals(expected, result);
}
 
开发者ID:OuZhencong,项目名称:log4j2,代码行数:18,代码来源:ExtendedThrowablePatternConverterTest.java


注:本文中的org.apache.logging.log4j.core.impl.Log4jLogEvent类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。