Search in sources :

Example 81 with Message

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

the class Log4jLogEventTest method testBuilderCorrectlyCopiesMutableLogEvent.

@Test
public void testBuilderCorrectlyCopiesMutableLogEvent() throws Exception {
    final StringMap contextData = new SortedArrayStringMap();
    contextData.putValue("A", "B");
    final ContextStack contextStack = ThreadContext.getImmutableStack();
    final Exception exception = new Exception("test");
    final Marker marker = MarkerManager.getMarker("EVENTTEST");
    final Message message = new SimpleMessage("foo");
    new StackTraceElement("A", "B", "file", 123);
    final String fqcn = "qualified";
    final String name = "Ceci n'est pas une pipe";
    final String threadName = "threadName";
    final MutableLogEvent event = new MutableLogEvent();
    event.setContextData(contextData);
    event.setContextStack(contextStack);
    event.setEndOfBatch(true);
    event.setIncludeLocation(true);
    // event.setSource(stackTraceElement); // cannot be explicitly set
    event.setLevel(Level.FATAL);
    event.setLoggerFqcn(fqcn);
    event.setLoggerName(name);
    event.setMarker(marker);
    event.setMessage(message);
    event.setNanoTime(1234567890L);
    event.setThreadName(threadName);
    event.setThrown(exception);
    event.setTimeMillis(987654321L);
    assertSame(contextData, event.getContextData());
    assertSame(contextStack, event.getContextStack());
    assertTrue(event.isEndOfBatch());
    assertTrue(event.isIncludeLocation());
    assertSame(Level.FATAL, event.getLevel());
    assertSame(fqcn, event.getLoggerFqcn());
    assertSame(name, event.getLoggerName());
    assertSame(marker, event.getMarker());
    assertSame(message, event.getMessage());
    assertEquals(1234567890L, event.getNanoTime());
    // assertSame(stackTraceElement, event.getSource()); // don't invoke
    assertSame(threadName, event.getThreadName());
    assertSame(exception, event.getThrown());
    assertEquals(987654321L, event.getTimeMillis());
    final LogEvent e2 = new Log4jLogEvent.Builder(event).build();
    assertEquals(contextData, e2.getContextData());
    assertSame(contextStack, e2.getContextStack());
    assertTrue(e2.isEndOfBatch());
    assertTrue(e2.isIncludeLocation());
    assertSame(Level.FATAL, e2.getLevel());
    assertSame(fqcn, e2.getLoggerFqcn());
    assertSame(name, e2.getLoggerName());
    assertSame(marker, e2.getMarker());
    assertSame(message, e2.getMessage());
    assertEquals(1234567890L, e2.getNanoTime());
    // assertSame(stackTraceElement, e2.getSource()); // don't invoke
    assertSame(threadName, e2.getThreadName());
    assertSame(exception, e2.getThrown());
    assertEquals(987654321L, e2.getTimeMillis());
    // use reflection to get value of source field in log event copy:
    // invoking the getSource() method would initialize the field
    final Field fieldSource = Log4jLogEvent.class.getDeclaredField("source");
    fieldSource.setAccessible(true);
    final Object value = fieldSource.get(e2);
    assertNull(value, "source in copy");
}
Also used : StringMap(org.apache.logging.log4j.util.StringMap) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) ReusableObjectMessage(org.apache.logging.log4j.message.ReusableObjectMessage) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) ReusableMessage(org.apache.logging.log4j.message.ReusableMessage) Message(org.apache.logging.log4j.message.Message) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Marker(org.apache.logging.log4j.Marker) ContextStack(org.apache.logging.log4j.ThreadContext.ContextStack) IOException(java.io.IOException) Field(java.lang.reflect.Field) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) ClockFactoryTest(org.apache.logging.log4j.core.time.ClockFactoryTest) Test(org.junit.jupiter.api.Test)

Example 82 with Message

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

the class MessageAttributeConverterTest method testConvert01.

@Test
public void testConvert01() {
    final Message message = LOGGER.getMessageFactory().newMessage("Message #{} said [{}].", 3, "Hello");
    final String converted = this.converter.convertToDatabaseColumn(message);
    assertNotNull("The converted value should not be null.", converted);
    assertEquals("The converted value is not correct.", "Message #3 said [Hello].", converted);
    final Message reversed = this.converter.convertToEntityAttribute(converted);
    assertNotNull("The reversed value should not be null.", reversed);
    assertEquals("The reversed value is not correct.", "Message #3 said [Hello].", reversed.getFormattedMessage());
}
Also used : Message(org.apache.logging.log4j.message.Message) Test(org.junit.Test)

Example 83 with Message

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

the class LevelTest method testLevelLogging.

@Test
public void testLevelLogging(final LoggerContext context) {
    final Marker marker = MarkerManager.getMarker("marker");
    final Message msg = new ObjectMessage("msg");
    final Throwable t = new Throwable("test");
    final Level[] levels = new Level[] { Level.TRACE, Level.DEBUG, Level.INFO, Level.WARN, Level.ERROR, Level.FATAL };
    final String[] names = new String[] { "levelTest", "levelTest.Trace", "levelTest.Debug", "levelTest.Info", "levelTest.Warn", "levelTest.Error", "levelTest.Fatal" };
    for (final Level level : levels) {
        for (final String name : names) {
            final Logger logger = context.getLogger(name);
            // Message
            logger.log(level, msg);
            // Object
            logger.log(level, 123);
            // String
            logger.log(level, name);
            // Marker, Message
            logger.log(level, marker, msg);
            // Marker, Object
            logger.log(level, marker, 123);
            // marker, String
            logger.log(level, marker, name);
            // Message, Throwable
            logger.log(level, msg, t);
            // Object, Throwable
            logger.log(level, 123, t);
            // String, Object...
            logger.log(level, name, "param1", "param2");
            // String, Throwable
            logger.log(level, name, t);
            // Marker, Message, Throwable
            logger.log(level, marker, msg, t);
            // Marker, Object, Throwable
            logger.log(level, marker, 123, t);
            // Marker, String, Object...
            logger.log(level, marker, name, "param1", "param2");
            // Marker, String, Throwable
            logger.log(level, marker, name, t);
        }
    }
    // Logger "levelTest" will not receive same events as "levelTest.Trace"
    int levelCount = names.length - 1;
    final int UNIT = 14;
    final Expected[] expectedResults = new Expected[] { // 
    new Expected(listAll, UNIT * levelCount, "TRACE", "All"), // 
    new Expected(listTrace, UNIT * levelCount--, "TRACE", "Trace"), // 
    new Expected(listDebug, UNIT * levelCount--, "DEBUG", "Debug"), // 
    new Expected(listInfo, UNIT * levelCount--, "INFO", "Info"), // 
    new Expected(listWarn, UNIT * levelCount--, "WARN", "Warn"), // 
    new Expected(listError, UNIT * levelCount--, "ERROR", "Error"), // 
    new Expected(listFatal, UNIT * levelCount--, "FATAL", "Fatal") };
    for (final Expected expected : expectedResults) {
        final String description = expected.description;
        final List<LogEvent> events = expected.appender.getEvents();
        assertNotNull(events, description + ": No events");
        assertThat(events, hasSize(expected.expectedEventCount));
        final LogEvent event = events.get(0);
        assertEquals(event.getLevel().name(), expected.expectedInitialEventLevel, description + ": Expected level " + expected.expectedInitialEventLevel + ", got" + event.getLevel());
    }
}
Also used : ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) Message(org.apache.logging.log4j.message.Message) Marker(org.apache.logging.log4j.Marker) Logger(org.apache.logging.log4j.Logger) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) Level(org.apache.logging.log4j.Level) Test(org.junit.jupiter.api.Test)

Example 84 with Message

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

the class LevelPatternConverterTest method testLevelMapWithLengthAndLowerCase.

@Test
public void testLevelMapWithLengthAndLowerCase() {
    final Message msg = new SimpleMessage("Hello");
    LogEvent event = // 
    Log4jLogEvent.newBuilder().setLoggerName(// 
    "MyLogger").setLevel(// 
    Level.DEBUG).setMessage(msg).build();
    final StringBuilder sb = new StringBuilder();
    LevelPatternConverter converter = LevelPatternConverter.newInstance(null);
    converter.format(event, sb);
    assertEquals(Level.DEBUG.toString(), sb.toString());
    final String[] opts = new String[] { "WARN=Warning, length=2, lowerCase=true" };
    converter = LevelPatternConverter.newInstance(opts);
    sb.setLength(0);
    converter.format(event, sb);
    assertEquals("de", sb.toString());
    event = // 
    Log4jLogEvent.newBuilder().setLoggerName(// 
    "MyLogger").setLevel(// 
    Level.WARN).setMessage(msg).build();
    sb.setLength(0);
    converter.format(event, sb);
    assertEquals("Warning", 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.jupiter.api.Test)

Example 85 with Message

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

the class LevelPatternConverterTest method testLevelLength.

private void testLevelLength(final int length, final String debug, final String warn) {
    final Message msg = new SimpleMessage("Hello");
    LogEvent event = // 
    Log4jLogEvent.newBuilder().setLoggerName(// 
    "MyLogger").setLevel(// 
    Level.DEBUG).setMessage(msg).build();
    final StringBuilder sb = new StringBuilder();
    LevelPatternConverter converter = LevelPatternConverter.newInstance(null);
    converter.format(event, sb);
    assertEquals(Level.DEBUG.toString(), sb.toString());
    final String[] opts = new String[] { "length=" + length };
    converter = LevelPatternConverter.newInstance(opts);
    sb.setLength(0);
    converter.format(event, sb);
    assertEquals(debug, sb.toString());
    event = // 
    Log4jLogEvent.newBuilder().setLoggerName(// 
    "MyLogger").setLevel(// 
    Level.WARN).setMessage(msg).build();
    sb.setLength(0);
    converter.format(event, sb);
    assertEquals(warn, 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)

Aggregations

Message (org.apache.logging.log4j.message.Message)128 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)88 LogEvent (org.apache.logging.log4j.core.LogEvent)55 Test (org.junit.jupiter.api.Test)50 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)46 Marker (org.apache.logging.log4j.Marker)26 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)26 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)24 Level (org.apache.logging.log4j.Level)23 Test (org.junit.Test)22 StringMapMessage (org.apache.logging.log4j.message.StringMapMessage)19 StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)18 ContextStack (org.apache.logging.log4j.ThreadContext.ContextStack)17 MapMessage (org.apache.logging.log4j.message.MapMessage)15 StringMap (org.apache.logging.log4j.util.StringMap)15 DummyNanoClock (org.apache.logging.log4j.core.time.internal.DummyNanoClock)9 EntryMessage (org.apache.logging.log4j.message.EntryMessage)9 Map (java.util.Map)8 StringFormattedMessage (org.apache.logging.log4j.message.StringFormattedMessage)8 IOException (java.io.IOException)7