I'm new to java and programming. Trying to learn on my own with books and the help of this community(thanks!). I'm sure something exists but i'm not sure how to approach my problem.
Here's some background: I have a java app that runs on a server and parses many pages(over 20,000). It takes several hours and once it fails its a bit hard to troubleshoot. As it processes the开发者_如何学Go files and writes them to a database, I also have it write to a file so I can see the last entry it was working on before it failed. This is helpful sometimes but sometimes it fails and I am not sure why(because it works on netbeans).
So my question...is there a way to get that error stack that I get when I run the app in netbeans? the output of the file only writes what I tell it to write and not the error stack. I tried to run the cron job with a > error.txt and it didn't help. Is this written to a file or somewhere? What I want to end up with is the job running every day and email me the log file I create and that java error stack when it fails.
Any ideas?
I think the problem is that you are redirecting stdout and not stderr--I believe the exception is sent to stderr and ">" redirects stdout.
In bash try "2>" to redirect stderr.
Have you looked at logging framework log4j ?
You can enable logging in your app. also you can log the required stuff to File
also.
You can use logging framework log4j log statements in your program and you can also use Thread.dumpStackTrace()
in your code at places you think causes exceptions.
Modify your main method to do something like this:
public static main (String[] args)
{
try
{
//your code
}catch(Exception e){
e.printStackTrace(yourLoggingPrintStream);
}
}
see printStackTrace
In the long term, I recommend a logging system like log4j or logback (if the app is important enough)
精彩评论