Search in sources :

Example 56 with LogEvent

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

the class MessagePatternConverterTest method testPattern.

@Test
public void testPattern() throws Exception {
    final MessagePatternConverter converter = MessagePatternConverter.newInstance(null, null);
    Message msg = new SimpleMessage("Hello!");
    LogEvent event = //
    Log4jLogEvent.newBuilder().setLoggerName(//
    "MyLogger").setLevel(//
    Level.DEBUG).setMessage(msg).build();
    StringBuilder sb = new StringBuilder();
    converter.format(event, sb);
    assertEquals("Unexpected result", "Hello!", sb.toString());
    event = //
    Log4jLogEvent.newBuilder().setLoggerName(//
    "MyLogger").setLevel(//
    Level.DEBUG).setMessage(null).build();
    sb = new StringBuilder();
    converter.format(event, sb);
    assertEquals("Incorrect length: " + sb, 0, sb.length());
    msg = new SimpleMessage(null);
    event = //
    Log4jLogEvent.newBuilder().setLoggerName(//
    "MyLogger").setLevel(//
    Level.DEBUG).setMessage(msg).build();
    sb = new StringBuilder();
    converter.format(event, sb);
    assertEquals("Incorrect length: " + sb, 4, sb.length());
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Message(org.apache.logging.log4j.message.Message) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) 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 57 with LogEvent

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

the class NanoTimePatternConverterTest method testConverterAppendsLogEventNanoTimeToStringBuilder.

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

Example 58 with LogEvent

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

the class NdcPatternConverterTest method testConverter.

private void testConverter(final String expected) {
    final Message msg = new SimpleMessage("Hello");
    final NdcPatternConverter converter = NdcPatternConverter.newInstance(null);
    final LogEvent event = //
    Log4jLogEvent.newBuilder().setLoggerName(//
    "MyLogger").setLevel(//
    Level.DEBUG).setMessage(//
    msg).build();
    final StringBuilder sb = new StringBuilder();
    converter.format(event, sb);
    final String str = sb.toString();
    assertEquals(expected, str);
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Message(org.apache.logging.log4j.message.Message) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage)

Example 59 with LogEvent

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

the class PatternParserTest method testPatternTruncateFromEnd.

@Test
public void testPatternTruncateFromEnd() {
    final List<PatternFormatter> formatters = parser.parse(patternTruncateFromEnd);
    assertNotNull(formatters);
    final LogEvent event = //
    Log4jLogEvent.newBuilder().setLoggerName(//
    "org.apache.logging.log4j.PatternParserTest").setLoggerFqcn(//
    Logger.class.getName()).setLevel(//
    Level.INFO).setMessage(//
    new SimpleMessage("Hello, world")).setThreadName(//
    "Thread1").setTimeMillis(//
    System.currentTimeMillis()).build();
    final StringBuilder buf = new StringBuilder();
    for (final PatternFormatter formatter : formatters) {
        formatter.format(event, buf);
    }
    final String str = buf.toString();
    final String expected = "INFO  org.a Hello, world" + Strings.LINE_SEPARATOR;
    assertTrue("Expected to end with: " + expected + ". Actual: " + str, str.endsWith(expected));
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.Test)

Example 60 with LogEvent

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

the class PatternParserTest method testNestedPatternHighlight.

private void testNestedPatternHighlight(final Level level, final String expectedStart) {
    final List<PatternFormatter> formatters = parser.parse(nestedPatternHighlight);
    assertNotNull(formatters);
    final Throwable t = new Throwable();
    t.getStackTrace();
    final LogEvent event = //
    Log4jLogEvent.newBuilder().setLoggerName(//
    "org.apache.logging.log4j.PatternParserTest").setMarker(//
    MarkerManager.getMarker("TEST")).setLoggerFqcn(//
    Logger.class.getName()).setLevel(//
    level).setMessage(//
    new SimpleMessage("Hello, world")).setThreadName(//
    "Thread1").setSource(/*stackTraceElement[0]*/
    null).setTimeMillis(//
    System.currentTimeMillis()).build();
    final StringBuilder buf = new StringBuilder();
    for (final PatternFormatter formatter : formatters) {
        formatter.format(event, buf);
    }
    final String str = buf.toString();
    final String expectedEnd = String.format("] %-5s: Hello, world%s", level, Strings.LINE_SEPARATOR);
    assertTrue("Expected to start with: " + expectedStart + ". Actual: " + str, str.startsWith(expectedStart));
    assertTrue("Expected to end with: \"" + expectedEnd + "\". Actual: \"" + str, str.endsWith(expectedEnd));
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Logger(org.apache.logging.log4j.core.Logger)

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