Search in sources :

Example 1 with LogEventFactory

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]);
}
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 2 with LogEventFactory

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

Aggregations

HashSet (java.util.HashSet)2 Level (org.apache.logging.log4j.Level)2 Marker (org.apache.logging.log4j.Marker)2 LogEvent (org.apache.logging.log4j.core.LogEvent)2 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)2 LogEventFactory (org.apache.logging.log4j.core.impl.LogEventFactory)2 Message (org.apache.logging.log4j.message.Message)2 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)2 Test (org.junit.Test)2 List (java.util.List)1