From a76ce78f46955c4b0bdf003fe39396f58ed36814 Mon Sep 17 00:00:00 2001 From: danfickle Date: Fri, 10 Sep 2021 21:06:46 +1000 Subject: [PATCH] #103 - Output class name and message for thrown exceptions by default. Should make it easier to debug #103 and similar mysteries. --- .../util/XRSimpleLogFormatter.java | 95 ++++++++----------- 1 file changed, 38 insertions(+), 57 deletions(-) diff --git a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/util/XRSimpleLogFormatter.java b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/util/XRSimpleLogFormatter.java index 9a462a5e2..96ce42d3f 100644 --- a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/util/XRSimpleLogFormatter.java +++ b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/util/XRSimpleLogFormatter.java @@ -35,32 +35,54 @@ * @author Patrick Wright */ public class XRSimpleLogFormatter extends Formatter { - /** Description of the Field */ + /** MessageFormat for standard messages (without Throwable) */ private final MessageFormat mformat; - /** Description of the Field */ + /** MessageFormat for messages with a throwable */ private final MessageFormat exmformat; - /** Description of the Field */ - private final static String msgFmt; - /** Description of the Field */ - private final static String exmsgFmt; + + private final static String MSG_FMT = "{1} {2}:: {5}\n"; + private final static String EX_MSG_FMT = "{1} {2}:: {5} => {6}:: {7}\n"; private final boolean[] usedPlaceholderForMsgFmt; private final boolean[] usedPlaceholderForExmsgFmt; - /** Constructor for the XRSimpleLogFormatter object */ public XRSimpleLogFormatter() { + super(); + mformat = new MessageFormat(MSG_FMT); + exmformat = new MessageFormat(EX_MSG_FMT); + usedPlaceholderForMsgFmt = usedPlaceholder(mformat); + usedPlaceholderForExmsgFmt = usedPlaceholder(exmformat); + } + + /** + * Create a custom log formatter for use with: + * {@link JDKXRLogger#JDKXRLogger(boolean, Level, Handler, Formatter)} + * + * Options: + * + * Example (msgFmt): {1} {2}:: {5}\n

+ * Example (throwableMsgFmt): {1} {2}:: {5} => {6}:: {7}\n + */ + public XRSimpleLogFormatter(String msgFmt, String throwableMsgFmt) { super(); mformat = new MessageFormat(msgFmt); - exmformat = new MessageFormat(exmsgFmt); + exmformat = new MessageFormat(throwableMsgFmt); usedPlaceholderForMsgFmt = usedPlaceholder(mformat); usedPlaceholderForExmsgFmt = usedPlaceholder(exmformat); } /** * Identify which arguments are effectively used. - * - * @param messageFormat - * @return */ private static boolean[] usedPlaceholder(MessageFormat messageFormat) { boolean[] used = new boolean[9]; @@ -78,13 +100,10 @@ private static boolean[] usedPlaceholder(MessageFormat messageFormat) { /** * Format the given log record and return the formatted string. - * - * @param record PARAM - * @return Returns */ + @Override public String format( LogRecord record ) { - Throwable th = record.getThrown(); boolean[] placeholderUse = th == null ? usedPlaceholderForMsgFmt : usedPlaceholderForExmsgFmt; @@ -118,64 +137,26 @@ public String format( LogRecord record ) { /** * Localize and format the message string from a log record. - * - * @param record PARAM - * @return Returns */ + @Override public String formatMessage( LogRecord record ) { return super.formatMessage( record ); } /** * Return the header string for a set of formatted records. - * - * @param h PARAM - * @return The head value */ + @Override public String getHead( Handler h ) { return super.getHead( h ); } /** * Return the tail string for a set of formatted records. - * - * @param h PARAM - * @return The tail value */ + @Override public String getTail( Handler h ) { return super.getTail( h ); } - static { - msgFmt = Configuration.valueFor( "xr.simple-log-format", "{1} {2}:: {5}" ).trim() + "\n"; - exmsgFmt = Configuration.valueFor( "xr.simple-log-format-throwable", "{1} {2}:: {5}" ).trim() + "\n"; - } - -}// end class - -/* - * $Id$ - * - * $Log$ - * Revision 1.6 2005/04/07 16:15:47 pdoubleya - * Typo. - * - * Revision 1.5 2005/01/29 20:18:37 pdoubleya - * Clean/reformat code. Removed commented blocks, checked copyright. - * - * Revision 1.4 2004/10/23 14:06:57 pdoubleya - * Re-formatted using JavaStyle tool. - * Cleaned imports to resolve wildcards except for common packages (java.io, java.util, etc). - * Added CVS log comments at bottom. - * - * Revision 1.3 2004/10/18 12:08:37 pdoubleya - * Incorrect Configuration key fixed. - * - * Revision 1.2 2004/10/14 12:53:26 pdoubleya - * Added handling for exception messages with stack trace and separate message format. - * - * Revision 1.1 2004/10/14 11:13:22 pdoubleya - * Added to CVS. - * - */ - +}