...
| Code Block | ||
|---|---|---|
| ||
import java.util.logging.Logger;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ExceptionLog {
private static String logMessage;
private static Logger theLogger =
Logger.getLogger("ExceptionLog.class.getName()");
public static void main(String[] args) {
ExceptionLog log = new ExceptionLog();
FileWriter fw=null;
BufferedWriter bw=null;
try {
fw = new FileWriter("log_file.txt"); //this can throw an exception, but security exception is still logged
bw = new BufferedWriter(fw);
}catch (FileNotFoundException fne){ logMessage("File Not Found Exception!"); }
catch (IOException e) { logMessage("IO Exception!"); }
//some security exception occurs here
log.logMessage("Security Exception has occurred!");
log.writeLog(bw);
}
public static void logMessage(String message) {
logMessage = message;
}
public void writeLog(BufferedWriter bw) {
theLogger.info("Starting to log");
// log to file
System.err.println(logMessage);
//logging to a file can miss writing the original security exception
}
}
|
Note that this recommendation does not prevent a program from reopening a closed log file after it realizes that important data must be captured. While an IOException is possible, there is little one can do when writing the data to the log file is itself under question.
...