不要在服务器端代码中使用System.out.println [英] Do not use System.out.println in server side code

查看:292
本文介绍了不要在服务器端代码中使用System.out.println的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说使用 System.out.println 进行日志记录是一种非常糟糕的做法,这可能会迫使服务器失败。

I heard that using System.out.println for logging purposes is a very bad practice and this may force the server to fail.

我不使用这种方法,但我非常有兴趣知道为什么System.out.println在后端代码中使用时会产生垃圾。

I don't use this approach but I am very interested in knowing why System.out.println could make so trash things when used in backend code.

推荐答案

System.out.println是一个IO操作,因此非常耗时。
在代码中使用它的问题是,程序将等到println完成。对于小型网站来说这可能不是问题,但是只要你加载或多次迭代,你就会感到痛苦。

System.out.println is an IO-operation and therefor is time consuming. The Problem with using it in your code is, that your program will wait until the println has finished. This may not be a problem with small sites but as soon as you get load or many iterations, you'll feel the pain.

更好的方法是使用日志记录框架。
他们使用消息队列并仅在没有其他输出的情况下写入。

The better approach is to use a logging framework. They use a message queue and write only if no other output is going on.

另一个好处是您可以为不同目的配置单独的日志文件。
你的Ops团队会喜欢你的东西。

And another benefit is that you can configure separate log files for different purposes. Something your Ops team will love you for.

在这里阅读更多内容:

  • http://logging.apache.org/log4j/1.2/manual.html
  • Logger vs. System.out.println

这篇关于不要在服务器端代码中使用System.out.println的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆