import java.util.Map; import java.util.Set; import java.util.HashMap; import java.io.File; import java.io.PrintWriter; import java.io.FileWriter; import java.io.BufferedReader; import java.io.FileReader; /** A general 'logger' or 'tracer' class that logs data into a file.
public class LogTest { public LogTest() { Log.open("c:\project\ErrorStream.log", "err"); Log.open("c:\project\StandardStream.log", "std"); Log.open("c:\prod.output", "out"); this.testLog(); Log.close("err"); Log.close(); } public void testLog() { Log.log("err", "This is an error Test.", false); Log.log("std", "This is a standard test."); Log.log("This will also output to standard since it was last opened and written to."); Log.log("out", "This is to output."); Log.log("Now this will write to the out stream."); } }@version 0.4, 05/06/03 @author Winson Chung */ public class Log { /** Map of all open streams. */ public static Map openFiles; /** Stores the last open stream for quick logging. */ public static String lastStream; /** Opens a file to write into. @param file the actualy path of the file that this log will write to. @param name the simple name that this specific log will be referenced by.*/ public static void open(String file, String name) { openFiles = new HashMap(); File newFile = new File(file); try { PrintWriter writer = new PrintWriter(new FileWriter(newFile)); //if file already exists, append to file. if (newFile.exists()) { BufferedReader br = new BufferedReader(new FileReader(newFile)); String ln = br.readLine(); while (ln != null) { writer.println(ln); ln = br.readLine(); } } writer.println(java.util.Calendar.getInstance().getTime()); openFiles.put(name, writer); lastStream = name; }catch (Exception e) { e.printStackTrace(); } } /** Logs input into file only if it is open. Sets the current stream to that of
name
.
@param name the name of a file stream to log to.
@param input the object input to log.
@param nl whether to write a 'new line' to end of line or not.
@return whether the log was successful or not. The only occurance that will cause the logger to return false is if the log ('name') has not been opened before hand. */
public static boolean log(String name, Object input, boolean nl) {
if (openFiles == null)
return false;
lastStream = name;
Object obj = openFiles.get(name);
if (obj != null) {
if (nl)
((PrintWriter)obj).println(input);
else
((PrintWriter)obj).print(input);
return true;
}else {
return false;
}
}
/** Default log method, creates new line for each input. Sets the current stream to that of name
.
@param name the name of a file stream to log to.
@param input the object input to log.
@return whether the log was successful or not. The only occurance that will cause the logger to return false is if the log ('name') has not been opened before hand.*/
public static boolean log(String name, Object input) {
return Log.log(name, input, true);
}
/** Lazy man's log method, logs to the last open stream, new line by default. */
public static boolean log(Object input) {
return Log.log(lastStream, input, true);
}
/** Closes the iostream for a all log files. */
public static void close() {
Object[] openWriters = openFiles.keySet().toArray();
for (int i = 0; i != openWriters.length; i++)
((PrintWriter)openFiles.get(openWriters[i])).close();
openFiles = null;
}
/** Closes the iostream for a specific log file.
@param name the name of the log to close. */
public static void close(String name) {
((PrintWriter)openFiles.get(name)).close();
openFiles.put(name, null);
}
}