Search in sources :

Example 16 with LogEvent

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

the class MaxLengthConverterTest method testOverMaxLength.

@Test
public void testOverMaxLength() throws Exception {
    final Message message = new SimpleMessage("01234567890123456789");
    final LogEvent event = Log4jLogEvent.newBuilder().setLoggerName("MyLogger").setLevel(Level.DEBUG).setMessage(message).build();
    final StringBuilder sb = new StringBuilder();
    converter.format(event, sb);
    assertEquals("0123456789", sb.toString());
}
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) Test(org.junit.Test)

Example 17 with LogEvent

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

the class MdcPatternConverterTest method testConverterWithKeys.

@Test
public void testConverterWithKeys() {
    final Message msg = new SimpleMessage("Hello");
    final String[] options = new String[] { "object, subject" };
    final MdcPatternConverter converter = MdcPatternConverter.newInstance(options);
    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();
    final String expected = "{object=Log4j, subject=I}";
    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) Test(org.junit.Test)

Example 18 with LogEvent

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

the class MdcPatternConverterTest method testConverterWithKey.

@Test
public void testConverterWithKey() {
    final Message msg = new SimpleMessage("Hello");
    final String[] options = new String[] { "object" };
    final MdcPatternConverter converter = MdcPatternConverter.newInstance(options);
    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();
    final String expected = "Log4j";
    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) Test(org.junit.Test)

Example 19 with LogEvent

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

the class MdcPatternConverterTest method testConverter.

@Test
public void testConverter() {
    final Message msg = new SimpleMessage("Hello");
    final MdcPatternConverter converter = MdcPatternConverter.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();
    final String expected = "{object=Log4j, subject=I, verb=love}";
    assertTrue("Incorrect result. Expected " + expected + ", actual " + str, str.equals(expected));
}
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) Test(org.junit.Test)

Example 20 with LogEvent

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

the class MessagePatternConverterTest method testPatternWithConfiguration.

@Test
public void testPatternWithConfiguration() throws Exception {
    final Configuration config = new DefaultConfiguration();
    final MessagePatternConverter converter = MessagePatternConverter.newInstance(config, 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 : DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) Configuration(org.apache.logging.log4j.core.config.Configuration) 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) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) 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