You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by mi...@apache.org on 2016/08/17 20:21:26 UTC
logging-log4j2 git commit: LOG4J2-1528 Serialize configuration into a
log4j2.xml file
Repository: logging-log4j2
Updated Branches:
refs/heads/LOG4J2-1528 [created] 446fb87af
LOG4J2-1528 Serialize configuration into a log4j2.xml file
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/446fb87a
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/446fb87a
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/446fb87a
Branch: refs/heads/LOG4J2-1528
Commit: 446fb87af4ccaab777280730fde921948eb4425f
Parents: 0728abc
Author: Mikael St�ldal <mi...@staldal.nu>
Authored: Wed Aug 17 22:20:44 2016 +0200
Committer: Mikael St�ldal <mi...@staldal.nu>
Committed: Wed Aug 17 22:20:44 2016 +0200
----------------------------------------------------------------------
.../builder/api/ConfigurationBuilder.java | 17 +++
.../impl/DefaultConfigurationBuilder.java | 113 +++++++++++++++++++
2 files changed, 130 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/446fb87a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/builder/api/ConfigurationBuilder.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/builder/api/ConfigurationBuilder.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/builder/api/ConfigurationBuilder.java
index 1fbfa01..2e7ef96 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/builder/api/ConfigurationBuilder.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/builder/api/ConfigurationBuilder.java
@@ -22,6 +22,9 @@ import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.util.Builder;
+import java.io.IOException;
+import java.io.OutputStream;
+
/**
* Interface for building logging configurations.
* @param <T> The Configuration type created by this builder.
@@ -405,4 +408,18 @@ public interface ConfigurationBuilder<T extends Configuration> extends Builder<T
* @return The constructed Configuration.
*/
T build(boolean initialize);
+
+ /**
+ * Write an XML configuration file from this builder.
+ *
+ * @param output OutputStream to write to, will not be closed
+ */
+ void writeXmlConfigurationFile(OutputStream output) throws IOException;
+
+ /**
+ * Write an XML configuration from this builder.
+ *
+ * @return XML configuration
+ */
+ String writeXmlConfiguration();
}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/446fb87a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/builder/impl/DefaultConfigurationBuilder.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/builder/impl/DefaultConfigurationBuilder.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/builder/impl/DefaultConfigurationBuilder.java
index 38b4fdb..6666486 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/builder/impl/DefaultConfigurationBuilder.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/builder/impl/DefaultConfigurationBuilder.java
@@ -16,8 +16,12 @@
*/
package org.apache.logging.log4j.core.config.builder.impl;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.StringWriter;
import java.lang.reflect.Constructor;
import java.util.List;
+import java.util.Map;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.Filter;
@@ -38,6 +42,10 @@ import org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuild
import org.apache.logging.log4j.core.config.builder.api.ScriptComponentBuilder;
import org.apache.logging.log4j.core.config.builder.api.ScriptFileComponentBuilder;
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
/**
* @param <T> The BuiltConfiguration type.
* @since 2.4
@@ -188,6 +196,111 @@ public class DefaultConfigurationBuilder<T extends BuiltConfiguration> implement
}
@Override
+ public void writeXmlConfigurationFile(OutputStream output) throws IOException {
+ try {
+ XMLStreamWriter xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(output);
+ writeConfigurationXml(xmlWriter);
+ xmlWriter.close();
+ } catch (XMLStreamException e) {
+ if (e.getNestedException() instanceof IOException) {
+ throw (IOException)e.getNestedException();
+ } else {
+ throw new RuntimeException(e);
+ }
+ }
+ }
+
+ @Override
+ public String writeXmlConfiguration() {
+ StringWriter sw = new StringWriter();
+ try {
+ XMLStreamWriter xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(sw);
+ writeConfigurationXml(xmlWriter);
+ xmlWriter.close();
+ } catch (XMLStreamException e) {
+ throw new RuntimeException(e);
+ }
+ return sw.toString();
+ }
+
+ private void writeConfigurationXml(XMLStreamWriter xmlWriter) throws XMLStreamException {
+ xmlWriter.writeStartDocument();
+ xmlWriter.writeCharacters(System.lineSeparator());
+
+ xmlWriter.writeStartElement("Configuration");
+ if (name != null) {
+ xmlWriter.writeAttribute("name", name);
+ }
+ if (level != null) {
+ xmlWriter.writeAttribute("status", level.name());
+ }
+ if (verbosity != null) {
+ xmlWriter.writeAttribute("verbose", verbosity);
+ }
+ if (destination != null) {
+ xmlWriter.writeAttribute("dest", destination);
+ }
+ if (packages != null) {
+ xmlWriter.writeAttribute("packages", packages);
+ }
+ if (shutdownFlag != null) {
+ xmlWriter.writeAttribute("shutdownHook", shutdownFlag);
+ }
+ if (advertiser != null) {
+ xmlWriter.writeAttribute("advertiser", advertiser);
+ }
+ if (monitorInterval > 0) {
+ xmlWriter.writeAttribute("monitorInterval", String.valueOf(monitorInterval));
+ }
+
+ xmlWriter.writeCharacters(System.lineSeparator());
+
+ for (Component component : root.getComponents()) {
+ if (!component.getAttributes().isEmpty() || !component.getComponents().isEmpty() || component.getValue() != null) {
+ writeComponentXml(xmlWriter, component, 1);
+ }
+ }
+
+ xmlWriter.writeEndElement(); // "Configuration"
+
+ xmlWriter.writeEndDocument();
+ }
+
+ private void writeComponentXml(XMLStreamWriter xmlWriter, Component component, int nesting) throws XMLStreamException {
+ if (!component.getComponents().isEmpty() || component.getValue() != null) {
+ indentXml(xmlWriter, nesting);
+ xmlWriter.writeStartElement(component.getPluginType());
+ writeAttributesXml(xmlWriter, component);
+ xmlWriter.writeCharacters(System.lineSeparator());
+ for (Component subComponent : component.getComponents()) {
+ writeComponentXml(xmlWriter, subComponent, nesting + 1);
+ }
+ if (component.getValue() != null) {
+ xmlWriter.writeCharacters(component.getValue());
+ }
+ indentXml(xmlWriter, nesting);
+ xmlWriter.writeEndElement();
+ } else {
+ indentXml(xmlWriter, nesting);
+ xmlWriter.writeEmptyElement(component.getPluginType());
+ writeAttributesXml(xmlWriter, component);
+ }
+ xmlWriter.writeCharacters(System.lineSeparator());
+ }
+
+ private void indentXml(XMLStreamWriter xmlWriter, int nesting) throws XMLStreamException {
+ for (int i = 0; i < nesting; i++) {
+ xmlWriter.writeCharacters("\t");
+ }
+ }
+
+ private void writeAttributesXml(XMLStreamWriter xmlWriter, Component component) throws XMLStreamException {
+ for (Map.Entry<String, String> attribute : component.getAttributes().entrySet()) {
+ xmlWriter.writeAttribute(attribute.getKey(), attribute.getValue());
+ }
+ }
+
+ @Override
public ScriptComponentBuilder newScript(final String name, final String language, final String text) {
return new DefaultScriptComponentBuilder(this, name, language, text);
}