Search in sources :

Example 76 with Message

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

the class MaxLengthConverterTest method testUnderMaxLength.

@Test
public void testUnderMaxLength() throws Exception {
    final Message message = new SimpleMessage("0123456789");
    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 77 with Message

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

the class LoggerTest method paramWithExceptionTest.

@Test
public void paramWithExceptionTest() throws Exception {
    logger.error("Throwing with parameters {}", "TestParam", new NullPointerException("Test Exception"));
    final List<LogEvent> events = app.getEvents();
    assertNotNull("Log event list not returned", events);
    assertTrue("Incorrect number of log events: expected 1, actual " + events.size(), events.size() == 1);
    final LogEvent event = events.get(0);
    final Throwable thrown = event.getThrown();
    assertNotNull("No throwable present in log event", thrown);
    final Message msg = event.getMessage();
    assertTrue("Incorrect message type. Expected ParameterizedMessage/ReusableParameterizedMessage, actual " + msg.getClass().getSimpleName(), msg instanceof ParameterizedMessage || msg instanceof ReusableParameterizedMessage);
}
Also used : ReusableParameterizedMessage(org.apache.logging.log4j.message.ReusableParameterizedMessage) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Message(org.apache.logging.log4j.message.Message) StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) ReusableParameterizedMessage(org.apache.logging.log4j.message.ReusableParameterizedMessage) ReusableParameterizedMessage(org.apache.logging.log4j.message.ReusableParameterizedMessage) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Test(org.junit.Test)

Example 78 with Message

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

the class LoggerConfigTest method testPropertiesWithoutSubstitution.

@SuppressWarnings({ "deprecation", "unchecked" })
@Test
public void testPropertiesWithoutSubstitution() {
    assertNull("null propertiesList", createForProperties(null).getPropertyList());
    assertNull("null property Map", createForProperties(null).getProperties());
    final Property[] all = new Property[] { Property.createProperty("key1", "value1"), Property.createProperty("key2", "value2") };
    final LoggerConfig loggerConfig = createForProperties(all);
    final List<Property> list = loggerConfig.getPropertyList();
    assertEquals("map and list contents equal", new HashSet(list), loggerConfig.getProperties().keySet());
    final Object[] actualList = new Object[1];
    loggerConfig.setLogEventFactory(new LogEventFactory() {

        @Override
        public LogEvent createEvent(final String loggerName, final Marker marker, final String fqcn, final Level level, final Message data, final List<Property> properties, final Throwable t) {
            actualList[0] = properties;
            return new Log4jLogEvent(System.currentTimeMillis());
        }
    });
    loggerConfig.log("name", "fqcn", null, Level.INFO, new SimpleMessage("msg"), null);
    assertSame("propertiesList passed in as is if no substitutions required", list, actualList[0]);
}
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) LogEventFactory(org.apache.logging.log4j.core.impl.LogEventFactory) Marker(org.apache.logging.log4j.Marker) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) Level(org.apache.logging.log4j.Level) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 79 with Message

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

the class LoggerConfigTest method testPropertiesWithSubstitution.

@SuppressWarnings({ "deprecation", "unchecked" })
@Test
public void testPropertiesWithSubstitution() {
    final Property[] all = new Property[] { Property.createProperty("key1", "value1-${sys:user.name}"), Property.createProperty("key2", "value2-${sys:user.name}") };
    final LoggerConfig loggerConfig = createForProperties(all);
    final List<Property> list = loggerConfig.getPropertyList();
    assertEquals("map and list contents equal", new HashSet(list), loggerConfig.getProperties().keySet());
    final Object[] actualListHolder = new Object[1];
    loggerConfig.setLogEventFactory(new LogEventFactory() {

        @Override
        public LogEvent createEvent(final String loggerName, final Marker marker, final String fqcn, final Level level, final Message data, final List<Property> properties, final Throwable t) {
            actualListHolder[0] = properties;
            return new Log4jLogEvent(System.currentTimeMillis());
        }
    });
    loggerConfig.log("name", "fqcn", null, Level.INFO, new SimpleMessage("msg"), null);
    assertNotSame("propertiesList with substitutions", list, actualListHolder[0]);
    final List<Property> actualList = (List<Property>) actualListHolder[0];
    for (int i = 0; i < list.size(); i++) {
        assertEquals("name[" + i + "]", list.get(i).getName(), actualList.get(i).getName());
        final String value = list.get(i).getValue().replace("${sys:user.name}", System.getProperty("user.name"));
        assertEquals("value[" + i + "]", value, actualList.get(i).getValue());
    }
}
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) LogEventFactory(org.apache.logging.log4j.core.impl.LogEventFactory) Marker(org.apache.logging.log4j.Marker) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) Level(org.apache.logging.log4j.Level) List(java.util.List) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 80 with Message

use of org.apache.logging.log4j.message.Message in project cas by apereo.

the class LoggingUtils method prepareLogEvent.

/**
 * Prepare log event log event.
 *
 * @param logEvent the log event
 * @return the log event
 */
public static LogEvent prepareLogEvent(final LogEvent logEvent) {
    final String messageModified = TicketIdSanitizationUtils.sanitize(logEvent.getMessage().getFormattedMessage());
    final Message message = new SimpleMessage(messageModified);
    final LogEvent newLogEvent = Log4jLogEvent.newBuilder().setLevel(logEvent.getLevel()).setLoggerName(logEvent.getLoggerName()).setLoggerFqcn(logEvent.getLoggerFqcn()).setContextData(new SortedArrayStringMap(logEvent.getContextData())).setContextStack(logEvent.getContextStack()).setEndOfBatch(logEvent.isEndOfBatch()).setIncludeLocation(logEvent.isIncludeLocation()).setMarker(logEvent.getMarker()).setMessage(message).setNanoTime(logEvent.getNanoTime()).setSource(logEvent.getSource()).setThreadName(logEvent.getThreadName()).setThrownProxy(logEvent.getThrownProxy()).setThrown(logEvent.getThrown()).setTimeMillis(logEvent.getTimeMillis()).build();
    return newLogEvent;
}
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) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap)

Aggregations

Message (org.apache.logging.log4j.message.Message)80 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)45 Test (org.junit.Test)42 LogEvent (org.apache.logging.log4j.core.LogEvent)33 Marker (org.apache.logging.log4j.Marker)23 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)22 Level (org.apache.logging.log4j.Level)17 ContextStack (org.apache.logging.log4j.ThreadContext.ContextStack)14 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)14 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)13 EntryMessage (org.apache.logging.log4j.message.EntryMessage)9 StringFormattedMessage (org.apache.logging.log4j.message.StringFormattedMessage)8 StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)8 Result (org.apache.logging.log4j.core.Filter.Result)7 LocalizedMessage (org.apache.logging.log4j.message.LocalizedMessage)6 ReusableMessage (org.apache.logging.log4j.message.ReusableMessage)6 IOException (java.io.IOException)5 ClockFactoryTest (org.apache.logging.log4j.core.util.ClockFactoryTest)5 ReusableObjectMessage (org.apache.logging.log4j.message.ReusableObjectMessage)5 MutableThreadContextStack (org.apache.logging.log4j.spi.MutableThreadContextStack)5