本文介绍了避免使用printStackTrace();改为使用记录器调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我正在通过PMD运行我的代码。它向我显示以下消息:

In my application, I am running my code through PMD.It shows me this message:

这是什么意思?

推荐答案

这意味着您应该使用日志框架,如或而不是直接打印例外:

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();改为使用记录器调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 17:24