Hi all,
In my program I need to create 2 log files, success and error. But I need to create either one of them, depending on the out come of the program execution. In other words, if program fails for any reason, I need to create the error log file and log any errors/exceptions. On the other hand, if program succeeds, then I need to log the normal process of the program.
Basically, I don't want to create the two files. So, here's what I have done:
I created 2 StringBuffer. errorBuffer and successBuffer, and I append to them during my program execution. Say there's any error, in my catch block, I append to errorBuffer. At the end, I determine, whether there was any error using a boolean flag, then based on that, using PrintStream, I create the file (success or error) and wirte the content of error/success buffer to my printStream.
But, when I do so, all lines are printed in the file into one single line.
Is it StringBuffer behaviour? how can I write multiple lines to printStream, using StringBuffer
Here's what I mean:
public static void main(String[] arg) {
DocActivityDetach da = new DocActivityDetach();
//getCredentialFromProxyService();
try {
sm = da.getSessionMgr();
da.startDetaching ();
} catch (Exception e) {
da.errorFlag = true;
errorBuffer.append(e.getMessage() + "\n"); <-- Notice \n Will it work?
e.printStackTrace();
} finally {
int STATUS = 0;
if (da.errorFlag) {
STATUS = 2;
da.writeToLog(errorLog, errorFile, errorBuffer);
} else {
da.writeToLog(successLog, successFile, successBuffer);
}
da.closeConnection ();
System.exit(STATUS);
}
}
private void writeToLog (PrintStream logFile, String fileName, StringBuffer buffer) {
try {
out = new FileOutputStream(fileName);
logFile = new PrintStream (out);
logFile.println(buffer.toString()); <---Writing to printStream
logFile.close();
} catch (IOException ioe) {
errorFlag = true;
ioe.printStackTrace();
}
}
But when I open the file, all the information are written in on single line.
Can someone please comment.
Thanks,