--- projects/cms/source/util/uk/org/iscream/cms/util/FormatName.java 2000/12/07 19:49:47 1.1 +++ projects/cms/source/util/uk/org/iscream/cms/util/FormatName.java 2001/05/29 17:02:35 1.12 @@ -1,7 +1,10 @@ //---PACKAGE DECLARATION--- -package uk.ac.ukc.iscream.util; +package uk.org.iscream.cms.server.util; //---IMPORTS--- +import java.util.Date; +import java.text.DateFormat; +import java.util.Locale; /** * This class provides static methods to format the @@ -9,13 +12,19 @@ package uk.ac.ukc.iscream.util; * the various objects within the system to create * a toString String to send to the logger. * - * @author $Author: ajm $ - * @version $Id: FormatName.java,v 1.1 2000/12/07 19:49:47 ajm Exp $ + * @author $Author: tdb $ + * @version $Id: FormatName.java,v 1.12 2001/05/29 17:02:35 tdb Exp $ */ -public class IscreamName { +public class FormatName { //---FINAL ATTRIBUTES--- + /** + * An array of names of verbosity levels. + * Thus logging messages are now "classed" by the level + */ + private final static String[] VERBOSITY_NAMES = {"FATAL ", "ERROR ", "WARNING", "SYSMSG ", "SYSINIT", "DEBUG "}; + //---STATIC METHODS--- /** @@ -24,26 +33,65 @@ public class IscreamName { * This is of use to the override of the toString() as * implemented by most of the iscream objects. * - * @param friendlyName the friendlyName of the calling class + * @param friendlyName the configured name of the instance of the calling component the class is in (eg "filter1") * @param className the class name of the calling class, as obtained by getClass().getName() * @param revision the CVS Revision number for the calling class * * @return an iscream standard name to be used as a toString() */ - public static String formatName(String friendlyName, String className, String revision) { - if (friendlyName == null) { - return "{"+ className + "}(" + revision.substring(11, revision.length() - 2) + ")"; + public static String getName(String friendlyName, String className, String revision) { + if (friendlyName == null && className == null) { + return "{static(v" + revision.substring(11, revision.length() - 2) + ")}"; + } else if (friendlyName == null) { + return "{"+ tidyClassName(className) + "(v" + revision.substring(11, revision.length() - 2) + ")}"; + } else if (className == null) { + return friendlyName + "{static(v" + revision.substring(11, revision.length() - 2) + ")}"; + } else { + return friendlyName + "{"+ tidyClassName(className) + "(v" + revision.substring(11, revision.length() - 2) + ")}"; } - return friendlyName + "{"+ className + "}(" + revision.substring(11, revision.length() - 2) + ")"; } + + /** + * This method generates a nicely formatted line for the log, + * including the date/time and the source of the message. The date + * and time are formatted using the DateFormat class, and the source + * class is formatted using the toString() method found in every + * source file. This is then prepended to the message and returned. + * + * @param source A string representation of the calling object. + * @param verbosity The verbosity of the message. + * @param message The message to be logged. + * @return The string to be written to the log. + */ + public static String formatLogLine(String source, int verbosity, String message){ + String date = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(new Date()); + return "[" + date + "] [" + VERBOSITY_NAMES[verbosity] + "] " + source + ": " + message; + } + /** + * If the class name begins with uk.org.iscream.cms.server + * this method will trim it off, otherwise it + * leaves the string unchanged. + * + * @param className the name of a class + * + * @return the tidy version of it + */ + private static String tidyClassName(String className) { + String prefix = "uk.org.iscream.cms.server"; + if (className.startsWith(prefix)) { + return className.substring(prefix.length()+1); + } + return className; + } + //---CONSTRUCTORS--- /** * A private constructor ensures an instance of this * class CANNOT be created. */ - private IscreamName() { + private FormatName() { // do nothing on purpose! }