001: // Jericho HTML Parser - Java based library for analysing and manipulating HTML
002: // Version 2.5
003: // Copyright (C) 2007 Martin Jericho
004: // http://jerichohtml.sourceforge.net/
005: //
006: // This library is free software; you can redistribute it and/or
007: // modify it under the terms of either one of the following licences:
008: //
009: // 1. The Eclipse Public License (EPL) version 1.0,
010: // included in this distribution in the file licence-epl-1.0.html
011: // or available at http://www.eclipse.org/legal/epl-v10.html
012: //
013: // 2. The GNU Lesser General Public License (LGPL) version 2.1 or later,
014: // included in this distribution in the file licence-lgpl-2.1.txt
015: // or available at http://www.gnu.org/licenses/lgpl.txt
016: //
017: // This library is distributed on an "AS IS" basis,
018: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
019: // See the individual licence texts for more details.
020:
021: package au.id.jericho.lib.html;
022:
023: import java.util.logging.*;
024:
025: /**
026: * Provides basic formatting for log messages.
027: * <p>
028: * This class extends the <code>java.util.logging.Formatter</code> class, allowing it to be specified as a formatter for the <code>java.util.logging</code> system.
029: * <p>
030: * The static {@link #format(String level, String message, String loggerName)} method provides a means of using the same formatting
031: * outside of the <code>java.util.logging</code> framework. See the documentation of this method for more details.
032: */
033: public class BasicLogFormatter extends Formatter {
034: /**
035: * Determines whether the <a href="Logger.html#LoggingLevel">logging level</a> is included in the output.
036: * <p>
037: * The default value is <code>true</code>.
038: * <p>
039: * As this is a static property, changing the value will affect all <code>BasicLogFormatter</code> instances, as well as the behaviour of the
040: * static {@link #format(String level, String message, String loggerName)} method.
041: */
042: public static boolean OutputLevel = true;
043:
044: /**
045: * Determines whether the logger name is included in the output.
046: * <p>
047: * The default value is <code>false</code>.
048: * <p>
049: * The logger name used for all automatically created {@link Logger} instances is "<code>net.htmlparser.jericho</code>".
050: * <p>
051: * As this is a static property, changing the value will affect all <code>BasicLogFormatter</code> instances, as well as the behaviour of the
052: * static {@link #format(String level, String message, String loggerName)} method.
053: */
054: public static boolean OutputName = false;
055:
056: static final Formatter INSTANCE = new BasicLogFormatter();
057:
058: /**
059: * Returns a formatted string representing the log entry information contained in the specified <code>java.util.logging.LogRecord</code>.
060: * <p>
061: * This method is not called directly, but is used by the <code>java.util.logging</code> framework when this class is specified
062: * as a formatter in the <code>logging.properties</code> file.
063: * <p>
064: * See the documentation of the parent <code>java.util.logging.Formatter</code> class in the Java SDK for more details.
065: *
066: * @param logRecord a <code>java.util.logging.LogRecord</code> object containing all of the log entry information.
067: * @return a formatted string representing the log entry information contained in the specified <code>java.util.logging.LogRecord</code>.
068: */
069: public String format(final LogRecord logRecord) {
070: return format(logRecord.getLevel().getName(), logRecord
071: .getMessage(), logRecord.getLoggerName());
072: }
073:
074: /**
075: * Returns a formatted string representing the specified log entry information.
076: * <p>
077: * This method is used by the {@link WriterLogger} class to format its output.
078: * <p>
079: * The static properties {@link #OutputLevel} and {@link #OutputName} affect what information is included in the output.
080: * <p>
081: * The {@link Config#NewLine} static property determines the character sequence used for line breaks.
082: * <p>
083: * A line of output typically looks like this:
084: * <blockquote class="SmallVerticalMargin"><code>INFO: this is the log message</code></blockquote>
085: * or if the {@link #OutputName} property is set to <code>true</code>, the output would look similar to this:
086: * <blockquote class="SmallVerticalMargin"><code>INFO: [net.htmlparser.jericho] this is the log message</code></blockquote>
087: *
088: * @param level a string representing the <a href="Logger.html#LoggingLevel">logging level</a> of the log entry.
089: * @param message the log message.
090: * @param loggerName the name of the logger.
091: * @return a formatted string representing the specified log entry information.
092: */
093: public static String format(final String level,
094: final String message, final String loggerName) {
095: final StringBuffer sb = new StringBuffer(message.length() + 40);
096: if (OutputLevel)
097: sb.append(level).append(": ");
098: if (OutputName && loggerName != null)
099: sb.append('[').append(loggerName).append("] ");
100: sb.append(message);
101: sb.append(Config.NewLine);
102: return sb.toString();
103: }
104: }
|