You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rg...@apache.org on 2015/08/03 03:37:27 UTC
[1/2] logging-log4j2 git commit: LOG4J2-1010 - Pass LogEvent when
interpolating logger properties.
Repository: logging-log4j2
Updated Branches:
refs/heads/master e78797358 -> aa5151e19
LOG4J2-1010 - Pass LogEvent when interpolating logger properties.
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/082615f6
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/082615f6
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/082615f6
Branch: refs/heads/master
Commit: 082615f67ff6fef2cc3551c94374f25063c18833
Parents: e787973
Author: Ralph Goers <rg...@nextiva.com>
Authored: Sun Aug 2 18:29:43 2015 -0700
Committer: Ralph Goers <rg...@nextiva.com>
Committed: Sun Aug 2 18:29:43 2015 -0700
----------------------------------------------------------------------
.../logging/log4j/core/config/LoggerConfig.java | 8 +-
log4j-samples/loggerProperties/pom.xml | 55 ++++++++++++
.../logging/log4j/lookup/CustomLookup.java | 84 ++++++++++++++++++
.../logging/log4j/lookup/CustomMapMessage.java | 51 +++++++++++
.../logging/log4j/lookup/MapMessageLookup.java | 90 ++++++++++++++++++++
.../logging/log4j/CustomPropertiesTest.java | 51 +++++++++++
.../src/test/resources/log4j2.xml | 48 +++++++++++
log4j-samples/pom.xml | 1 +
8 files changed, 386 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/082615f6/log4j-core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java
index 2f76d51..01f8f4a 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java
@@ -46,6 +46,7 @@ import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.filter.AbstractFilterable;
import org.apache.logging.log4j.core.impl.DefaultLogEventFactory;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
import org.apache.logging.log4j.core.impl.LogEventFactory;
import org.apache.logging.log4j.core.lookup.StrSubstitutor;
import org.apache.logging.log4j.core.util.Booleans;
@@ -355,11 +356,14 @@ public class LoggerConfig extends AbstractFilterable {
List<Property> props = null;
if (properties != null) {
props = new ArrayList<>(properties.size());
-
+ Log4jLogEvent.Builder builder = new Log4jLogEvent.Builder();
+ builder.setMessage(data).setMarker(marker).setLevel(level).setLoggerName(loggerName);
+ builder.setLoggerFqcn(fqcn).setThrown(t);
+ LogEvent event = builder.build();
for (final Map.Entry<Property, Boolean> entry : properties.entrySet()) {
final Property prop = entry.getKey();
final String value = entry.getValue() ? config.getStrSubstitutor()
- .replace(prop.getValue()) : prop.getValue();
+ .replace(event, prop.getValue()) : prop.getValue();
props.add(Property.createProperty(prop.getName(), value));
}
}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/082615f6/log4j-samples/loggerProperties/pom.xml
----------------------------------------------------------------------
diff --git a/log4j-samples/loggerProperties/pom.xml b/log4j-samples/loggerProperties/pom.xml
new file mode 100644
index 0000000..db20997
--- /dev/null
+++ b/log4j-samples/loggerProperties/pom.xml
@@ -0,0 +1,55 @@
+<?xml version="1.0"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <artifactId>log4j-samples</artifactId>
+ <groupId>org.apache.logging.log4j.samples</groupId>
+ <version>2.4-SNAPSHOT</version>
+ </parent>
+ <artifactId>log4j-samples-loggerProperties</artifactId>
+ <packaging>jar</packaging>
+ <name>Apache Log4j Samples: LoggerProperties</name>
+ <url>http://maven.apache.org</url>
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ </properties>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-core</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ </plugin>
+ </plugins>
+ </build>
+</project>
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/082615f6/log4j-samples/loggerProperties/src/main/java/org/apache/logging/log4j/lookup/CustomLookup.java
----------------------------------------------------------------------
diff --git a/log4j-samples/loggerProperties/src/main/java/org/apache/logging/log4j/lookup/CustomLookup.java b/log4j-samples/loggerProperties/src/main/java/org/apache/logging/log4j/lookup/CustomLookup.java
new file mode 100644
index 0000000..471d2ba
--- /dev/null
+++ b/log4j-samples/loggerProperties/src/main/java/org/apache/logging/log4j/lookup/CustomLookup.java
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.lookup;
+
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.Marker;
+import org.apache.logging.log4j.MarkerManager;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.config.plugins.Plugin;
+import org.apache.logging.log4j.core.lookup.AbstractLookup;
+import org.apache.logging.log4j.core.lookup.StrLookup;
+import org.apache.logging.log4j.status.StatusLogger;
+
+import java.lang.Override;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+/**
+ *
+ */
+
+@Plugin(name = "custom", category = StrLookup.CATEGORY)
+public class CustomLookup extends AbstractLookup {
+
+ private static final Logger LOGGER = StatusLogger.getLogger();
+ private static final Marker LOOKUP = MarkerManager.getMarker("LOOKUP");
+
+ private static ConcurrentMap<String, Map<String, String>> loggerProperties = new ConcurrentHashMap<>();
+
+ /**
+ * Looks up the value for the key using the data in the LogEvent.
+ * @param event The current LogEvent.
+ * @param key the key to be looked up, may be null
+ * @return The value associated with the key.
+ */
+ @Override
+ public String lookup(final LogEvent event, final String key) {
+ try {
+ Map<String, String> properties = loggerProperties.get(event.getLoggerName());
+ if (properties == null) {
+ return "";
+ }
+ if (key == null || key.length() == 0 || key.equals("*")) {
+ StringBuilder sb = new StringBuilder("{");
+ boolean first = true;
+ for (Map.Entry<String, String> entry : properties.entrySet()) {
+ if (!first) {
+ sb.append(", ");
+ }
+ sb.append(entry.getKey()).append("=").append(entry.getValue());
+ first = false;
+ }
+ sb.append("}");
+ return sb.toString();
+ } else {
+ return properties.get(key);
+ }
+ } catch (final Exception ex) {
+ LOGGER.warn(LOOKUP, "Error while getting property [{}].", key, ex);
+ return null;
+ }
+ }
+
+ public static void setLoggerProperties(String loggerName, Map<String, String> properties) {
+ loggerProperties.put(loggerName, properties);
+ }
+
+}
+
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/082615f6/log4j-samples/loggerProperties/src/main/java/org/apache/logging/log4j/lookup/CustomMapMessage.java
----------------------------------------------------------------------
diff --git a/log4j-samples/loggerProperties/src/main/java/org/apache/logging/log4j/lookup/CustomMapMessage.java b/log4j-samples/loggerProperties/src/main/java/org/apache/logging/log4j/lookup/CustomMapMessage.java
new file mode 100644
index 0000000..6988c72
--- /dev/null
+++ b/log4j-samples/loggerProperties/src/main/java/org/apache/logging/log4j/lookup/CustomMapMessage.java
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.lookup;
+
+import org.apache.logging.log4j.message.MapMessage;
+
+import java.util.Map;
+
+/**
+ *
+ */
+public class CustomMapMessage extends MapMessage {
+
+ private final String message;
+
+ public CustomMapMessage(String msg, Map<String, String> map) {
+ super(map);
+ this.message = msg;
+ }
+
+ @Override
+ public String getFormattedMessage(final String[] formats) {
+ if (message != null) {
+ return message;
+ }
+ return super.getFormattedMessage(formats);
+ }
+
+ @Override
+ public String getFormattedMessage() {
+ if (message != null) {
+ return message;
+ }
+ return super.getFormattedMessage();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/082615f6/log4j-samples/loggerProperties/src/main/java/org/apache/logging/log4j/lookup/MapMessageLookup.java
----------------------------------------------------------------------
diff --git a/log4j-samples/loggerProperties/src/main/java/org/apache/logging/log4j/lookup/MapMessageLookup.java b/log4j-samples/loggerProperties/src/main/java/org/apache/logging/log4j/lookup/MapMessageLookup.java
new file mode 100644
index 0000000..1ed1868
--- /dev/null
+++ b/log4j-samples/loggerProperties/src/main/java/org/apache/logging/log4j/lookup/MapMessageLookup.java
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.lookup;
+
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.Marker;
+import org.apache.logging.log4j.MarkerManager;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.config.plugins.Plugin;
+import org.apache.logging.log4j.core.lookup.AbstractLookup;
+import org.apache.logging.log4j.core.lookup.StrLookup;
+import org.apache.logging.log4j.message.MapMessage;
+import org.apache.logging.log4j.message.Message;
+import org.apache.logging.log4j.status.StatusLogger;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+/**
+ *
+ */
+
+@Plugin(name = "mapMessage", category = StrLookup.CATEGORY)
+public class MapMessageLookup extends AbstractLookup {
+
+ private static final Logger LOGGER = StatusLogger.getLogger();
+ private static final Marker LOOKUP = MarkerManager.getMarker("LOOKUP");
+
+ private static ConcurrentMap<String, Map<String, String>> loggerProperties = new ConcurrentHashMap<>();
+
+ /**
+ * Looks up the value for the key using the data in the LogEvent.
+ * @param event The current LogEvent.
+ * @param key the key to be looked up, may be null
+ * @return The value associated with the key.
+ */
+ @Override
+ public String lookup(final LogEvent event, final String key) {
+ Message msg = event.getMessage();
+ if (msg instanceof MapMessage) {
+ try {
+ Map<String, String> properties = ((MapMessage) msg).getData();
+ if (properties == null) {
+ return "";
+ }
+ if (key == null || key.length() == 0 || key.equals("*")) {
+ StringBuilder sb = new StringBuilder("{");
+ boolean first = true;
+ for (Map.Entry<String, String> entry : properties.entrySet()) {
+ if (!first) {
+ sb.append(", ");
+ }
+ sb.append(entry.getKey()).append("=").append(entry.getValue());
+ first = false;
+ }
+ sb.append("}");
+ return sb.toString();
+ } else {
+ return properties.get(key);
+ }
+ } catch (final Exception ex) {
+ LOGGER.warn(LOOKUP, "Error while getting property [{}].", key, ex);
+ return null;
+ }
+ } else {
+ return null;
+ }
+ }
+
+ public static void setLoggerProperties(String loggerName, Map<String, String> properties) {
+ loggerProperties.put(loggerName, properties);
+ }
+
+}
+
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/082615f6/log4j-samples/loggerProperties/src/test/java/org/apache/logging/log4j/CustomPropertiesTest.java
----------------------------------------------------------------------
diff --git a/log4j-samples/loggerProperties/src/test/java/org/apache/logging/log4j/CustomPropertiesTest.java b/log4j-samples/loggerProperties/src/test/java/org/apache/logging/log4j/CustomPropertiesTest.java
new file mode 100644
index 0000000..3612dde
--- /dev/null
+++ b/log4j-samples/loggerProperties/src/test/java/org/apache/logging/log4j/CustomPropertiesTest.java
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j;
+
+import org.apache.logging.log4j.lookup.CustomLookup;
+import org.apache.logging.log4j.lookup.CustomMapMessage;
+import org.junit.Test;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Created by rgoers on 8/2/15.
+ */
+public class CustomPropertiesTest {
+
+ @Test
+ public void testProperties() throws Exception {
+ Logger logger = LogManager.getLogger("TestProperties");
+ Map<String, String> loggerProperties = new ConcurrentHashMap<>();
+ CustomLookup.setLoggerProperties("TestProperties", loggerProperties);
+ loggerProperties.put("key1", "CustomPropertiesTest");
+ loggerProperties.put("key2", "TestValue");
+ logger.debug("This is a test");
+ }
+
+ @Test
+ public void mapMessageProperties() throws Exception {
+ Logger logger = LogManager.getLogger("MapProperties");
+ Map<String, String> loggerProperties = new ConcurrentHashMap<>();
+ loggerProperties.put("key1", "CustomPropertiesTest");
+ loggerProperties.put("key2", "TestValue");
+ logger.debug(new CustomMapMessage("This is a test", loggerProperties));
+ }
+
+
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/082615f6/log4j-samples/loggerProperties/src/test/resources/log4j2.xml
----------------------------------------------------------------------
diff --git a/log4j-samples/loggerProperties/src/test/resources/log4j2.xml b/log4j-samples/loggerProperties/src/test/resources/log4j2.xml
new file mode 100644
index 0000000..0aa688b
--- /dev/null
+++ b/log4j-samples/loggerProperties/src/test/resources/log4j2.xml
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+-->
+<Configuration name="XMLPerfTest" status="error">
+ <Appenders>
+ <File name="TestLogfile" fileName="target/testlog4j2.log" immediateFlush="false">
+ <PatternLayout>
+ <Pattern>%d %5p [%t] %c{1} - %m%n</Pattern>
+ </PatternLayout>
+ </File>
+ <RandomAccessFile name="RandomAccessLogFile" fileName="target/testCustomlog4j2.log" immediateFlush="false">
+ <PatternLayout>
+ <Pattern>%d %5p [%t] %c{1} %X{key1} %X{key2} - %m%n</Pattern>
+ </PatternLayout>
+ </RandomAccessFile>
+ </Appenders>
+ <Loggers>
+ <Logger name="TestProperties" level="debug" additivity="false">
+ <Property name="key1">$${custom:key1}</Property>
+ <Property name="key2">$${custom:key2}</Property>
+ <AppenderRef ref="RandomAccessLogFile"/>
+ </Logger>
+ <Logger name="MapProperties" level="debug" additivity="false">
+ <Property name="key1">$${mapMessage:key1}</Property>
+ <Property name="key2">$${mapMessage:key2}</Property>
+ <AppenderRef ref="RandomAccessLogFile"/>
+ </Logger>
+
+ <Root level="debug">
+ <AppenderRef ref="TestLogfile"/>
+ </Root>
+ </Loggers>
+</Configuration>
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/082615f6/log4j-samples/pom.xml
----------------------------------------------------------------------
diff --git a/log4j-samples/pom.xml b/log4j-samples/pom.xml
index a70fc08..0d9a29e 100644
--- a/log4j-samples/pom.xml
+++ b/log4j-samples/pom.xml
@@ -82,6 +82,7 @@
<module>flume-remote</module>
<module>flume-embedded</module>
<module>configuration</module>
+ <module>loggerProperties</module>
</modules>
<build>
<plugins>
[2/2] logging-log4j2 git commit: LOG4J2-1010 - update changes.xml
Posted by rg...@apache.org.
LOG4J2-1010 - update changes.xml
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/aa5151e1
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/aa5151e1
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/aa5151e1
Branch: refs/heads/master
Commit: aa5151e193b903389abb2591e652867e6e89234c
Parents: 082615f
Author: Ralph Goers <rg...@nextiva.com>
Authored: Sun Aug 2 18:37:15 2015 -0700
Committer: Ralph Goers <rg...@nextiva.com>
Committed: Sun Aug 2 18:37:15 2015 -0700
----------------------------------------------------------------------
src/changes/changes.xml | 3 +++
1 file changed, 3 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/aa5151e1/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 35bfbd1..d540f30 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -24,6 +24,9 @@
</properties>
<body>
<release version="2.4" date="2015-MM-DD" description="GA Release 2.4">
+ <action issue="LOG4J2-1010" dev="rgoers" type="update">
+ Pass log event when interpolating logger properties.
+ </action>
<action issue="LOG4J2-1076" dev="rpopma" type="add">
Added support for system nanosecond time in pattern layout.
</action>