避免使用 printStackTrace();改用记录器调用 [英] Avoid printStackTrace(); use a logger call instead
本文介绍了避免使用 printStackTrace();改用记录器调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用程序中,我通过 PMD 运行我的代码.它向我显示以下消息:
In my application, I am running my code through PMD.It shows me this message:
- 避免使用printStackTrace();改用记录器调用.
这是什么意思?
推荐答案
这意味着你应该使用日志框架,比如 logback 或 log4j 而不是直接打印异常:
It means you should use logging framework like logback or log4j and instead of printing exceptions directly:
e.printStackTrace();
你应该使用这个框架的 API 来记录它们:
you should log them using this frameworks' API:
log.error("Ops!", e);
日志框架为您提供了很大的灵活性,例如您可以选择是否要登录到控制台或文件 - 或者如果您发现它们在某些环境中不再相关,则可以跳过某些消息.
Logging frameworks give you a lot of flexibility, e.g. you can choose whether you want to log to console or file - or maybe skip some messages if you find them no longer relevant in some environment.
这篇关于避免使用 printStackTrace();改用记录器调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文