log4j的优点
log4j
相对于设置 System.out
和 System.err
输出到日志文件有什么优势?
What's the advantage of log4j
over set System.out
and System.err
to output to a log file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
从较高的层面来看,Log4j 相对于手动日志记录的优势在于,您可以将日志记录代码与您实际想要记录的内容以及您想要记录的位置和方式分离。有关日志记录详细程度/过滤、格式、日志位置甚至日志类型(文件、网络等)的详细信息均使用配置以声明方式处理,并通过 自定义附加程序,而不是您必须自己编写灵活性代码。
这一点至关重要,因为开发人员通常很难预测软件投入生产后日志记录需求将如何变化。管理该软件的运营团队可能需要较少详细的日志,可能需要多个日志,可能需要将这些日志发送到多个服务器,有时可能需要获取非常详细的数据以进行故障排除等。对于运营团队来说,如果他们需要,通常是不可能的改变日志记录的工作方式,说服开发人员进行重大代码更改。这通常会导致生产停机、运营和开发之间的摩擦以及时间的浪费。
从开发人员的角度来看,Log4j 使您不必更改代码来支持日志记录,并使您免受想要更改日志记录的人的困扰。它使管理您代码的人员能够解决自己的问题,而不是打扰您!
此外,由于 Log4j 是 Java 日志记录的事实上的标准,因此有许多可用的工具可以使用 Log4j 完成很酷的事情——此外,还可以防止您和您的运营团队重新发明轮子。
我最喜欢的功能是能够轻松编写附加程序将数据发送到非文件源,例如 SYSLOG, Splunk 等,使您的应用程序可以轻松自定义登录到您的 IT 部门已在使用的运营管理工具。
At a high level, the win from Log4j over manual logging is that you can decouple your logging code from what you actually want to log and where and how you want to log it. Details about logging verbosity/filtering, formatting, log location, and even log type (files, network, etc.) are handled declaratively using configuration and extensibly via custom appenders, rather you having to code that flexibility yourself.
This is critically important because it's often hard for developers to predict how logging needs will change once their software is in production. Operations teams managing that software may need less verbose logs, may need mulitple logs, may need to ship those logs to multiple servers, may need to sometimes get really verbose data for troubleshooting, etc. And it's usually impossible for operations teams, if they need to change how logging works, to convince the developer to make big code changes. This often leads to production downtime, friction between operations and development, and wasted time all around.
From the developer's point of view, Log4j insulates you from having to make code changes to support logging, and insulates you from being pestered by people who want logging changes. It enables people managing your code to scratch their own itch rather than bugging you!
Also, since Log4j is the de-facto standard for Java logging, there are lots of tools available which can do cool things with Log4j-- furthermore preventing you and your operations teams from re-inventing the wheel.
My favorite feature is the ability to easily write appenders send data to non-file sources, like SYSLOG, Splunk, etc. which makes it easy to your app's custom logging into operations management tools your IT department is already using.
实际上,您现在应该研究一下 slf4j 外观,因为它允许您使用 {} 占位符来实现最简洁的语句。然后,您可以使用 slf4j 背后适当的日志记录框架来处理日志语句的实际处理。这可以是 log4j 或 slf4j-simple,它只打印出所有 INFO、WARN 和 ERROR,并丢弃其余部分。
您需要进行的关键观察是,日志语句的写入是在编写代码时完成的,而所需内容的决定是在部署代码时完成的,这可能是在代码编写和测试之后的几年。 System.out.println 要求您物理更改代码以摆脱它们,这在严格的写入-测试-部署周期中是不可接受的。如果代码发生更改,则必须重新测试。使用 slf4j,您只需启用您想查看的内容即可。
我们在测试阶段进行了完整的日志记录,并在生产部署的初始阶段进行了相当详细的日志记录,之后我们只记录信息。这为我们提供了在极少可能调试案例的情况下的完整信息。
您可能会发现我写的这篇文章很有趣。目标受众是初级 Java 程序员,我的目的是从一开始就让他们养成良好的习惯。 http://runjva.appspot.com/logging101/index.html
Actually, you should look into the slf4j facade these days, as it allows you to use {}-placeholders for the most concise statements. You can then use the appropriate logging framework behind slf4j to handle the actual treatment of your log statements. This could be log4j or the slf4j-simple which just prints out all of INFO, WARN and ERROR, and discards the rest.
The crucial observation you need to make is that the WRITING of log statements is done when the code is written, and the DECISION of what is needed is done when the code is deployed, which may be years after the code was written and tested. System.out.println requires you to physically change your code to get rid of them, which is unacceptable in a rigid write-test-deploy cycle. IF the code changes, it must be retested. With slf4j you just enable those you want to see.
We have full logging in the test phase, and rather verbose logging in the initial period of a production deployment, after which we go down to information only. This gives us full information in a scenario where debugging a case is very rarely possible.
You might find this article I wrote interesting. The target audience is beginning Java programmers, with my intention of giving them good habits from the start. http://runjva.appspot.com/logging101/index.html
我的最爱(不是全部)
my favorites (not all)
级别、格式化、记录到多个文件...如果代码运行时可能出现任何问题,日志记录框架(即使它是 java.util.logging)确实非常有用。
Levels, formatting, logging to multiple files... A logging framework (even if it's java.util.logging) is really beneficial if there's a chance anything may go wrong while your code is running.
log4j 允许您记录各种资源,例如事件日志、电子邮件、文件系统等,同时允许您的应用程序与所有这些资源保持解耦。此外,您可以使用通用接口来登录所有各种资源,而无需学习或集成相应的 API。
log4j allows you to log to various resources e.g. event log, email, file system etc while allowing your application to remain decoupled from all of these resources. Furthermore, you get to use a common interface to log to all of the various resources without having to learn or integrate thier corresponding APIs.
Log4j 提供了根据大小轮换日志文件并根据数量删除日志文件 (logrotate) 的功能,因此您的服务器不会填满磁盘。我个人认为这是 Log4j 中更有价值的功能之一。
Log4j 也很流行并被许多开发人员理解。我最近工作过的三个公司在大多数项目中都使用了 Log4j。
Log4j offers the ability to rotate your log files based on size and delete them based on quantity (logrotate), so your servers don't fill up their disks. Personally I think that is one of the more valuable features in Log4j.
Also Log4j is popular and understood by many developers. The last three companies I've worked at have all used Log4j in most projects.
看一下,您就会了解 log4j 的强大功能:
log4j.properties 我在一个项目中使用过一次:
要更改 java 代码中的变量,您可以执行以下操作:
Take a look and you will understand the power of log4j :
log4j.properties I used once for a project :
To change the variables in your java code you can do :