[Warm Reminder] One of the fatal reasons to use e.printStackTrace() sparingly at work

Use special log objects to print logs in work

First conclusion

e.printStackTrace () with System.out.print() The same will not output information to the log file.

solution

use log.error(e.getMessage(),e); The error information is output to the log file through the log object.

The cause

I took over the needs of a lan booth at work, and a bug occurred. I got a log file from the server and quickly located the lines of program execution. One of the processes skipped before finishing.
I thought that if there is no exception, it should be executed.
Why is it not executed?
Is something abnormal?
No, the
program has written e.printStackTrace() to print abnormal information, and there is no abnormality in the log file! ! !

} catch(Exception e){
    
    
	e.printStackTrace();
} finally{
    
    
	log.info("step last!!!")
}
log.info("step outer 3!!!")

I immediately responded to the tester with a "ghost code"! ! !
After tossing for a long time, I found that this e.printStackTrace() is getting more and more unpleasant;
but, how can I, a 1 year experienced programmer, dare to question the code written by a 3-year experienced programmer?
Hiding in the corner doubting life.
Finally, I couldn't hold it anymore, and the test was rushed,
so I raised the first question "e.printStackTrace() must have a problem".
I searched the Internet and found that it was true.
e.printStackTrace() will not output errors to the log file. So it will be easier later.
No fuck to say! ! !

supplement

① Servers generally do not have a console, so they use log files to collect and print log information.
②e.printStackTrace() will print the information in the console and the separately set console log file. The general company does not set the console file. All use third-party objects such as Log4J that specialize in log output to output log files. Therefore, the operations printed in the console will not be printed to the log file. You can also list other operations by analogy.
③The e.printStackTrace() or System.out.print() operation can be used in the local program to print the log to facilitate the printing of information. It is an indispensable operation for students who can't debug in a single step. However, we should try our best to use a special Log object to print log information during work.

Guess you like

Origin blog.csdn.net/liangcheng0523/article/details/107685646