I'm using the Enerjy (http://www.enerjy.com/) static code analyzer tool on my Java code. It tells me that the following line:

我在Java代码上使用Enerjy (http://www.enerjy.com/)静态代码分析器工具。它告诉我下面一行:

System.err.println("Ignored that database");

System.err。println(“忽略数据库”);

is bad because it uses System.err. The exact error is: "JAVA0267 Use of System.err"

不好是因为它使用了System.err。确切的错误是:“JAVA0267使用System.err”

What is wrong with using System.err?

使用系统有什么问题?

7 个解决方案

#1


22

Short answer: It is considered a bad practice to use it for logging purposes.

简短的回答:将其用于日志记录被认为是一种不好的做法。

It is an observation that in the old times when there where no widely available/accepted logging frameworks, everyone used System.err to print error messages and stack traces to the console. This approach might be appropriate during the development and local testing phase but is not appropriate for a production environment, because you might lose important error messages. Because of this, in almost all static analysis tools today this kind of code is detected and flagged as bad practice (or a similarly named problem).

这是一个观察,在过去没有广泛可用/接受的日志记录框架时,每个人都使用系统。错误地打印错误消息和堆栈跟踪到控制台。这种方法可能适用于开发和本地测试阶段,但不适用于生产环境,因为您可能会丢失重要的错误消息。正因为如此,在当今几乎所有的静态分析工具中,这类代码都会被检测并标记为不良实践(或类似的命名问题)。

Logging frameworks in turn provide the structured and logical way to log your events and error messages as they can store the message in various persistent locations (log file, log db, etc.).

日志框架反过来提供了结构化和逻辑的方式来记录事件和错误消息,因为它们可以将消息存储在不同的持久位置(日志文件、日志db等)。

The most obvious (and free of external dependencies) hack resolution is to use the built in Java Logging framework through the java.util.logging.Logger class as it forwards the logging events to the console by default. For example:

最明显(且不依赖外部)的破解方法是通过Java .util. Logging使用内置的Java日志框架。Logger类在默认情况下将日志事件转发到控制台。例如:

final Logger log = Logger.getLogger(getClass().getName());
...
log.log(Level.ERROR, "Something went wrong", theException);

(or you could just turn off that analysis option)

(或者你可以关闭分析选项)

更多相关文章

  1. 错误:警告:无法从javaldx读取路径
  2. Java错误:线程“main”中的异常java.lang.ArrayIndexOutOfBoundsE
  3. 不幸的是,在声明按钮时,模拟器中出现了错误
  4. 当我运行.jar时,在java.library中会得到一个“No lwjgl”。路线”
  5. 导入证书后的Java Keytool错误,“Keytool error: Java .io。FileN
  6. Cassandra Java驱动程序错误 - 所有主机尝试查询失败连接已关闭
  7. 线程“main”中的异常java.lang.RuntimeException:无法编译的源代
  8. java httpclient访问某些网页报403错误
  9. Java:IntelliJ想法生成的代码错误地为所有Class名称添加了其包名

随机推荐

  1. android SDK包引用了java SDK哪些包
  2. Android 开发人员必须掌握的 10 个开发工
  3. Android 当中的 MVP 模式(二)封装
  4. Android的建议
  5. Android SVG矢量图
  6. Android USB 开发详解
  7. Android API版本对应Android系统版本及内
  8. Android C/C++开发指南
  9. Android入门教程 (一) Android简介和androi
  10. 4.0.x Launcher UI