Search in sources :

Example 71 with SimpleMessage

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

the class LambdaUtilTest method testGetMessageSupplierResultOfSupplier.

@Test
public void testGetMessageSupplierResultOfSupplier() {
    final Message expected = new SimpleMessage("hi");
    final Message actual = LambdaUtil.get(new MessageSupplier() {

        @Override
        public Message get() {
            return expected;
        }
    });
    assertSame(expected, actual);
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Message(org.apache.logging.log4j.message.Message) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.Test)

Example 72 with SimpleMessage

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

the class LoggerSupplierTest method flowTracing_SupplierOfSimpleMessage.

@Test
public void flowTracing_SupplierOfSimpleMessage() {
    logger.traceEntry(new Supplier<SimpleMessage>() {

        @Override
        public SimpleMessage get() {
            return new SimpleMessage("1234567890");
        }
    });
    assertEquals(1, results.size());
    assertThat("Incorrect Entry", results.get(0), startsWith("ENTER[ FLOW ] TRACE Enter"));
    assertThat("Missing entry data", results.get(0), containsString("(1234567890)"));
    assertThat("Bad toString()", results.get(0), not(containsString("SimpleMessage")));
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.Test)

Example 73 with SimpleMessage

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

the class LoggerNameLevelRewritePolicyTest method testUpdate.

@Test
public void testUpdate() {
    final KeyValuePair[] rewrite = new KeyValuePair[] { new KeyValuePair("INFO", "DEBUG"), new KeyValuePair("WARN", "INFO") };
    final String loggerNameRewrite = "com.foo.bar";
    LogEvent logEvent = Log4jLogEvent.newBuilder().setLoggerName(loggerNameRewrite).setLoggerFqcn("LoggerNameLevelRewritePolicyTest.testUpdate()").setLevel(Level.INFO).setMessage(new SimpleMessage("Test")).setThrown(new RuntimeException("test")).setThreadName("none").setTimeMillis(1).build();
    final LoggerNameLevelRewritePolicy updatePolicy = LoggerNameLevelRewritePolicy.createPolicy(loggerNameRewrite, rewrite);
    LogEvent rewritten = updatePolicy.rewrite(logEvent);
    Assert.assertEquals(Level.DEBUG, rewritten.getLevel());
    logEvent = Log4jLogEvent.newBuilder().setLoggerName(loggerNameRewrite).setLoggerFqcn("LoggerNameLevelRewritePolicyTest.testUpdate()").setLevel(Level.WARN).setMessage(new SimpleMessage("Test")).setThrown(new RuntimeException("test")).setThreadName("none").setTimeMillis(1).build();
    rewritten = updatePolicy.rewrite(logEvent);
    Assert.assertEquals(Level.INFO, rewritten.getLevel());
    final String loggerNameReadOnly = "com.nochange";
    logEvent = Log4jLogEvent.newBuilder().setLoggerName(loggerNameReadOnly).setLoggerFqcn("LoggerNameLevelRewritePolicyTest.testUpdate()").setLevel(Level.INFO).setMessage(new SimpleMessage("Test")).setThrown(new RuntimeException("test")).setThreadName("none").setTimeMillis(1).build();
    rewritten = updatePolicy.rewrite(logEvent);
    Assert.assertEquals(Level.INFO, rewritten.getLevel());
    logEvent = Log4jLogEvent.newBuilder().setLoggerName(loggerNameReadOnly).setLoggerFqcn("LoggerNameLevelRewritePolicyTest.testUpdate()").setLevel(Level.WARN).setMessage(new SimpleMessage("Test")).setThrown(new RuntimeException("test")).setThreadName("none").setTimeMillis(1).build();
    rewritten = updatePolicy.rewrite(logEvent);
    Assert.assertEquals(Level.WARN, rewritten.getLevel());
}
Also used : KeyValuePair(org.apache.logging.log4j.core.util.KeyValuePair) 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 74 with SimpleMessage

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

the class ThrowablePatternConverterTest method testShortFileName.

@Test
public void testShortFileName() {
    final String[] options = { "short.fileName" };
    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();
    assertEquals("The file names should be same", "ThrowablePatternConverterTest.java", result);
}
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 75 with SimpleMessage

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

the class ThrowablePatternConverterTest method testShortMethodName.

@Test
public void testShortMethodName() {
    final String[] options = { "short.methodName" };
    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();
    assertEquals("The method names should be same", "testShortMethodName", result);
}
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