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());
}
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);
}
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]);
}
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());
}
}
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;
}
Aggregations