本文整理汇总了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;
}
示例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();
}
示例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();
}
示例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());
}
示例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());
}
示例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());
}
示例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);
}
示例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"));
}
示例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());
}
示例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));
}
示例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));
}
示例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"));
}
示例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));
}
示例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"));
}
示例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);
}