Search in sources :

Example 61 with LogEvent

use of org.apache.logging.log4j.core.LogEvent in project logging-log4j2 by apache.

the class ThreadIdPatternConverterTest method testConverterAppendsLogEventNanoTimeToStringBuilder.

@Test
public void testConverterAppendsLogEventNanoTimeToStringBuilder() {
    final LogEvent event = //
    Log4jLogEvent.newBuilder().setThreadId(1).build();
    final StringBuilder sb = new StringBuilder();
    final ThreadIdPatternConverter converter = ThreadIdPatternConverter.newInstance(null);
    converter.format(event, sb);
    assertEquals("1", sb.toString());
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) Test(org.junit.Test)

Example 62 with LogEvent

use of org.apache.logging.log4j.core.LogEvent in project logging-log4j2 by apache.

the class ThreadPriorityPatternConverterTest method testConverterAppendsLogEventNanoTimeToStringBuilder.

@Test
public void testConverterAppendsLogEventNanoTimeToStringBuilder() {
    final LogEvent event = //
    Log4jLogEvent.newBuilder().setThreadPriority(1).build();
    final StringBuilder sb = new StringBuilder();
    final ThreadPriorityPatternConverter converter = ThreadPriorityPatternConverter.newInstance(null);
    converter.format(event, sb);
    assertEquals("1", sb.toString());
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) Test(org.junit.Test)

Example 63 with LogEvent

use of org.apache.logging.log4j.core.LogEvent in project logging-log4j2 by apache.

the class ThrowablePatternConverterTest method testShortOptionWithSuffix.

@Test
public void testShortOptionWithSuffix() {
    final String packageName = "org.apache.logging.log4j.core.pattern.";
    final String[] options = { "short.className", "suffix(test suffix)" };
    final ThrowablePatternConverter converter = ThrowablePatternConverter.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("Each line should end with suffix", everyLineEndsWith(result, "test suffix"));
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.Test)

Example 64 with LogEvent

use of org.apache.logging.log4j.core.LogEvent in project logging-log4j2 by apache.

the class RegexReplacementConverterTest method testReplacement.

@Test
public void testReplacement() {
    ThreadContext.put("MyKey", "Apache");
    final LogEvent event = //
    Log4jLogEvent.newBuilder().setLoggerName(//
    RegexReplacementConverterTest.class.getName()).setLevel(//
    Level.DEBUG).setMessage(//
    new SimpleMessage("This is a test")).build();
    final StringBuilder sb = new StringBuilder();
    final LoggerContext ctx = LoggerContext.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" + Strings.LINE_SEPARATOR, sb.toString());
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Test(org.junit.Test)

Example 65 with LogEvent

use of org.apache.logging.log4j.core.LogEvent in project logging-log4j2 by apache.

the class RootThrowablePatternConverterTest method testSuffixWillIgnoreThrowablePattern.

@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"));
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.Test)

Aggregations

LogEvent (org.apache.logging.log4j.core.LogEvent)189 Test (org.junit.Test)150 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)127 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)94 Message (org.apache.logging.log4j.message.Message)33 Marker (org.apache.logging.log4j.Marker)16 StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)11 HashMap (java.util.HashMap)9 LoggerContext (org.apache.logging.log4j.core.LoggerContext)8 ClockFactoryTest (org.apache.logging.log4j.core.util.ClockFactoryTest)8 File (java.io.File)7 IOException (java.io.IOException)7 Level (org.apache.logging.log4j.Level)7 Appender (org.apache.logging.log4j.core.Appender)6 MapMessage (org.apache.logging.log4j.message.MapMessage)6 ContextStack (org.apache.logging.log4j.ThreadContext.ContextStack)5 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)5 ReusableMessage (org.apache.logging.log4j.message.ReusableMessage)5 ReusableObjectMessage (org.apache.logging.log4j.message.ReusableObjectMessage)5 ListAppender (org.apache.logging.log4j.test.appender.ListAppender)5