Can I ask whether the exception stack information that appears when the program is run is also saved into the log file specified by log4j

Source: Internet
Author: User
Tags log4j
Please ask if you can save the error stack information that occurs when the program is running into the log file specified by log4j.
For example, a run-time error occurs while the program is running, and the following error stack information is printed in the console window:
Exception in thread "main" java.lang.ArithmeticException:/By zero
At Des.main (des.java:43)

I specify the log output level to debug in the log4j configuration file, and the run-time error log that appears when the program is running can only be displayed in the console window, and there is no way to output them to the log file specified log4j. (now the log file can only get logs such as Log.debug (), Log.error () and other output, but not the run-time error stack log)

I know through things like:
try{
...
} catch (Exception e) {
Log.debug (e);
}

Way, but this is too much trouble, it is impossible to write such statements in all the code, there is no easy way.


------Solution--------------------
try{//Redirect output stream
PrintStream PS = new PrintStream (LogPath);
System.setout (PS);
System.seterr (PS);
}catch (FileNotFoundException e) {
E.printstacktrace ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.