Is there any way to write error log or exception into a file in java. i have gone through Log4j. I googled about it but diidnt find a good solution. I have written a simple code
catch (Exception e) {
PrintWriter pw = new PrintWriter(new FileOutputStream("Log"));
e.printStackTrace(pw);
}
Is there any other way to log the errors or exception? can any body provide me wwith sample example of Log4j?
First read log4j Manual, it's easy to configure a rolling log file. You do not have to do any explicit file operations.
Second, whenever you catch an exception, do like this
It worth reading the manual. Even better read Complete log4j Manual
Now all error output is written into this file
Using
log4j
you can log exceptiosn quite easily:You can add the exception as a parameter to your log4j statement, e.g.
Provided you've got a file appender running OK, this will output the complete stack trace of the exception.
Look this tutorial about "File Appender"
See official Log4j short introduction and the "Configuration" section.
You can also make search about "RollingFileAppender" or "File appender".
You configure your logger to send its message to an appender. This appender can forward message towards the console (stdin), towards a file (FileAppender, RollingFileAppender...)...
Use this to perform error log: