Search in sources :

Example 46 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage 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)

Example 47 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.

the class RootThrowablePatternConverterTest method testFull2.

/**
     * Sanity check for testFull1() above, makes sure that the way testFull1 is written matches actually throwing
     * exceptions.
     */
@Test
public void testFull2() {
    final RootThrowablePatternConverter converter = RootThrowablePatternConverter.newInstance(null, null);
    Throwable parent;
    try {
        try {
            throw new NullPointerException("null pointer");
        } catch (final NullPointerException e) {
            throw new IllegalArgumentException("IllegalArgument", e);
        }
    } catch (final IllegalArgumentException e) {
        parent = e;
    }
    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();
    // System.out.print(result);
    assertTrue("Missing Exception", result.contains("Wrapped by: java.lang.IllegalArgumentException: IllegalArgument"));
    assertTrue("Incorrect start of msg", result.startsWith("java.lang.NullPointerException: null pointer"));
}
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 48 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.

the class RootThrowablePatternConverterTest method testFull1.

@Test
public void testFull1() {
    final RootThrowablePatternConverter converter = RootThrowablePatternConverter.newInstance(null, null);
    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();
    // System.out.print(result);
    assertTrue("Missing Exception", result.contains("Wrapped by: java.lang.IllegalArgumentException: IllegalArgument"));
    assertTrue("Incorrect start of msg", result.startsWith("java.lang.NullPointerException: null pointer"));
}
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 49 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.

the class ThrowablePatternConverterTest method testFull.

@Test
public void testFull() {
    final String[] options = { "full" };
    final ThrowablePatternConverter converter = ThrowablePatternConverter.newInstance(null, options);
    Throwable parent;
    try {
        try {
            throw new NullPointerException("null pointer");
        } catch (final NullPointerException e) {
            throw new IllegalArgumentException("IllegalArgument", e);
        }
    } catch (final IllegalArgumentException e) {
        parent = e;
    }
    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();
    // System.out.print(result);
    assertTrue("Incorrect start of msg", result.startsWith("java.lang.IllegalArgumentException: IllegalArgument"));
    assertTrue("Missing nested exception", result.contains("java.lang.NullPointerException: null pointer"));
}
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 50 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.

the class ThrowablePatternConverterTest method testFullWithSuffix.

@Test
public void testFullWithSuffix() {
    final String[] options = { "full", "suffix(test suffix)" };
    final ThrowablePatternConverter converter = ThrowablePatternConverter.newInstance(null, options);
    Throwable parent;
    try {
        try {
            throw new NullPointerException("null pointer");
        } catch (final NullPointerException e) {
            throw new IllegalArgumentException("IllegalArgument", e);
        }
    } catch (final IllegalArgumentException e) {
        parent = e;
    }
    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 of full stack trace should end with the specified 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)

Aggregations

SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)130 Test (org.junit.Test)98 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)96 LogEvent (org.apache.logging.log4j.core.LogEvent)93 Message (org.apache.logging.log4j.message.Message)34 Marker (org.apache.logging.log4j.Marker)19 Level (org.apache.logging.log4j.Level)11 ContextStack (org.apache.logging.log4j.ThreadContext.ContextStack)10 IOException (java.io.IOException)7 LoggerContext (org.apache.logging.log4j.core.LoggerContext)7 ClockFactoryTest (org.apache.logging.log4j.core.util.ClockFactoryTest)6 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)6 HashMap (java.util.HashMap)5 ReusableMessage (org.apache.logging.log4j.message.ReusableMessage)5 ReusableObjectMessage (org.apache.logging.log4j.message.ReusableObjectMessage)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 ObjectInputStream (java.io.ObjectInputStream)4 ObjectOutputStream (java.io.ObjectOutputStream)4 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)4