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

查看:33
本文介绍了不要在服务器端代码中使用 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.

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

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

在此处阅读更多信息:

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

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