ceki 2004/06/03 14:57:09 Modified: src/java/org/apache/log4j Layout.java SimpleLayout.java HTMLLayout.java TTCCLayout.java PatternLayout.java src/java/org/apache/log4j/xml XMLLayout.java Log: Made ignoresThrowable a settable property. The user can now force a layout to ignore the throwable in the event Revision Changes Path 1.11 +29 -10 logging-log4j/src/java/org/apache/log4j/Layout.java Index: Layout.java =================================================================== RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/Layout.java,v retrieving revision 1.10 retrieving revision 1.11 diff -u -r1.10 -r1.11 --- Layout.java 28 May 2004 17:16:43 -0000 1.10 +++ Layout.java 3 Jun 2004 21:57:09 -0000 1.11 @@ -45,6 +45,9 @@ String header; String footer; + // Most layouts ignore the throwable. If a subclasses needs to override the + // default value it should do so in its default constructor. + protected boolean ignoresThrowable = true; /** * Implement this method to create your own layout format. @@ -86,17 +89,33 @@ /** - If the layout handles the throwable object contained within - [EMAIL PROTECTED] LoggingEvent}, then the layout should return - <code>false</code>. Otherwise, if the layout ignores throwable - object, then the layout should return <code>true</code>. - - <p>The [EMAIL PROTECTED] SimpleLayout}, [EMAIL PROTECTED] TTCCLayout}, [EMAIL PROTECTED] - PatternLayout} all return <code>true</code>. The [EMAIL PROTECTED] - org.apache.log4j.xml.XMLLayout} returns <code>false</code>. + * If the layout handles the throwable object contained within + * [EMAIL PROTECTED] LoggingEvent}, then the layout should return <code>false</code>. + * Otherwise, if the layout ignores throwable object, then the layout should + * return <code>true</code>. + * + * <p>By default, [EMAIL PROTECTED] SimpleLayout}, [EMAIL PROTECTED] TTCCLayout}, [EMAIL PROTECTED] + * PatternLayout} all return <code>true</code>. The [EMAIL PROTECTED] + * org.apache.log4j.xml.XMLLayout} returns <code>false</code>. + * + * <p>As of log4j version 1.3, ignoresThrowable is a settable property. Thus, + * you can override, a layout's default setting. + * + * @since 0.8.4 + * */ + public boolean ignoresThrowable() { + return ignoresThrowable; + } - @since 0.8.4 */ - public abstract boolean ignoresThrowable(); + /** + * + * @since 1.3 + * @param ignoresThrowable + */ + public void setIgnoresThrowable(boolean ignoresThrowable) { + this.ignoresThrowable = ignoresThrowable; + } + /** * Set the footer. Note that some layout have their own footers and may choose 1.15 +0 -10 logging-log4j/src/java/org/apache/log4j/SimpleLayout.java Index: SimpleLayout.java =================================================================== RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/SimpleLayout.java,v retrieving revision 1.14 retrieving revision 1.15 diff -u -r1.14 -r1.15 --- SimpleLayout.java 27 Feb 2004 16:47:28 -0000 1.14 +++ SimpleLayout.java 3 Jun 2004 21:57:09 -0000 1.15 @@ -61,14 +61,4 @@ output.write(event.getRenderedMessage()); output.write(LINE_SEP); } - - /** - The SimpleLayout does not handle the throwable contained within - [EMAIL PROTECTED] LoggingEvent LoggingEvents}. Thus, it returns - <code>true</code>. - - @since version 0.8.4 */ - public boolean ignoresThrowable() { - return true; - } } 1.34 +96 -86 logging-log4j/src/java/org/apache/log4j/HTMLLayout.java Index: HTMLLayout.java =================================================================== RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/HTMLLayout.java,v retrieving revision 1.33 retrieving revision 1.34 diff -u -r1.33 -r1.34 --- HTMLLayout.java 1 May 2004 13:10:55 -0000 1.33 +++ HTMLLayout.java 3 Jun 2004 21:57:09 -0000 1.34 @@ -23,36 +23,38 @@ import org.apache.log4j.spi.LocationInfo; import org.apache.log4j.spi.LoggingEvent; - /** - This layout outputs events in a HTML table. - - @author Ceki Gülcü + * This layout outputs events in a HTML table. + * + * @author Ceki Gülcü */ public class HTMLLayout extends Layout { + static final String TRACE_PREFIX = "<br> "; /** - A string constant used in naming the option for setting the the - location information flag. Current value of this string - constant is <b>LocationInfo</b>. - - <p>Note that all option keys are case sensitive. - - @deprecated Options are now handled using the JavaBeans paradigm. - This constant is not longer needed and will be removed in the - <em>near</em> term. - - */ + * A string constant used in naming the option for setting the the location + * information flag. Current value of this string constant is <b>LocationInfo + * </b>. + * + * <p> + * Note that all option keys are case sensitive. + * + * @deprecated Options are now handled using the JavaBeans paradigm. This + * constant is not longer needed and will be removed in the + * <em>near</em> term. + * + */ public static final String LOCATION_INFO_OPTION = "LocationInfo"; /** - A string constant used in naming the option for setting the the - HTML document title. Current value of this string - constant is <b>Title</b>. - */ + * A string constant used in naming the option for setting the the HTML + * document title. Current value of this string constant is <b>Title </b>. + */ public static final String TITLE_OPTION = "Title"; + protected static final int BUF_SIZE = 256; + protected static final int MAX_CAPACITY = 1024; // output buffer appended to when format() is invoked @@ -60,66 +62,82 @@ // Print no location info by default boolean locationInfo = false; + String title = "Log4J Log Messages"; /** - The <b>LocationInfo</b> option takes a boolean value. By - default, it is set to false which means there will be no location - information output by this layout. If the the option is set to - true, then the file name and line number of the statement - at the origin of the log statement will be output. - - <p>If you are embedding this layout within an [EMAIL PROTECTED] - org.apache.log4j.net.SMTPAppender} then make sure to set the - <b>LocationInfo</b> option of that appender as well. + * Default constructor. + * + * @since 1.3 + */ + public HTMLLayout() { + super(); + // HTMLLayout prints and does not ignore exceptions. Hence the + // return value <code>false</code>. + ignoresThrowable = false; + } + + /** + * The <b>LocationInfo </b> option takes a boolean value. By default, it is + * set to false which means there will be no location information output by + * this layout. If the the option is set to true, then the file name and line + * number of the statement at the origin of the log statement will be output. + * + * <p> + * If you are embedding this layout within an [EMAIL PROTECTED] + * org.apache.log4j.net.SMTPAppender} then make sure to set the + * <b>LocationInfo </b> option of that appender as well. */ public void setLocationInfo(boolean flag) { locationInfo = flag; } /** - Returns the current value of the <b>LocationInfo</b> option. + * Returns the current value of the <b>LocationInfo </b> option. */ public boolean getLocationInfo() { return locationInfo; } /** - The <b>Title</b> option takes a String value. This option sets the - document title of the generated HTML document. - - <p>Defaults to 'Log4J Log Messages'. - */ + * The <b>Title </b> option takes a String value. This option sets the + * document title of the generated HTML document. + * + * <p> + * Defaults to 'Log4J Log Messages'. + */ public void setTitle(String title) { this.title = title; } /** - Returns the current value of the <b>Title</b> option. - */ + * Returns the current value of the <b>Title </b> option. + */ public String getTitle() { return title; } /** - Returns the content type output by this layout, i.e "text/html". + * Returns the content type output by this layout, i.e "text/html". */ public String getContentType() { return "text/html"; } /** - No options to activate. - */ + * No options to activate. + */ public void activateOptions() { } - public void format(java.io.Writer output, LoggingEvent event) throws java.io.IOException { - + public void format(java.io.Writer output, LoggingEvent event) + throws java.io.IOException { + output.write(Layout.LINE_SEP + "<tr>" + Layout.LINE_SEP); output.write("<td>"); - output.write(Long.toString(event.getTimeStamp() - LoggingEvent.getStartTime())); + output.write(Long.toString(event.getTimeStamp() + - LoggingEvent.getStartTime())); output.write("</td>" + Layout.LINE_SEP); output.write("<td title=\"" + event.getThreadName() + " thread\">"); @@ -161,19 +179,23 @@ output.write("</tr>" + Layout.LINE_SEP); if (event.getNDC() != null) { - output.write( - "<tr><td bgcolor=\"#EEEEEE\" style=\"font-size : xx-small;\" colspan=\"6\" title=\"Nested Diagnostic Context\">"); + output + .write("<tr><td bgcolor=\"#EEEEEE\" style=\"font-size : xx-small;\" colspan=\"6\" title=\"Nested Diagnostic Context\">"); Transform.escapeTags(event.getNDC(), output); output.write("</td></tr>" + Layout.LINE_SEP); } - String[] s = event.getThrowableStrRep(); - - if (s != null) { - output.write( - "<tr><td bgcolor=\"#993300\" style=\"color:White; font-size : xx-small;\" colspan=\"6\">"); - appendThrowableAsHTML(s, output); - output.write("</td></tr>" + Layout.LINE_SEP); + // If we are told to ignore the throwable we will ignore it. Otherwise, + // we will print it + if (!ignoresThrowable) { + String[] s = event.getThrowableStrRep(); + + if (s != null) { + output + .write("<tr><td bgcolor=\"#993300\" style=\"color:White; font-size : xx-small;\" colspan=\"6\">"); + appendThrowableAsHTML(s, output); + output.write("</td></tr>" + Layout.LINE_SEP); + } } } @@ -181,9 +203,7 @@ if (s != null) { int len = s.length; - if (len == 0) { - return; - } + if (len == 0) { return; } Transform.escapeTags(s[0], output); output.write(Layout.LINE_SEP); @@ -197,38 +217,35 @@ } /** - Returns appropriate HTML headers. - */ + * Returns appropriate HTML headers. + */ public String getHeader() { StringBuffer sbuf = new StringBuffer(); - sbuf.append( - "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">" - + Layout.LINE_SEP); + sbuf + .append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">" + + Layout.LINE_SEP); sbuf.append("<html>" + Layout.LINE_SEP); sbuf.append("<head>" + Layout.LINE_SEP); sbuf.append("<title>" + title + "</title>" + Layout.LINE_SEP); sbuf.append("<style type=\"text/css\">" + Layout.LINE_SEP); sbuf.append("<!--" + Layout.LINE_SEP); - sbuf.append( - "body, table {font-family: arial,sans-serif; font-size: x-small;}" - + Layout.LINE_SEP); - sbuf.append( - "th {background: #336699; color: #FFFFFF; text-align: left;}" - + Layout.LINE_SEP); + sbuf + .append("body, table {font-family: arial,sans-serif; font-size: x-small;}" + + Layout.LINE_SEP); + sbuf.append("th {background: #336699; color: #FFFFFF; text-align: left;}" + + Layout.LINE_SEP); sbuf.append("-->" + Layout.LINE_SEP); sbuf.append("</style>" + Layout.LINE_SEP); sbuf.append("</head>" + Layout.LINE_SEP); - sbuf.append( - "<body bgcolor=\"#FFFFFF\" topmargin=\"6\" leftmargin=\"6\">" - + Layout.LINE_SEP); + sbuf.append("<body bgcolor=\"#FFFFFF\" topmargin=\"6\" leftmargin=\"6\">" + + Layout.LINE_SEP); sbuf.append("<hr size=\"1\" noshade>" + Layout.LINE_SEP); - sbuf.append( - "Log session start time " + new java.util.Date() + "<br>" - + Layout.LINE_SEP); + sbuf.append("Log session start time " + new java.util.Date() + "<br>" + + Layout.LINE_SEP); sbuf.append("<br>" + Layout.LINE_SEP); - sbuf.append( - "<table cellspacing=\"0\" cellpadding=\"4\" border=\"1\" bordercolor=\"#224466\" width=\"100%\">" - + Layout.LINE_SEP); + sbuf + .append("<table cellspacing=\"0\" cellpadding=\"4\" border=\"1\" bordercolor=\"#224466\" width=\"100%\">" + + Layout.LINE_SEP); sbuf.append("<tr>" + Layout.LINE_SEP); sbuf.append("<th>Time</th>" + Layout.LINE_SEP); sbuf.append("<th>Thread</th>" + Layout.LINE_SEP); @@ -246,21 +263,14 @@ } /** - Returns the appropriate HTML footers. - */ + * Returns the appropriate HTML footers. + */ public String getFooter() { StringBuffer sbuf = new StringBuffer(); sbuf.append("</table>" + Layout.LINE_SEP); - sbuf.append("<br>" + Layout.LINE_SEP); - sbuf.append("</body></html>"); + sbuf.append("<br>" + Layout.LINE_SEP); + sbuf.append("</body></html>"); return sbuf.toString(); } - - /** - The HTML layout handles the throwable contained in logging - events. Hence, this method return <code>false</code>. */ - public boolean ignoresThrowable() { - return false; - } -} +} \ No newline at end of file 1.22 +0 -10 logging-log4j/src/java/org/apache/log4j/TTCCLayout.java Index: TTCCLayout.java =================================================================== RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/TTCCLayout.java,v retrieving revision 1.21 retrieving revision 1.22 diff -u -r1.21 -r1.22 --- TTCCLayout.java 27 Feb 2004 16:47:28 -0000 1.21 +++ TTCCLayout.java 3 Jun 2004 21:57:09 -0000 1.22 @@ -188,14 +188,4 @@ output.write(event.getRenderedMessage()); output.write(LINE_SEP); } - - /** - The TTCCLayout does not handle the throwable contained within - [EMAIL PROTECTED] LoggingEvent LoggingEvents}. Thus, it returns - <code>true</code>. - - @since version 0.8.4 */ - public boolean ignoresThrowable() { - return true; - } } 1.29 +0 -10 logging-log4j/src/java/org/apache/log4j/PatternLayout.java Index: PatternLayout.java =================================================================== RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/PatternLayout.java,v retrieving revision 1.28 retrieving revision 1.29 diff -u -r1.28 -r1.29 --- PatternLayout.java 27 Feb 2004 16:47:28 -0000 1.28 +++ PatternLayout.java 3 Jun 2004 21:57:09 -0000 1.29 @@ -506,16 +506,6 @@ } /** - The PatternLayout does not handle the throwable contained within - [EMAIL PROTECTED] LoggingEvent LoggingEvents}. Thus, it returns - <code>true</code>. - - @since 0.8.4 */ - public boolean ignoresThrowable() { - return true; - } - - /** Produces a formatted string as specified by the conversion pattern. */ public void format(Writer output, LoggingEvent event) throws IOException { 1.29 +101 -95 logging-log4j/src/java/org/apache/log4j/xml/XMLLayout.java Index: XMLLayout.java =================================================================== RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/xml/XMLLayout.java,v retrieving revision 1.28 retrieving revision 1.29 diff -u -r1.28 -r1.29 --- XMLLayout.java 9 May 2004 19:15:03 -0000 1.28 +++ XMLLayout.java 3 Jun 2004 21:57:09 -0000 1.29 @@ -14,7 +14,6 @@ * limitations under the License. */ - // Contributors: Mathias Bogaert package org.apache.log4j.xml; @@ -28,61 +27,75 @@ import java.util.Iterator; import java.util.Set; - /** - * The output of the XMLLayout consists of a series of log4j:event - * elements as defined in the <a - * href="doc-files/log4j.dtd">log4j.dtd</a>. It does not output a - * complete well-formed XML file. The output is designed to be - * included as an <em>external entity</em> in a separate file to form - * a correct XML file. - * - * <p>For example, if <code>abc</code> is the name of the file where - * the XMLLayout ouput goes, then a well-formed XML file would be: - * - <pre> - <?xml version="1.0" ?> - - <!DOCTYPE log4j:eventSet SYSTEM "log4j.dtd" [<!ENTITY data SYSTEM "abc">]> - - <log4j:eventSet version="1.2" xmlns:log4j="http://jakarta.apache.org/log4j/"> - &data; - </log4j:eventSet> - </pre> - - * <p>This approach enforces the independence of the XMLLayout and the - * appender where it is embedded. - * - * <p>The <code>version</code> attribute helps components to correctly - * intrepret output generated by XMLLayout. The value of this - * attribute should be "1.1" for output generated by log4j versions - * prior to log4j 1.2 (final release) and "1.2" for relase 1.2 and - * later. - * - * @author Ceki Gülcü + * The output of the XMLLayout consists of a series of log4j:event elements as + * defined in the <a href="doc-files/log4j.dtd">log4j.dtd </a>. It does not + * output a complete well-formed XML file. The output is designed to be included + * as an <em>external entity</em> in a separate file to form a correct XML + * file. + * + * <p> + * For example, if <code>abc</code> is the name of the file where the + * XMLLayout ouput goes, then a well-formed XML file would be: + * + * <pre> + * + * <?xml version="1.0" ?> + * + * <!DOCTYPE log4j:eventSet SYSTEM "log4j.dtd" [<!ENTITY data SYSTEM "abc">]> + * + * <log4j:eventSet version="1.2" xmlns:log4j="http://jakarta.apache.org/log4j/"> + * &data + * </log4j:eventSet> + * + * </pre> + * + * <p> + * This approach enforces the independence of the XMLLayout and the appender + * where it is embedded. + * + * <p> + * The <code>version</code> attribute helps components to correctly intrepret + * output generated by XMLLayout. The value of this attribute should be "1.1" + * for output generated by log4j versions prior to log4j 1.2 (final release) and + * "1.2" for relase 1.2 and later. + * + * @author Ceki Gülcü * @since 0.9.0 - * */ + */ public class XMLLayout extends Layout { private boolean locationInfo = false; /** - * The <b>LocationInfo</b> option takes a boolean value. By default, - * it is set to false which means there will be no location - * information output by this layout. If the the option is set to - * true, then the file name and line number of the statement at the - * origin of the log statement will be output. - * - * <p>If you are embedding this layout within an [EMAIL PROTECTED] + * Default constructor. + * + * @since 1.3 + */ + public XMLLayout() { + super(); + // The XMLLayout prints and does not ignore exceptions. Hence the + // return value <code>false</code>. + ignoresThrowable = false; + } + + /** + * The <b>LocationInfo </b> option takes a boolean value. By default, it is + * set to false which means there will be no location information output by + * this layout. If the the option is set to true, then the file name and line + * number of the statement at the origin of the log statement will be output. + * + * <p> + * If you are embedding this layout within an [EMAIL PROTECTED] * org.apache.log4j.net.SMTPAppender} then make sure to set the - * <b>LocationInfo</b> option of that appender as well. - * */ + * <b>LocationInfo </b> option of that appender as well. + */ public void setLocationInfo(boolean flag) { locationInfo = flag; } /** - Returns the current value of the <b>LocationInfo</b> option. + * Returns the current value of the <b>LocationInfo </b> option. */ public boolean getLocationInfo() { return locationInfo; @@ -93,10 +106,9 @@ } /** - * Formats a [EMAIL PROTECTED] LoggingEvent} in conformance with the log4j.dtd. - * */ - public void format(Writer output, LoggingEvent event) - throws IOException { + * Formats a [EMAIL PROTECTED] LoggingEvent}in conformance with the log4j.dtd. + */ + public void format(Writer output, LoggingEvent event) throws IOException { // We yield to the \r\n heresy. output.write("<log4j:event logger=\""); output.write(event.getLoggerName()); @@ -125,46 +137,48 @@ output.write("]]></log4j:NDC>\r\n"); } -// Set mdcKeySet = event.getMDCKeySet(); -// -// if ((mdcKeySet != null) && (mdcKeySet.size() > 0)) { -// /** -// * Normally a sort isn't required, but for Test Case purposes -// * we need to guarantee a particular order. -// * -// * Besides which, from a human readable point of view, the sorting -// * of the keys is kinda nice.. -// */ -// List sortedList = new ArrayList(mdcKeySet); -// Collections.sort(sortedList); -// -// output.write("<log4j:MDC>\r\n"); -// -// Iterator iter = sortedList.iterator(); -// -// while (iter.hasNext()) { -// String propName = iter.next().toString(); -// output.write(" <log4j:data name=\"" + propName); -// -// String propValue = event.getMDC(propName).toString(); -// output.write("\" value=\"" + propValue); -// output.write("\"/>\r\n"); -// } -// -// output.write("</log4j:MDC>\r\n"); -// } - - String[] s = event.getThrowableStrRep(); - - if (s != null) { - output.write("<log4j:throwable><![CDATA["); - - for (int i = 0; i < s.length; i++) { - output.write(s[i]); - output.write("\r\n"); - } + // Set mdcKeySet = event.getMDCKeySet(); + // + // if ((mdcKeySet != null) && (mdcKeySet.size() > 0)) { + // /** + // * Normally a sort isn't required, but for Test Case purposes + // * we need to guarantee a particular order. + // * + // * Besides which, from a human readable point of view, the sorting + // * of the keys is kinda nice.. + // */ + // List sortedList = new ArrayList(mdcKeySet); + // Collections.sort(sortedList); + // + // output.write("<log4j:MDC>\r\n"); + // + // Iterator iter = sortedList.iterator(); + // + // while (iter.hasNext()) { + // String propName = iter.next().toString(); + // output.write(" <log4j:data name=\"" + propName); + // + // String propValue = event.getMDC(propName).toString(); + // output.write("\" value=\"" + propValue); + // output.write("\"/>\r\n"); + // } + // + // output.write("</log4j:MDC>\r\n"); + // } + + if (!ignoresThrowable) { + String[] s = event.getThrowableStrRep(); + + if (s != null) { + output.write("<log4j:throwable><![CDATA["); + + for (int i = 0; i < s.length; i++) { + output.write(s[i]); + output.write("\r\n"); + } - output.write("]]></log4j:throwable>\r\n"); + output.write("]]></log4j:throwable>\r\n"); + } } if (locationInfo) { @@ -201,12 +215,4 @@ output.write("</log4j:event>\r\n\r\n"); } - - /** - The XMLLayout prints and does not ignore exceptions. Hence the - return value <code>false</code>. - */ - public boolean ignoresThrowable() { - return false; - } -} +} \ No newline at end of file
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]