--- projects/cms/source/util/uk/org/iscream/cms/util/FormatName.java 2000/12/12 19:15:30 1.4 +++ projects/cms/source/util/uk/org/iscream/cms/util/FormatName.java 2001/02/25 20:32:50 1.5 @@ -2,6 +2,9 @@ package uk.ac.ukc.iscream.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.4 2000/12/12 19:15:30 ajm Exp $ + * @author $Author: tdb $ + * @version $Id: FormatName.java,v 1.5 2001/02/25 20:32:50 tdb Exp $ */ 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,9 +33,6 @@ public class FormatName { * This is of use to the override of the toString() as * implemented by most of the iscream objects. * - * It has support for null friendly name and null class name - * bit NOT both at the same time. - * * @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 @@ -34,7 +40,9 @@ public class FormatName { * @return an iscream standard name to be used as a toString() */ public static String getName(String friendlyName, String className, String revision) { - if (friendlyName == null) { + 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) + ")}"; @@ -42,7 +50,24 @@ public class FormatName { return friendlyName + "{"+ tidyClassName(className) + "(v" + 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.ac.ukc.iscream * this method will trim it off, otherwise it