package logging; import java.io.*; import java.util.Date; import java.util.TimeZone; import java.text.DateFormat; /** Superclass of various Logging Objects */ public abstract class Logger{ /** Date Format for Printing Dates */ public static final DateFormat DateFmt = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT); static{ DateFmt.setTimeZone(TimeZone.getTimeZone("EST5EDT")); //DateFmt.setTimeZone(TimeZone.getTimeZone("EST")); } /** Simple notifications, no Date/Stamp needed; returns the msg given. */ public abstract String notify(String msg); /** Log an Error occurence; returns the msg given. */ public abstract String error(String msg); /** Capture an event, by logging the Date and message; returns the msg given. */ public String event(String msg){ return notify(stamp()+msg); } /** Returns a Time/Date Stamp */ protected String stamp(){ return DateFmt.format(new Date()); } /** Shutdown this Logger */ public abstract void shutdown(); /** Create a PlainText Logger out to the given File */ public static Logger text(String file)throws IOException{ return text(new FileOutputStream(file)); } /** Create a PlainText Logger out to the given OutputStream */ public static Logger text(OutputStream strm){ return text(new PrintStream(strm)); } /** Create a PlainText Logger out to the given PrintStream */ public static Logger text(PrintStream strm){ return new PlainText(strm); } /** Create a HTML Logger out to the given File */ public static Logger html(String file)throws IOException{ return html(new FileOutputStream(file)); } /** Create a HTML Logger out to the given OutputStream */ public static Logger html(OutputStream strm){ return html(new PrintStream(strm)); } /** Create a HTML Logger out to the given PrintStream */ public static Logger html(PrintStream strm){ return new HTMLText(strm); } /** General Output Logging to a Stream/File */ private static abstract class OutputLogger extends Logger{ PrintStream out; OutputLogger(PrintStream strm){ out = strm; } void log(String s){ out.print(s); } void logln(String s){ out.println(s); } public void shutdown(){ out.close(); } } /** Plain Text Output Logging */ private static class PlainText extends OutputLogger{ PlainText(PrintStream strm){ super(strm); } public String error(String msg){ logln(" !! "+msg); return msg; } public String notify(String msg){ logln(" -- "+msg); return msg; } protected String stamp(){ return super.stamp()+":"; } } /** HTML Output Logging */ private static class HTMLText extends OutputLogger{ HTMLText(PrintStream strm){ super(strm); } public String error(String msg){ logln(" !! "+msg+"
"); return msg; } public String notify(String msg){ logln(" "+msg+"
"); return msg; } protected String stamp(){ String s = super.stamp(); log(" ** "+s+""); return s; } } }