@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); 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());
@Test public void testIsValueNeedsLookup() { assertTrue("with ${ as value", Property.createProperty("", "${").isValueNeedsLookup()); assertTrue("with ${ in value", Property.createProperty("", "blah${blah").isValueNeedsLookup()); assertFalse("empty value", Property.createProperty("", "").isValueNeedsLookup()); assertFalse("without ${ in value", Property.createProperty("", "blahblah").isValueNeedsLookup()); assertFalse("without $ in value", Property.createProperty("", "blahb{sys:lah").isValueNeedsLookup()); assertFalse("without { in value", Property.createProperty("", "blahb$sys:lah").isValueNeedsLookup()); } }
@Test public void testNullValueIsConvertedToEmptyString() { // LOG4J2-1313 <Property name="x" /> support assertEquals("", Property.createProperty("name", null).getValue()); }
header.getName(), header.isValueNeedsLookup() ? getConfiguration().getStrSubstitutor().replace(event, header.getValue()) : header.getValue());
for (int i = 0; i < properties.size(); i++) { final Property prop = properties.get(i); final String value = prop.isValueNeedsLookup() // since LOG4J2-1575 ? config.getStrSubstitutor().replace(event, prop.getValue()) // : prop.getValue(); props.add(Property.createProperty(prop.getName(), value));
@Test public void testH2Properties() throws SQLException { Property[] properties = new Property[] { // @formatter:off Property.createProperty("username", JdbcH2TestHelper.USER_NAME), Property.createProperty("password", JdbcH2TestHelper.PASSWORD), // @formatter:on }; // @formatter:off DriverManagerConnectionSource source = DriverManagerConnectionSource.newBuilder() .setConnectionString(JdbcH2TestHelper.CONNECTION_STRING) .setProperties(properties) .build(); // @formatter:on try (Connection conn = source.getConnection()) { Assert.assertFalse(conn.isClosed()); } }
@Override public String toString() { return name + '=' + getValue(); } }
private static boolean containsPropertyRequiringLookup(final Property[] properties) { if (properties == null) { return false; } for (int i = 0; i < properties.length; i++) { if (properties[i].isValueNeedsLookup()) { return true; } } return false; }
/** * Creates a Property. * * @param name The key. * @param value The value. * @return A Property. */ @PluginFactory public static Property createProperty( @PluginAttribute("name") final String name, @PluginValue("value") final String value) { if (name == null) { LOGGER.error("Property name cannot be null"); } return new Property(name, value); }
public KafkaManager(final LoggerContext loggerContext, final String name, final String topic, final boolean syncSend, final Property[] properties) { super(loggerContext, name); this.topic = Objects.requireNonNull(topic, "topic"); this.syncSend = syncSend; config.setProperty("key.serializer", "org.apache.kafka.common.serialization.ByteArraySerializer"); config.setProperty("value.serializer", "org.apache.kafka.common.serialization.ByteArraySerializer"); config.setProperty("batch.size", "0"); for (final Property property : properties) { config.setProperty(property.getName(), property.getValue()); } this.timeoutMillis = Integer.parseInt(config.getProperty("timeout.ms", DEFAULT_TIMEOUT_MILLIS)); }
if (contextData.getValue(prop.getName()) != null) { continue; // contextMap overrides config properties final String value = prop.isValueNeedsLookup() // ? privateConfig.config.getStrSubstitutor().replace(event, prop.getValue()) // : prop.getValue(); contextData.putValue(prop.getName(), value);
@SuppressWarnings({"deprecation"}) @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), new HashSet<>(loggerConfig.getPropertyList())); 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 Builder().setTimeMillis(System.currentTimeMillis()).build(); } }); loggerConfig.log("name", "fqcn", null, Level.INFO, new SimpleMessage("msg"), null); assertSame("propertiesList passed in as is if no substitutions required", list, actualList[0]); }
private PropertiesRewritePolicy(final Configuration config, final List<Property> props) { this.config = config; this.properties = new HashMap<>(props.size()); for (final Property property : props) { final Boolean interpolate = Boolean.valueOf(property.getValue().contains("${")); properties.put(property, interpolate); } }
/** * Returns an unmodifiable map with the configuration properties, or {@code null} if this {@code LoggerConfig} does * not have any configuration properties. * <p> * For each {@code Property} key in the map, the value is {@code true} if the property value has a variable that * needs to be substituted. * * @return an unmodifiable map with the configuration properties, or {@code null} * @see Configuration#getStrSubstitutor() * @see StrSubstitutor * @deprecated use {@link #getPropertyList()} instead */ // LOG4J2-157 @Deprecated public Map<Property, Boolean> getProperties() { if (properties == null) { return null; } if (propertiesMap == null) { // lazily initialize: only used by user custom code, not by Log4j any more final Map<Property, Boolean> result = new HashMap<>(properties.size() * 2); for (int i = 0; i < properties.size(); i++) { result.put(properties.get(i), Boolean.valueOf(properties.get(i).isValueNeedsLookup())); } propertiesMap = Collections.unmodifiableMap(result); } return propertiesMap; }
/** * Copies key-value pairs from the specified property list into the specified {@code StringMap}. * * @param properties list of configuration properties, may be {@code null} * @param result the {@code StringMap} object to add the key-values to. Must be non-{@code null}. */ public static void copyProperties(final List<Property> properties, final StringMap result) { if (properties != null) { for (int i = 0; i < properties.size(); i++) { final Property prop = properties.get(i); result.putValue(prop.getName(), prop.getValue()); } } } }
@Test public void testAppendCustomHeader() throws Exception { wireMockRule.stubFor(post(urlEqualTo("/test/log4j/")) .willReturn(SUCCESS_RESPONSE)); final Appender appender = HttpAppender.newBuilder() .withName("Http") .withLayout(JsonLayout.createDefaultLayout()) .setConfiguration(ctx.getConfiguration()) .setUrl(new URL("http://localhost:" + wireMockRule.port() + "/test/log4j/")) .setHeaders(new Property[] { Property.createProperty("X-Test", "header value"), Property.createProperty("X-Runtime", "${java:runtime}") }) .build(); appender.append(createLogEvent()); wireMockRule.verify(postRequestedFor(urlEqualTo("/test/log4j/")) .withHeader("Host", containing("localhost")) .withHeader("X-Test", equalTo("header value")) .withHeader("X-Runtime", equalTo(JAVA_LOOKUP.getRuntime())) .withHeader("Content-Type", containing("application/json")) .withRequestBody(containing("\"message\" : \"" + LOG_MESSAGE + "\""))); }
@Override public String toString() { final StringBuilder sb = new StringBuilder(); sb.append(" {"); boolean first = true; for (final Map.Entry<Property, Boolean> entry : properties.entrySet()) { if (!first) { sb.append(", "); } final Property prop = entry.getKey(); sb.append(prop.getName()).append('=').append(prop.getValue()); first = false; } sb.append('}'); return sb.toString(); }
@Test public void insertConfigurationProperties() { assertThat( new OpenCensusTraceContextDataInjector() .injectContextData( Lists.newArrayList( Property.createProperty("property1", "value1"), Property.createProperty("property2", "value2")), new SortedArrayStringMap()) .toMap()) .containsExactly( "property1", "value1", "property2", "value2", "traceId", "00000000000000000000000000000000", "spanId", "0000000000000000", "traceSampled", "false"); }
/** * Rewrite the event. * @param source a logging event that may be returned or * used to create a new logging event. * @return The LogEvent after rewriting. */ @Override public LogEvent rewrite(final LogEvent source) { final Map<String, String> props = new HashMap<>(source.getContextData().toMap()); for (final Map.Entry<Property, Boolean> entry : properties.entrySet()) { final Property prop = entry.getKey(); props.put(prop.getName(), entry.getValue().booleanValue() ? config.getStrSubstitutor().replace(prop.getValue()) : prop.getValue()); } final LogEvent result = new Log4jLogEvent.Builder(source).setContextMap(props).build(); return result; }