[LOGBack-dev] svn commit: r560 - in logback/trunk/logback-classic/src: main/java/ch/qos/logback/classic/html test/java/ch/qos/logback/classic/html
noreply.seb at qos.ch
noreply.seb at qos.ch
Tue Sep 12 11:48:41 CEST 2006
Author: seb
Date: Tue Sep 12 11:48:40 2006
New Revision: 560
Modified:
logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/html/HTMLLayout.java
logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/html/HTMLLayoutTest.java
Log:
- added table break each 10'000 logs
- updated html code to xhtml
- improved test
Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/html/HTMLLayout.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/html/HTMLLayout.java (original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/html/HTMLLayout.java Tue Sep 12 11:48:40 2006
@@ -13,9 +13,7 @@
import ch.qos.logback.classic.ClassicLayout;
import ch.qos.logback.classic.PatternLayout;
import ch.qos.logback.classic.helpers.Transform;
-import ch.qos.logback.classic.pattern.NopThrowableInformationConverter;
import ch.qos.logback.classic.pattern.ThrowableHandlingConverter;
-import ch.qos.logback.classic.pattern.ThrowableInformationConverter;
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.classic.spi.ThrowableInformation;
import ch.qos.logback.core.LayoutBase;
@@ -53,7 +51,6 @@
//private String timezone;
private String title = "Logback Log Messages";
- private boolean locationInfo;
private boolean internalCSS = false;
private String url2ExternalCSS = "http://logging.apache.org/log4j/docs/css/eventTable-1.0.css";
@@ -63,6 +60,8 @@
// counter keeping track of the rows output
private long counter = 0;
+ //max number of rows before we close the table and create a new one
+ private static final int ROW_LIMIT = 10000;
/**
* Constructs a PatternLayout using the DEFAULT_LAYOUT_PATTERN.
@@ -209,8 +208,8 @@
*/
public String getHeader() {
StringBuffer sbuf = new StringBuffer();
- sbuf.append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"");
- sbuf.append(" \"http://www.w3.org/TR/html4/loose.dtd\">");
+ sbuf.append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"");
+ sbuf.append(" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
sbuf.append(LINE_SEP);
sbuf.append("<html>");
sbuf.append(LINE_SEP);
@@ -225,7 +224,7 @@
} else {
sbuf.append("<LINK REL=StyleSheet HREF=\"");
sbuf.append(url2ExternalCSS);
- sbuf.append("\" TITLE=\"Basic\">");
+ sbuf.append("\" TITLE=\"Basic\" />");
}
sbuf.append(LINE_SEP);
sbuf.append("</head>");
@@ -233,23 +232,28 @@
sbuf.append("<body>");
sbuf.append(LINE_SEP);
- sbuf.append("<hr size=\"1\" noshade>");
+ sbuf.append("<hr size=\"1\" noshade=\"true\" />");
sbuf.append(LINE_SEP);
sbuf.append("Log session start time ");
sbuf.append(new java.util.Date());
- sbuf.append("<br>");
+ sbuf.append("<br />");
sbuf.append(LINE_SEP);
- sbuf.append("<br>");
+ sbuf.append("<br />");
sbuf.append(LINE_SEP);
sbuf.append("<table cellspacing=\"0\">");
sbuf.append(LINE_SEP);
-
- sbuf.append("<tr class=\"header\">");
- sbuf.append(LINE_SEP);
+ createTableHeader(sbuf);
+
+ return sbuf.toString();
+ }
+
+ private void createTableHeader(StringBuffer sbuf) {
Converter c = head;
String name;
+ sbuf.append("<tr class=\"header\">");
+ sbuf.append(LINE_SEP);
while (c != null) {
name = computeConverterName(c);
if (name == null) {
@@ -259,7 +263,6 @@
sbuf.append("<td class=\"");
sbuf.append(computeConverterName(c));
sbuf.append("\">");
- sbuf.append("<td>");
sbuf.append(computeConverterName(c));
sbuf.append("</td>");
sbuf.append(LINE_SEP);
@@ -267,9 +270,9 @@
}
sbuf.append("</tr>");
sbuf.append(LINE_SEP);
-
- return sbuf.toString();
}
+
+
/**
* Returns the appropriate HTML footers.
@@ -297,7 +300,9 @@
}
public String doLayout(LoggingEvent event) {
-
+ StringBuffer buf = new StringBuffer();
+ handleTableClosing(buf);
+
boolean odd = true;
if (((counter++) & 1) == 0) {
odd = false;
@@ -305,7 +310,7 @@
String level = event.getLevel().toString().toLowerCase();
- StringBuffer buf = new StringBuffer();
+
buf.append(LINE_SEP);
buf.append("<tr class=\"");
buf.append(level);
@@ -355,6 +360,18 @@
return buf.toString();
}
+ private void handleTableClosing(StringBuffer sbuf) {
+ if (this.counter >= ROW_LIMIT) {
+ counter = 0;
+ sbuf.append("</table>");
+ sbuf.append(LINE_SEP);
+ sbuf.append("<br />");
+ sbuf.append("<table cellspacing=\"0\">");
+ sbuf.append(LINE_SEP);
+ createTableHeader(sbuf);
+ }
+ }
+
private void appendEventToBuffer(StringBuffer buf, Converter c, LoggingEvent event) {
buf.append("<td class=\"");
buf.append(computeConverterName(c));
Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/html/HTMLLayoutTest.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/html/HTMLLayoutTest.java (original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/html/HTMLLayoutTest.java Tue Sep 12 11:48:40 2006
@@ -1,5 +1,7 @@
package ch.qos.logback.classic.html;
+import java.util.List;
+
import junit.framework.TestCase;
import org.dom4j.Document;
@@ -30,6 +32,7 @@
layout = new HTMLLayout();
layout.setContext(lc);
layout.setPattern("%level %thread %msg");
+ layout.setInternalCSS(true);
layout.start();
appender.setLayout(layout);
logger = lc.getLogger(LoggerContext.ROOT_NAME);
@@ -44,14 +47,22 @@
layout = null;
}
+ @SuppressWarnings("unchecked")
public void testHeader() throws Exception {
String header = layout.getHeader();
- // System.out.println(header);
- assertTrue(header.indexOf("Level") == 422);
- assertTrue(header.indexOf("Literal") == 456);
- assertTrue(header.indexOf("Thread") == 494);
- assertTrue(header.lastIndexOf("Literal") == 543);
- assertTrue(header.indexOf("Message") == 139);
+ //System.out.println(header);
+
+ Document doc = parseOutput(header + "</table></body></html>");
+ Element rootElement = doc.getRootElement();
+ Element bodyElement = rootElement.element("body");
+ Element tableElement = bodyElement.element("table");
+ Element trElement = tableElement.element("tr");
+ List<Element> elementList = trElement.elements();
+ assertEquals("Level", elementList.get(0).getText());
+ assertEquals("Literal", elementList.get(1).getText());
+ assertEquals("Thread", elementList.get(2).getText());
+ assertEquals("Literal", elementList.get(3).getText());
+ assertEquals("Message", elementList.get(4).getText());
}
public void testAppendThrowable() throws Exception {
@@ -93,6 +104,7 @@
// System.out.println(result);
}
+ @SuppressWarnings("unchecked")
public void testDoLayoutWithException() throws Exception {
layout.setPattern("%level %thread %msg %ex");
LoggingEvent le = createLoggingEvent();
@@ -100,44 +112,29 @@
"test Exception")));
String result = layout.doLayout(le);
- //System.out.println(result);
-
- Document doc = parseOutput(result);
- Element trElement = doc.getRootElement();
- assertEquals(6, trElement.elements().size());
- {
- Element tdElement = (Element) trElement.elements().get(0);
- assertEquals("DEBUG", tdElement.getText());
- }
- {
- Element tdElement = (Element) trElement.elements().get(1);
- assertEquals(" ", tdElement.getText());
- }
- {
- Element tdElement = (Element) trElement.elements().get(2);
- assertEquals("main", tdElement.getText());
- }
- {
- Element tdElement = (Element) trElement.elements().get(3);
- assertEquals(" ", tdElement.getText());
- }
- {
- Element tdElement = (Element) trElement.elements().get(4);
- assertEquals("test message", tdElement.getText());
- }
-// {
-// Element trElement2 = (Element) trElement.elements().get(5);
-// Element tdElement = (Element) trElement2.elements().get(0);
-// assertTrue(tdElement.getText().contains(
-// "java.lang.Exception: test Exception"));
-// }
+ String stringToParse = layout.getHeader();
+ stringToParse += result;
+ stringToParse += "</table></body></html>";
+
+ System.out.println(stringToParse);
+
+ Document doc = parseOutput(stringToParse);
+ Element rootElement = doc.getRootElement();
+ Element bodyElement = rootElement.element("body");
+ Element tableElement = bodyElement.element("table");
+ List<Element> trElementList = tableElement.elements();
+ Element exceptionRowElement = trElementList.get(2);
+ Element exceptionElement = exceptionRowElement.element("td");
+
+ assertEquals(3, tableElement.elements().size());
+ assertTrue(exceptionElement.getText().contains("java.lang.Exception: test Exception"));
}
- public void testLog() {
- for (int i = 0; i < 2000; i++) {
- logger.debug("test message");
- }
- }
+// public void testLog() {
+// for (int i = 1; i <= 100000; i++) {
+// logger.debug("test message" + i);
+// }
+// }
private LoggingEvent createLoggingEvent() {
LoggingEvent le = new LoggingEvent(this.getClass().getName(), logger,
More information about the logback-dev
mailing list