You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by th...@apache.org on 2013/02/20 19:11:17 UTC

svn commit: r1448335 - in /cocoon/cocoon3/trunk/cocoon-optional/src/main: java/org/apache/cocoon/optional/pipeline/components/sax/generator/ resources/META-INF/cocoon/spring-optional/

Author: thorsten
Date: Wed Feb 20 18:11:16 2013
New Revision: 1448335

URL: http://svn.apache.org/r1448335
Log:
COCOON3-121
If you use something like ch.qos.logback.classic.log4j.XMLLayout you can create xml based log files. However the problem is that it does not add root element making the resulting file not well-formed.

You can activate the logging in your logback.xml like

   <appender name="FAILS" class="ch.qos.logback.core.FileAppender">
        <file>${crawler.log.error}</file>
        <append>false</append>
        <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
          <layout class="ch.qos.logback.classic.log4j.XMLLayout">
            <locationInfo>true</locationInfo>
          </layout>
        </encoder>
    </appender>

The implemented solution has the following configuration in spring:

  <bean name="generator:log4j" class="org.apache.cocoon.optional.pipeline.components.sax.generator.AddRootElementGenerator" scope="prototype">
    <property name="encoding" value="UTF-8"/>
    <property name="localName" value="events"/>
    <property name="prefix" value="log4j"/>
    <property name="namespace" value="http://jakarta.apache.org/log4j/"/>
  </bean>

and later parse the file that the appender gives like:
<map:pipeline>
      <map:match pattern="errorLogs">
        <map:generate src="${crawler.log.error}" type="log4j"/>
        <map:serialize type="xml" />
      </map:match>
    </map:pipeline>

which will result in something like:
<?xml version="1.0" encoding="UTF-8"?>
<log4j:events xmlns:log4j="http://jakarta.apache.org/log4j/">
  <log4j:event logger="org.apache.droids.exception.ExceptionHandler" timestamp="1361325224196" level="ERROR" thread="main">
   <log4j:message><![CDATA[org.apache.droids.core.DroidsException: org.apache.droids.core.DroidsException: org.apache.droids.core.DroidsException: org.apache.http.client.HttpResponseException: Internal Server Error http://localhost:8888/xxx/details/xxx]]>
  </log4j:message>
  <log4j:locationInfo class="org.apache.droids.exception.ExceptionHandler" method="handleException" file="ExceptionHandler.java" line="23"/>
 </log4j:event>
</log4j:events> 

Added:
    cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/generator/
    cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/generator/AddRootElementGenerator.java
    cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/generator/package-info.java
Modified:
    cocoon/cocoon3/trunk/cocoon-optional/src/main/resources/META-INF/cocoon/spring-optional/cocoon-optional-generators.xml

Added: cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/generator/AddRootElementGenerator.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/generator/AddRootElementGenerator.java?rev=1448335&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/generator/AddRootElementGenerator.java (added)
+++ cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/generator/AddRootElementGenerator.java Wed Feb 20 18:11:16 2013
@@ -0,0 +1,153 @@
+/*
+ * 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.cocoon.optional.pipeline.components.sax.generator;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Map;
+
+import org.apache.cocoon.pipeline.ProcessingException;
+import org.apache.cocoon.pipeline.caching.CacheKey;
+import org.apache.cocoon.pipeline.caching.TimestampCacheKey;
+import org.apache.cocoon.pipeline.component.CachingPipelineComponent;
+import org.apache.cocoon.pipeline.util.URLConnectionUtils;
+import org.apache.cocoon.sax.AbstractSAXGenerator;
+import org.apache.cocoon.sax.component.XMLGenerator;
+import org.apache.cocoon.sax.util.XMLUtils;
+import org.apache.commons.io.IOUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * LogsGenerator.
+ */
+public class AddRootElementGenerator extends AbstractSAXGenerator implements
+        CachingPipelineComponent {
+
+    /**
+     * Logger.
+     */
+    private static final Logger LOG = LoggerFactory
+            .getLogger(XMLGenerator.class);
+    private URL source;
+
+    private String encoding;
+    private String localName;
+    private String prefix;
+    private String namespace;
+    /**
+     * {@inheritDoc}.
+     * @see org.apache.cocoon.sax.AbstractSAXProducer#setConfiguration(java.util.Map)
+     */
+    @Override
+    public void setConfiguration(
+            final Map<String, ? extends Object> configuration) {
+        this.source = (URL) configuration.get("source");
+    }
+
+    /**
+     * {@inheritDoc}
+     * @see org.apache.cocoon.pipeline.component.Starter#execute()
+     */
+    @Override
+    public void execute() {
+
+        if (this.source == null) {
+            throw new ProcessingException(this.getClass().getSimpleName()
+                + " has no source.");
+        }
+
+        LOG.debug("Using the URL {} to produce SAX events.", this.source
+                .toExternalForm());
+
+        try {
+            byte[] contenidoXML =
+                getLogs(this.source.openConnection().getInputStream());
+            InputStream aux = new ByteArrayInputStream(contenidoXML);
+            XMLUtils.toSax(aux, this.getSAXConsumer());
+        } catch (Exception e) {
+            throw new ProcessingException("Can't open connection to "
+                + this.source.toExternalForm(), e);
+        }
+    }
+
+    /**
+     * Parsea el fichero de log para incluir el elemento raiz y convertir el log
+     * en un XML well-formed.
+     * @param content content
+     * @return byte[]
+     */
+    private byte[] getLogs(InputStream content) {
+
+        StringBuffer texto = new StringBuffer();
+
+        try {
+            // Inicializar
+            texto.append("<?xml version=\"1.0\" encoding=\"");
+            texto.append(encoding);
+            texto.append("\"?><");
+            texto.append(prefix);
+            texto.append(":");
+            texto.append(localName);
+            texto.append(" ");
+            texto.append("xmlns");
+            texto.append(":");
+            texto.append(prefix);
+            texto.append("=\"");
+            texto.append(namespace);
+            texto.append("\">");
+            // Parse original stream
+            texto.append(IOUtils.toString(content, encoding));
+            // Finalizar
+            texto.append("</");
+            texto.append(prefix);
+            texto.append(":");
+            texto.append(localName);
+            texto.append(">");
+        } catch (Exception e) {
+            LOG.error("Error in getLogs method: " + e.getMessage());
+        }
+        return texto.toString().getBytes();
+    }
+
+    /**
+     * {@inheritDoc}.
+     * @see org.apache.cocoon.pipeline.component.CachingPipelineComponent
+     *      #constructCacheKey()
+     */
+    @Override
+    public CacheKey constructCacheKey() {
+        return new TimestampCacheKey(this.source, URLConnectionUtils
+                .getLastModified(this.source));
+    }
+    public void setEncoding(String encoding) {
+        this.encoding = encoding;
+    }
+
+    public void setLocalName(String localName) {
+        this.localName = localName;
+    }
+
+    public void setPrefix(String prefix) {
+        this.prefix = prefix;
+    }
+
+    public void setNamespace(String namespace) {
+        this.namespace = namespace;
+    }
+}

Added: cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/generator/package-info.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/generator/package-info.java?rev=1448335&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/generator/package-info.java (added)
+++ cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/generator/package-info.java Wed Feb 20 18:11:16 2013
@@ -0,0 +1,17 @@
+/*
+ * 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.cocoon.optional.pipeline.components.sax.generator;
\ No newline at end of file

Modified: cocoon/cocoon3/trunk/cocoon-optional/src/main/resources/META-INF/cocoon/spring-optional/cocoon-optional-generators.xml
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-optional/src/main/resources/META-INF/cocoon/spring-optional/cocoon-optional-generators.xml?rev=1448335&r1=1448334&r2=1448335&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-optional/src/main/resources/META-INF/cocoon/spring-optional/cocoon-optional-generators.xml (original)
+++ cocoon/cocoon3/trunk/cocoon-optional/src/main/resources/META-INF/cocoon/spring-optional/cocoon-optional-generators.xml Wed Feb 20 18:11:16 2013
@@ -25,5 +25,11 @@
   <bean name="generator:html" class="org.apache.cocoon.optional.pipeline.components.sax.neko.NekoGenerator" scope="prototype" />
   <bean name="generator:csv" class="org.apache.cocoon.optional.pipeline.components.sax.csv.CSVGenerator" scope="prototype" />
   <bean name="generator:calendar" class="org.apache.cocoon.optional.pipeline.components.sax.calendar.CalendarGenerator" scope="prototype" />
+  <bean name="generator:log4j" class="org.apache.cocoon.optional.pipeline.components.sax.generator.AddRootElementGenerator" scope="prototype">
+    <property name="encoding" value="UTF-8"/>
+    <property name="localName" value="events"/>
+    <property name="prefix" value="log4j"/>
+    <property name="namespace" value="http://jakarta.apache.org/log4j/"/>
+  </bean>
 
 </beans>