All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ch.qos.logback.core.status.ViewStatusMessagesServletBase Maven / Gradle / Ivy

There is a newer version: 2.12.15
Show newest version
/**
 * Logback: the reliable, generic, fast and flexible logging framework.
 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
 *
 * This program and the accompanying materials are dual-licensed under
 * either the terms of the Eclipse Public License v1.0 as published by
 * the Eclipse Foundation
 *
 *   or (per the licensee's choosing)
 *
 * under the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation.
 */
package ch.qos.logback.core.status;

import static ch.qos.logback.core.CoreConstants.LINE_SEPARATOR;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.helpers.Transform;
import ch.qos.logback.core.util.CachingDateFormatter;

abstract public class ViewStatusMessagesServletBase extends HttpServlet {

    private static final long serialVersionUID = -3551928133801157219L;
    private static CachingDateFormatter SDF = new CachingDateFormatter("yyyy-MM-dd HH:mm:ss");

    static String SUBMIT = "submit";
    static String CLEAR = "Clear";

    protected abstract StatusManager getStatusManager(HttpServletRequest req, HttpServletResponse resp);

    protected abstract String getPageTitle(HttpServletRequest req, HttpServletResponse resp);

    int count;

    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        count = 0;
        StatusManager sm = getStatusManager(req, resp);

        resp.setContentType("text/html");
        PrintWriter output = resp.getWriter();

        output.append("\r\n");
        output.append("\r\n");
        printCSS(req.getContextPath(), output);
        output.append("\r\n");
        output.append("\r\n");
        output.append(getPageTitle(req, resp));

        output.append("
\r\n"); output.append(""); output.append("
\r\n"); if (CLEAR.equalsIgnoreCase(req.getParameter(SUBMIT))) { sm.clear(); sm.add(new InfoStatus("Cleared all status messages", this)); } output.append(""); StringBuilder buf = new StringBuilder(); if (sm != null) { printList(buf, sm); } else { output.append("Could not find status manager"); } output.append(buf); output.append("
"); output.append("\r\n"); output.append("\r\n"); output.flush(); output.close(); } public void printCSS(String localRef, PrintWriter output) { output.append(" \r\n"); } public void printList(StringBuilder buf, StatusManager sm) { buf.append("\r\n"); printHeader(buf); List statusList = sm.getCopyOfStatusList(); for (Status s : statusList) { count++; printStatus(buf, s); } buf.append("
\r\n"); } public void printHeader(StringBuilder buf) { buf.append(" \r\n"); buf.append(" Date \r\n"); buf.append(" Level\r\n"); buf.append(" Origin\r\n"); buf.append(" Message\r\n"); buf.append(" \r\n"); } String statusLevelAsString(Status s) { switch (s.getEffectiveLevel()) { case Status.INFO: return "INFO"; case Status.WARN: return "WARN"; case Status.ERROR: return "ERROR"; } return null; } String abbreviatedOrigin(Status s) { Object o = s.getOrigin(); if (o == null) { return null; } String fqClassName = o.getClass().getName(); int lastIndex = fqClassName.lastIndexOf(CoreConstants.DOT); if (lastIndex != -1) { return fqClassName.substring(lastIndex + 1, fqClassName.length()); } else { return fqClassName; } } private void printStatus(StringBuilder buf, Status s) { String trClass; if (count % 2 == 0) { trClass = "even"; } else { trClass = "odd"; } buf.append(" \r\n"); String dateStr = SDF.format(s.getDate()); buf.append(" ").append(dateStr).append("\r\n"); buf.append(" ").append(statusLevelAsString(s)).append("\r\n"); buf.append(" ").append(abbreviatedOrigin(s)).append("\r\n"); buf.append(" ").append(s.getMessage()).append("\r\n"); buf.append(" \r\n"); if (s.getThrowable() != null) { printThrowable(buf, s.getThrowable()); } } private void printThrowable(StringBuilder buf, Throwable t) { buf.append(" \r\n"); buf.append("
");
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        t.printStackTrace(pw);
        buf.append(Transform.escapeTags(sw.getBuffer()));
        buf.append("    
\r\n"); buf.append(" \r\n"); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy