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/10/10 06:35:06 UTC
logging-log4j2 git commit: LOG4J2-1136 - Add script support to
Properties configuration
Repository: logging-log4j2
Updated Branches:
refs/heads/LOG4J2-1136 f6f41c190 -> fd5de4efc
LOG4J2-1136 - Add script support to Properties configuration
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/fd5de4ef
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/fd5de4ef
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/fd5de4ef
Branch: refs/heads/LOG4J2-1136
Commit: fd5de4efcb62e18cef98263ff9cc1f306a4eb8c9
Parents: f6f41c1
Author: Ralph Goers <rg...@nextiva.com>
Authored: Fri Oct 9 21:34:44 2015 -0700
Committer: Ralph Goers <rg...@nextiva.com>
Committed: Fri Oct 9 21:34:44 2015 -0700
----------------------------------------------------------------------
.../PropertiesConfigurationFactory.java | 57 ++++++++++++++++++
.../filter/ScriptFileFilterPropertiesTest.java | 37 ++++++++++++
.../log4j-scriptFile-filters.properties | 63 ++++++++++++++++++++
3 files changed, 157 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/fd5de4ef/log4j-core/src/main/java/org/apache/logging/log4j/core/config/properties/PropertiesConfigurationFactory.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/properties/PropertiesConfigurationFactory.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/properties/PropertiesConfigurationFactory.java
index 50f9285..58094f0 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/properties/PropertiesConfigurationFactory.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/properties/PropertiesConfigurationFactory.java
@@ -30,6 +30,8 @@ import org.apache.logging.log4j.core.config.builder.api.FilterComponentBuilder;
import org.apache.logging.log4j.core.config.builder.api.LayoutComponentBuilder;
import org.apache.logging.log4j.core.config.builder.api.LoggerComponentBuilder;
import org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuilder;
+import org.apache.logging.log4j.core.config.builder.api.ScriptComponentBuilder;
+import org.apache.logging.log4j.core.config.builder.api.ScriptFileComponentBuilder;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.util.PropertiesUtil;
import org.apache.logging.log4j.util.Strings;
@@ -105,6 +107,25 @@ public class PropertiesConfigurationFactory extends ConfigurationFactory {
builder.addProperty(key, props.getProperty(key));
}
+ String scriptProp = properties.getProperty("scripts");
+ if (scriptProp != null) {
+ String[] scriptNames = scriptProp.split(",");
+ for (String scriptName : scriptNames) {
+ String name = scriptName.trim();
+ Properties scriptProps = PropertiesUtil.extractSubset(properties, "script." + name);
+ String type = scriptProps.getProperty("type");
+ if (type == null) {
+ throw new ConfigurationException("No type provided for script - must be Script or ScriptFile");
+ }
+ scriptProps.remove("type");
+ if (type.equalsIgnoreCase("script")) {
+ builder.add(createScript(builder, name, scriptProps));
+ } else {
+ builder.add(createScriptFile(builder, name, scriptProps));
+ }
+ }
+ }
+
Properties levelProps = PropertiesUtil.extractSubset(properties, "customLevel");
if (levelProps.size() > 0) {
for (String key : levelProps.stringPropertyNames()) {
@@ -149,6 +170,42 @@ public class PropertiesConfigurationFactory extends ConfigurationFactory {
return builder.build();
}
+
+ private ScriptComponentBuilder createScript(ConfigurationBuilder<PropertiesConfiguration> builder, String key,
+ Properties properties) {
+ String name = properties.getProperty("name");
+ if (name != null) {
+ properties.remove("name");
+ }
+ String language = properties.getProperty("language");
+ if (language!= null) {
+ properties.remove("language");
+ }
+ String text = properties.getProperty("text");
+ if (text != null) {
+ properties.remove("text");
+ }
+ ScriptComponentBuilder scriptBuilder = builder.newScript(name, language, text);
+ processRemainingProperties(scriptBuilder, key, properties);
+ return scriptBuilder;
+ }
+
+ private ScriptFileComponentBuilder createScriptFile(ConfigurationBuilder<PropertiesConfiguration> builder, String key,
+ Properties properties) {
+ String name = properties.getProperty("name");
+ if (name != null) {
+ properties.remove("name");
+ }
+ String path = properties.getProperty("path");
+ if (path != null) {
+ properties.remove("path");
+ }
+ ScriptFileComponentBuilder scriptFileBuilder = builder.newScriptFile(name, path);
+ processRemainingProperties(scriptFileBuilder, key, properties);
+ return scriptFileBuilder;
+ }
+
+
private AppenderComponentBuilder createAppender(ConfigurationBuilder<PropertiesConfiguration> builder, String key,
Properties properties) {
String name = properties.getProperty(CONFIG_NAME);
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/fd5de4ef/log4j-core/src/test/java/org/apache/logging/log4j/core/filter/ScriptFileFilterPropertiesTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/filter/ScriptFileFilterPropertiesTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/filter/ScriptFileFilterPropertiesTest.java
new file mode 100644
index 0000000..431cea8
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/filter/ScriptFileFilterPropertiesTest.java
@@ -0,0 +1,37 @@
+/*
+ * 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.core.filter;
+
+import org.apache.logging.log4j.junit.LoggerContextRule;
+import org.junit.ClassRule;
+
+/**
+ *
+ */
+public class ScriptFileFilterPropertiesTest extends AbstractScriptFilterTest {
+
+ private static final String CONFIG = "log4j-scriptFile-filters.properties";
+
+ @ClassRule
+ public static LoggerContextRule context = new LoggerContextRule(CONFIG);
+
+ @Override
+ public LoggerContextRule getContext() {
+ return context;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/fd5de4ef/log4j-core/src/test/resources/log4j-scriptFile-filters.properties
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/resources/log4j-scriptFile-filters.properties b/log4j-core/src/test/resources/log4j-scriptFile-filters.properties
new file mode 100644
index 0000000..ac1874b
--- /dev/null
+++ b/log4j-core/src/test/resources/log4j-scriptFile-filters.properties
@@ -0,0 +1,63 @@
+# 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.
+
+status = error
+name = ScriptConfigTest
+
+scripts = javascript, groovy
+script.javascript.type = ScriptFile
+script.javascript.name = filter.js
+script.javascript.language = JavaScript
+script.javascript.path = src/test/resources/scripts/filter.js
+script.javascript.charset = UTF-8
+script.groovy.type = ScriptFile
+script.groovy.name = filter.groovy
+script.groovy.path = src/test/resources/scripts/filter.groovy
+script.groovy.charset = UTF-8
+
+appenders = list
+appender.list.type = List
+appender.list.name = List
+appender.list.layout.type = PatternLayout
+appender.list.layout.pattern = [%-5level] %c{1.} %msg%n
+
+loggers = javascriptFilter, groovyFilter
+logger.javascriptFilter.name = TestJavaScriptFilter
+logger.javascriptFilter.level = trace
+logger.javascriptFilter.additivity = false
+logger.javascriptFilter.appenderRefs = list
+logger.javascriptFilter.appenderRef.list.ref = List
+logger.javascriptFilter.filters = script
+logger.javascriptFilter.filter.script.type = ScriptFilter
+logger.javascriptFilter.filter.script.onMatch = ACCEPT
+logger.javascriptFilter.filter.script.onMisMatch = DENY
+logger.javascriptFilter.filter.script.ref.type = ScriptRef
+logger.javascriptFilter.filter.script.ref.ref = filter.js
+
+logger.groovyFilter.name = TestGroovyFilter
+logger.groovyFilter.level = trace
+logger.groovyFilter.additivity = false
+logger.groovyFilter.appenderRefs = list
+logger.groovyFilter.appenderRef.list.ref = List
+logger.groovyFilter.filters = script
+logger.groovyFilter.filter.script.type = ScriptFilter
+logger.groovyFilter.filter.script.onMatch = ACCEPT
+logger.groovyFilter.filter.script.onMisMatch = DENY
+logger.groovyFilter.filter.script.ref.type = ScriptRef
+logger.groovyFilter.filter.script.ref.ref = filter.groovy
+
+rootLogger.level = trace
+rootLogger.appenderRefs = list
+rootLogger.appenderRef.list.ref = List