You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ch...@apache.org on 2015/07/25 09:10:03 UTC

svn commit: r1692605 - in /sling/trunk/bundles/commons/log: ./ src/main/java/org/apache/sling/commons/log/logback/internal/ src/main/resources/OSGI-INF/metatype/ src/test/java/org/apache/sling/commons/log/logback/internal/

Author: chetanm
Date: Sat Jul 25 07:10:02 2015
New Revision: 1692605

URL: http://svn.apache.org/r1692605
Log:
SLING-4904 - Log Config printer should only dump last 'n' lines of the logs

Added:
    sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/Tailer.java   (with props)
    sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/logback/internal/TestTailer.java   (with props)
Modified:
    sling/trunk/bundles/commons/log/pom.xml
    sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/LogConfigManager.java
    sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/SlingConfigurationPrinter.java
    sling/trunk/bundles/commons/log/src/main/resources/OSGI-INF/metatype/metatype.properties
    sling/trunk/bundles/commons/log/src/main/resources/OSGI-INF/metatype/metatype.xml

Modified: sling/trunk/bundles/commons/log/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/log/pom.xml?rev=1692605&r1=1692604&r2=1692605&view=diff
==============================================================================
--- sling/trunk/bundles/commons/log/pom.xml (original)
+++ sling/trunk/bundles/commons/log/pom.xml Sat Jul 25 07:10:02 2015
@@ -446,6 +446,12 @@
       <version>1.6.2</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.hamcrest</groupId>
+      <artifactId>hamcrest-integration</artifactId>
+      <version>1.3</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <profiles>

Modified: sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/LogConfigManager.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/LogConfigManager.java?rev=1692605&r1=1692604&r2=1692605&view=diff
==============================================================================
--- sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/LogConfigManager.java (original)
+++ sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/LogConfigManager.java Sat Jul 25 07:10:02 2015
@@ -78,6 +78,10 @@ public class LogConfigManager implements
 
     public static final int PRINTER_MAX_INCLUDED_FILES_DEFAULT = 3;
 
+    public static final String PRINTER_NUM_OF_LINES = "org.apache.sling.commons.log.numOfLines";
+
+    public static final int PRINTER_NUM_OF_LINES_DEFAULT = 1000;
+
     public static final String LOG_LEVEL_DEFAULT = "INFO";
 
     public static final String LOG_LEVEL_RESET_TO_DEFAULT = "DEFAULT";
@@ -136,6 +140,8 @@ public class LogConfigManager implements
 
     private int maxOldFileCount;
 
+    private int numOfLines;
+
     /**
      * Logs a message an optional stack trace to error output. This method is
      * used by the logging system in case of errors writing to the correct
@@ -601,6 +607,13 @@ public class LogConfigManager implements
         return maxOldFileCount;
     }
 
+    /**
+     * Maximum number of lines from a log files to be included in txt mode dump
+     */
+    public int getNumOfLines() {
+        return numOfLines;
+    }
+
     // ---------- ManagedService interface -------------------------------------
 
     private Dictionary<String, String> getBundleConfiguration(BundleContext bundleContext) {
@@ -656,6 +669,9 @@ public class LogConfigManager implements
                 ClassicConstants.DEFAULT_MAX_CALLEDER_DATA_DEPTH);
         maxOldFileCount = Util.toInteger(configuration.get(PRINTER_MAX_INCLUDED_FILES),
                 PRINTER_MAX_INCLUDED_FILES_DEFAULT);
+        numOfLines = Util.toInteger(configuration.get(PRINTER_NUM_OF_LINES),
+                PRINTER_NUM_OF_LINES_DEFAULT);
+
     }
 
     // ---------- Internal helpers ---------------------------------------------

Modified: sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/SlingConfigurationPrinter.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/SlingConfigurationPrinter.java?rev=1692605&r1=1692604&r2=1692605&view=diff
==============================================================================
--- sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/SlingConfigurationPrinter.java (original)
+++ sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/SlingConfigurationPrinter.java Sat Jul 25 07:10:02 2015
@@ -61,6 +61,9 @@ public class SlingConfigurationPrinter {
     public void printConfiguration(PrintWriter printWriter, String mode) {
         LogbackManager.LoggerStateContext ctx = logbackManager.determineLoggerState();
 
+        int numOfLines = getNumOfLines();
+        Tailer tailer = new Tailer(new PrinterListener(printWriter), numOfLines);
+
         dumpLogFileSummary(printWriter, ctx.getAllAppenders());
 
         if (!MODE_ZIP.equals(mode)) {
@@ -71,22 +74,14 @@ public class SlingConfigurationPrinter {
                         printWriter.print("Log file ");
                         printWriter.println(file.getAbsolutePath());
                         printWriter.println("--------------------------------------------------");
-                        FileReader fr = null;
-                        try {
-                            fr = new FileReader(file);
-                            final char[] buffer = new char[512];
-                            int len;
-                            while ((len = fr.read(buffer)) != -1) {
-                                printWriter.write(buffer, 0, len);
-                            }
-                        } catch (IOException ignore) {
-                            // we just ignore this
-                        } finally {
-                            if (fr != null) {
-                                try {
-                                    fr.close();
-                                } catch (IOException ignored) {
-                                }
+                        if (numOfLines < 0) {
+                            includeWholeFile(printWriter, file);
+                        } else {
+                            try {
+                                tailer.tail(file);
+                            } catch (IOException e) {
+                                logbackManager.getLogConfigManager().internalFailure("Error occurred " +
+                                        "while processing log file " + file, e);
                             }
                         }
                         printWriter.println();
@@ -98,6 +93,27 @@ public class SlingConfigurationPrinter {
         dumpLogbackStatus(logbackManager, printWriter);
     }
 
+    private static void includeWholeFile(PrintWriter printWriter, File file) {
+        FileReader fr = null;
+        try {
+            fr = new FileReader(file);
+            final char[] buffer = new char[512];
+            int len;
+            while ((len = fr.read(buffer)) != -1) {
+                printWriter.write(buffer, 0, len);
+            }
+        } catch (IOException ignore) {
+            // we just ignore this
+        } finally {
+            if (fr != null) {
+                try {
+                    fr.close();
+                } catch (IOException ignored) {
+                }
+            }
+        }
+    }
+
     private void dumpLogFileSummary(PrintWriter pw, Collection<Appender<ILoggingEvent>> appenders) {
         pw.println("Summary");
         pw.println("=======");
@@ -204,6 +220,10 @@ public class SlingConfigurationPrinter {
         return new File[]{file};
     }
 
+    private int getNumOfLines(){
+        return logbackManager.getLogConfigManager().getNumOfLines();
+    }
+
     private int getMaxOldFileCount(){
         return logbackManager.getLogConfigManager().getMaxOldFileCount();
     }
@@ -278,4 +298,17 @@ public class SlingConfigurationPrinter {
         return SDF.format(modified);
     }
 
+    private static class PrinterListener implements Tailer.TailerListener {
+        private final PrintWriter pw;
+
+        public PrinterListener(PrintWriter pw) {
+            this.pw = pw;
+        }
+
+        @Override
+        public void handle(String line) {
+            pw.println(line);
+        }
+    }
+
 }

Added: sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/Tailer.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/Tailer.java?rev=1692605&view=auto
==============================================================================
--- sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/Tailer.java (added)
+++ sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/Tailer.java Sat Jul 25 07:10:02 2015
@@ -0,0 +1,151 @@
+/*
+ * 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.sling.commons.log.logback.internal;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+
+class Tailer{
+    static final int BUFFER_SIZE = 1024;
+    private final int numOfLines;
+    private final TailerListener listener;
+    private final byte[] buffer = new byte[BUFFER_SIZE];
+
+    public Tailer(TailerListener listener, int numOfLines) {
+        this.listener = listener;
+        this.numOfLines = numOfLines;
+    }
+
+    interface TailerListener {
+        /**
+         * Handles a line from a Tailer.
+         *
+         * @param line the line.
+         */
+        void handle(String line);
+    }
+
+    public void tail(File file) throws IOException {
+        RandomAccessFile raf = null;
+        try{
+            raf = new RandomAccessFile(file, "r");
+            long startPos = getTailStartPos(raf, numOfLines);
+            readLines(raf, startPos);
+        } finally {
+            try{
+                if (raf != null) {
+                    raf.close();
+                }
+            } catch (IOException ignore){
+
+            }
+        }
+    }
+
+    /**
+     * Returns the starting position of UNIX "tail -n".
+     */
+    private long getTailStartPos(RandomAccessFile file, int n) throws IOException {
+        int newlineCount = 0;
+        long length = file.length();
+        long pos = length - BUFFER_SIZE;
+        int buffLength = BUFFER_SIZE;
+
+        if (pos < 0) {
+            pos = 0;
+            buffLength = (int)length;
+        }
+
+        while (true) {
+            file.seek(pos);
+            file.readFully(buffer, 0, buffLength);
+
+            for (int i = buffLength - 1; i >= 0; i--) {
+                if ((char) buffer[i] == '\n') {
+                    newlineCount++;
+
+                    if (newlineCount >= n) {
+                        pos += (i + 1);
+                        return pos;
+                    }
+                }
+            }
+
+            if (pos == 0) {
+                break;
+            }
+
+            if (pos - BUFFER_SIZE < 0) {
+                buffLength = (int)pos;
+                pos = 0;
+            } else {
+                pos -= BUFFER_SIZE;
+            }
+        }
+
+        return pos;
+    }
+
+    /**
+     * Read new lines. Code below is taken from org.apache.commons.io.input.Tailer
+     *
+     * @throws java.io.IOException if an I/O error occurs.
+     * @param startPos position in file from where to start reading
+     */
+    private void readLines(RandomAccessFile file, long startPos) throws IOException {
+        StringBuilder sb = new StringBuilder();
+
+        file.seek(startPos);
+        int num;
+        boolean seenCR = false;
+        while (((num = file.read(buffer)) != -1)) {
+            for (int i = 0; i < num; i++) {
+                byte ch = buffer[i];
+                switch (ch) {
+                    case '\n':
+                        seenCR = false; // swallow CR before LF
+                        listener.handle(sb.toString());
+                        sb.setLength(0);
+                        break;
+                    case '\r':
+                        if (seenCR) {
+                            sb.append('\r');
+                        }
+                        seenCR = true;
+                        break;
+                    default:
+                        if (seenCR) {
+                            seenCR = false; // swallow final CR
+                            listener.handle(sb.toString());
+                            sb.setLength(0);
+                        }
+                        sb.append((char) ch); // add character, not its ascii value
+                }
+            }
+        }
+
+        //Drain the left over part
+        if (sb.length() != 0) {
+            listener.handle(sb.toString());
+        }
+    }
+
+}

Propchange: sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/Tailer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: sling/trunk/bundles/commons/log/src/main/resources/OSGI-INF/metatype/metatype.properties
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/log/src/main/resources/OSGI-INF/metatype/metatype.properties?rev=1692605&r1=1692604&r2=1692605&view=diff
==============================================================================
--- sling/trunk/bundles/commons/log/src/main/resources/OSGI-INF/metatype/metatype.properties (original)
+++ sling/trunk/bundles/commons/log/src/main/resources/OSGI-INF/metatype/metatype.properties Sat Jul 25 07:10:02 2015
@@ -87,4 +87,8 @@ log.file.buffered.description = By defau
 
 log.config.maxOldFileCountInDump.name = Max Count of files in dump
 log.config.maxOldFileCountInDump.description = Maximum number of old rolled over files for each \
-  active file to be included while generating the dump as part of Status zip support
\ No newline at end of file
+  active file to be included while generating the dump as part of Status zip support
+
+log.config.numOfLines.name = Number of lines
+log.config.numOfLines.description = Number of lines from each log files to include while generating the \
+  dump in 'txt' mode. If set to -1 then whole file would be included
\ No newline at end of file

Modified: sling/trunk/bundles/commons/log/src/main/resources/OSGI-INF/metatype/metatype.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/log/src/main/resources/OSGI-INF/metatype/metatype.xml?rev=1692605&r1=1692604&r2=1692605&view=diff
==============================================================================
--- sling/trunk/bundles/commons/log/src/main/resources/OSGI-INF/metatype/metatype.xml (original)
+++ sling/trunk/bundles/commons/log/src/main/resources/OSGI-INF/metatype/metatype.xml Sat Jul 25 07:10:02 2015
@@ -66,6 +66,11 @@
              default="3"
              name="%log.config.maxOldFileCountInDump.name"
              description="%log.config.maxOldFileCountInDump.description" />
+        <metatype:AD id="org.apache.sling.commons.log.numOfLines"
+             type="Integer"
+             default="1000"
+             name="%log.config.numOfLines.name"
+             description="%log.config.numOfLines.description" />
     </metatype:OCD>
     <metatype:Designate pid="org.apache.sling.commons.log.LogManager">
         <metatype:Object

Added: sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/logback/internal/TestTailer.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/logback/internal/TestTailer.java?rev=1692605&view=auto
==============================================================================
--- sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/logback/internal/TestTailer.java (added)
+++ sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/logback/internal/TestTailer.java Sat Jul 25 07:10:02 2015
@@ -0,0 +1,124 @@
+/*
+ * 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.sling.commons.log.logback.internal;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+
+import org.apache.commons.io.FileUtils;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import static java.util.Arrays.asList;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.contains;
+import static org.hamcrest.Matchers.empty;
+import static org.junit.Assert.assertEquals;
+
+public class TestTailer {
+    private final Random rnd = new Random();
+    @Rule
+    public final TemporaryFolder tempFolder = new TemporaryFolder();
+
+    private final LineCollector listener = new LineCollector();
+
+    @Test
+    public void testEmpty() throws Exception{
+        File f1 = tempFolder.newFile();
+        Tailer t = new Tailer(listener, 10);
+        t.tail(f1);
+        assertThat(listener.lines, empty());
+    }
+
+    @Test
+    public void testLessAndMore() throws Exception{
+        File f1 = tempFolder.newFile();
+        writeToFile(f1, asList("a", "b", "c", "d"));
+        new Tailer(listener, 2).tail(f1);
+        assertThat(listener.lines, contains("c", "d"));
+
+        listener.reset();
+        new Tailer(listener, 10).tail(f1);
+        assertThat(listener.lines, contains("a", "b", "c", "d"));
+    }
+
+    @Test
+    public void randomTest() throws Exception{
+        File f1 = tempFolder.newFile();
+        List<String> lines = createRandomLines(Tailer.BUFFER_SIZE * 10);
+        int numOfLines = lines.size();
+        writeToFile(f1, lines);
+        int n = rnd.nextInt(numOfLines/2);
+        new Tailer(listener, n).tail(f1);
+        assertEquals(listener.lines, lines.subList(numOfLines - n, numOfLines));
+
+    }
+
+    private List<String> createRandomLines(int totalSize){
+
+        List<String> result = new ArrayList<String>();
+        int size = 0;
+        while(true){
+            int l = rnd.nextInt(100);
+            StringBuilder sb = new StringBuilder();
+            for (int i = 0; i < l; i++) {
+                sb.append('x');
+            }
+            size += sb.length();
+            result.add(sb.toString());
+
+            if (size > totalSize){
+                break;
+            }
+        }
+        return result;
+    }
+
+    private void writeToFile(File f, List<String> lines) throws IOException {
+        StringBuilder sb = new StringBuilder();
+        boolean firstLine = true;
+        for (String line : lines){
+            if (firstLine){
+                firstLine = false;
+            } else {
+                sb.append("\n");
+            }
+            sb.append(line);
+
+        }
+        FileUtils.write(f, sb);
+    }
+
+    private static class LineCollector implements Tailer.TailerListener {
+        final List<String> lines = new ArrayList<String>();
+        @Override
+        public void handle(String line) {
+            lines.add(line);
+        }
+
+        public void reset(){
+            lines.clear();
+        }
+    }
+}

Propchange: sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/logback/internal/TestTailer.java
------------------------------------------------------------------------------
    svn:eol-style = native