use of org.apache.logging.log4j.core.impl.LogEventFactory 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.core.impl.LogEventFactory 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());
}
}
Aggregations