Search in sources :

Example 1 with LogFilter

use of org.apache.sling.tail.LogFilter in project sling by apache.

the class LogTailerWebConsolePlugin method getQueryFromCookie.

private LogFilter[] getQueryFromCookie(HttpServletRequest request) {
    try {
        for (Cookie cookie : request.getCookies()) {
            if (cookie.getName().equals(FILTER_COOKIE)) {
                String[] parts = cookie.getValue().split("&&");
                LogFilter[] conditions = new LogFilter[parts.length];
                for (int i = 0; i < parts.length; i++) {
                    final String part = parts[i];
                    conditions[i] = new LogFilter() {

                        public boolean eval(String input) {
                            return input.contains(part);
                        }

                        public String toString() {
                            return part;
                        }
                    };
                }
                return conditions;
            }
        }
    } catch (Exception e) {
    }
    return null;
}
Also used : Cookie(javax.servlet.http.Cookie) LogFilter(org.apache.sling.tail.LogFilter) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 2 with LogFilter

use of org.apache.sling.tail.LogFilter in project sling by apache.

the class LogTailerWebConsolePlugin method renderContent.

@Override
protected void renderContent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if (isAjaxRequest(request)) {
        parseCommand(request, response);
        RandomAccessFile randomAccessFile = null;
        try {
            try {
                randomAccessFile = new RandomAccessFile(errLog, "r");
                log.debug("Tailing file " + fileName + " of length " + randomAccessFile.length());
            } catch (Exception e) {
                log.error("Error reading " + fileName, e);
                response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                return;
            }
            JSONWriter json = new JSONWriter(response.getWriter());
            json.object();
            boolean reverse = false;
            //long created = getCreatedTimestampFromCookie(request);
            /*long modified = getModifiedTimestampFromCookie(request);
            if(errLog.lastModified() == modified) {
                json.endObject();
                return;
            }
            else {
                 persistCookie(response, MODIFIED_COOKIE, String.valueOf(errLog.lastModified()));
            }*/
            long pos = getPositionFromCookie(request);
            if (pos < 0) {
                pos = randomAccessFile.length() - 1;
                reverse = true;
            } else if (pos > randomAccessFile.length()) {
                //file rotated
                pos = 0;
            }
            LogFilter[] query = getQueryFromCookie(request);
            if (reverse) {
                randomAccessFile.seek(pos);
                if (randomAccessFile.read() == '\n') {
                    pos--;
                    randomAccessFile.seek(pos);
                    if (randomAccessFile.read() == '\r') {
                        pos--;
                    }
                }
                json.key("content").array();
                int found = 0;
                StringBuilder sb = new StringBuilder();
                String line = null;
                List<String> lines = new ArrayList<String>();
                while (found != LINES_TO_TAIL && pos > 0) {
                    boolean eol = false;
                    randomAccessFile.seek(pos);
                    int c = randomAccessFile.read();
                    if (c == '\n') {
                        found++;
                        sb = sb.reverse();
                        line = sb.toString();
                        sb = new StringBuilder();
                        eol = true;
                        pos--;
                        if (pos > 0) {
                            randomAccessFile.seek(pos);
                            if (randomAccessFile.read() == '\r') {
                                pos--;
                            }
                        }
                    } else {
                        sb.append((char) c);
                        pos--;
                    }
                    if (eol) {
                        if (filter(line, query)) {
                            lines.add(line);
                        }
                    }
                }
                if (pos < 0) {
                    if (filter(line, query)) {
                        lines.add(line);
                    }
                }
                for (int i = lines.size() - 1; i > -1; i--) {
                    json.object().key("line").value(lines.get(i)).endObject();
                }
                json.endArray();
                json.endObject();
            } else {
                randomAccessFile.seek(pos);
                String line = null;
                int lineCount = 0;
                json.key("content").array();
                boolean read = true;
                while (read) {
                    StringBuilder input = new StringBuilder();
                    int c = -1;
                    boolean eol = false;
                    while (!eol) {
                        switch(c = randomAccessFile.read()) {
                            case -1:
                            case '\n':
                                eol = true;
                                break;
                            case '\r':
                                eol = true;
                                long cur = randomAccessFile.getFilePointer();
                                if ((randomAccessFile.read()) != '\n') {
                                    randomAccessFile.seek(cur);
                                }
                                break;
                            default:
                                input.append((char) c);
                                break;
                        }
                    }
                    if ((c == -1) && (input.length() == 0)) {
                        read = false;
                        continue;
                    }
                    line = input.toString();
                    lineCount++;
                    if (lineCount == LINES_TO_TAIL) {
                        read = false;
                    }
                    pos = randomAccessFile.getFilePointer();
                    if (filter(line, query)) {
                        json.object().key("line").value(line).endObject();
                    }
                }
                json.endArray();
                json.endObject();
            }
            persistCookie(response, POSITION_COOKIE, String.valueOf(randomAccessFile.getFilePointer()));
        } catch (Exception e) {
            log.error("Error tailing " + fileName, e);
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        } finally {
            try {
                if (randomAccessFile != null) {
                    randomAccessFile.close();
                }
            } catch (Exception e) {
                log.error("Error closing " + fileName, e);
            }
        }
    } else {
        PrintWriter printWriter = response.getWriter();
        printWriter.println("<script type=\"text/javascript\" src=\"/libs/sling/logtail-plugin/js/tail.js\"></script>");
        printWriter.println("<link href=\"/libs/sling/logtail-plugin/css/tail.css\" rel=\"stylesheet\" type=\"text/css\"></link>");
        printWriter.println("<div class=\"header-cont\">");
        printWriter.println("   <div class=\"header\" style=\"display:none;\">");
        printWriter.println("       <table>");
        printWriter.println("           <tr>");
        printWriter.println("               <td><button class=\"numbering\" title=\"Show Line Numbers\" data-numbers=\"false\">Show Line No.</button></td>");
        printWriter.println("               <td><button class=\"pause\" title=\"Pause\">Pause</button></td>");
        printWriter.println("               <td class=\"longer\"><label>Sync frequency(msec)</label>");
        printWriter.println("                   <button class=\"faster\" title=\"Sync Faster\">-</button>");
        printWriter.println("                   <input id=\"speed\" type=\"text\" value=\"3000\"/>");
        printWriter.println("                   <button class=\"slower\" title=\"Sync Slower\">+</button></td>");
        printWriter.println("               <td><button class=\"tail\" title=\"Unfollow Tail\" data-following=\"true\">Unfollow</button></td>");
        printWriter.println("               <td><button class=\"highlighting\" title=\"Highlight\">Highlight</button></td>");
        printWriter.println("               <td><button class=\"clear\" title=\"Clear Display\">Clear</button></td>");
        printWriter.println("               <td class=\"longer\"><input id=\"filter\" type=\"text\"/><span class=\"filterClear ui-icon ui-icon-close\" title=\"Clear Filter\">&nbsp;</span><button class=\"filter\" title=\"Filter Logs\">Filter</button></td>");
        printWriter.println("               <td><button class=\"refresh\" title=\"Reload Logs\">Reload</button></td>");
        printWriter.println("               <td><button class=\"sizeplus\" title=\"Bigger\">a->A</button></td>");
        printWriter.println("               <td><button class=\"sizeminus\" title=\"Smaller\">A->a</button></td>");
        printWriter.println("               <td><button class=\"top\" title=\"Scroll to Top\">Top</button></td>");
        printWriter.println("               <td><button class=\"bottom\" title=\"Scroll to Bottom\">Bottom</button></td>");
        printWriter.println("           </tr>");
        printWriter.println("           <tr>");
        printWriter.println("               <td class=\"loadingstatus\" colspan=\"2\" data-status=\"inactive\"><ul><li></li></ul></td>");
        printWriter.println("               <td>Tailing &nbsp; <select id=\"logfiles\">" + getOptions() + "</select></td>");
        printWriter.println("           </tr>");
        printWriter.println("       </table>");
        printWriter.println("   </div>");
        printWriter.println("   <div class=\"pulldown\" title=\"Click to show options\">&nbsp;==&nbsp;</div>");
        printWriter.println("</div>");
        printWriter.println("");
        printWriter.println("   <div class=\"content\">");
        printWriter.println("");
        printWriter.println("       <div id=\"logarea\"></div>");
        printWriter.println("");
        printWriter.println("   </div>");
    }
}
Also used : JSONWriter(org.apache.felix.utils.json.JSONWriter) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) LogFilter(org.apache.sling.tail.LogFilter) PrintWriter(java.io.PrintWriter)

Aggregations

IOException (java.io.IOException)2 ServletException (javax.servlet.ServletException)2 LogFilter (org.apache.sling.tail.LogFilter)2 PrintWriter (java.io.PrintWriter)1 RandomAccessFile (java.io.RandomAccessFile)1 Cookie (javax.servlet.http.Cookie)1 JSONWriter (org.apache.felix.utils.json.JSONWriter)1