log4j的优点

发布于 2024-08-31 02:37:38 字数 95 浏览 4 评论 0原文

log4j 相对于设置 System.outSystem.err 输出到日志文件有什么优势?

What's the advantage of log4j over set System.out and System.err to output to a log file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

躲猫猫 2024-09-07 02:37:38

从较高的层面来看,Log4j 相对于手动日志记录的优势在于,您可以将日志记录代码与您实际想要记录的内容以及您想要记录的位置和方式分离。有关日志记录详细程度/过滤、格式、日志位置甚至日志类型(文件、网络等)的详细信息均使用配置以声明方式处理,并通过 自定义附加程序,而不是您必须自己编写灵活性代码。

这一点至关重要,因为开发人员通常很难预测软件投入生产后日志记录需求将如何变化。管理该软件的运营团队可能需要较少详细的日志,可能需要多个日志,可能需要将这些日志发送到多个服务器,有时可能需要获取非常详细的数据以进行故障排除等。对于运营团队来说,如果他们需要,通常是不可能的改变日志记录的工作方式,说服开发人员进行重大代码更改。这通常会导致生产停机、运营和开发之间的摩擦以及时间的浪费。

从开发人员的角度来看,Log4j 使您不必更改代码来支持日志记录,并使您免受想要更改日志记录的人的困扰。它使管理您代码的人员能够解决自己的问题,而不是打扰您!

此外,由于 Log4j 是 Java 日志记录的事实上的标准,因此有许多可用的工具可以使用 Log4j 完成很酷的事情——此外,还可以防止您和您的运营团队重新发明轮子。

我最喜欢的功能是能够轻松编写附加程序将数据发送到非文件源,例如 SYSLOGSplunk 等,使您的应用程序可以轻松自定义登录到您的 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.

百思不得你姐 2024-09-07 02:37:38

实际上,您现在应该研究一下 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

想念有你 2024-09-07 02:37:38

我的最爱(不是全部)

  • 能够在配置中设置登录参数,无需重新编译
  • 能够设置日志写入方式(从文本文件到 SMTP 发件人)
  • 能够按严重性进行过滤

my favorites (not all)

  • Ability to set parameters of logging in config, without recompiling
  • Ability to set the way log is written (from text file to SMTP sender)
  • Ability to filter by severity
标点 2024-09-07 02:37:38

级别、格式化、记录到多个文件...如果代码运行时可能出现任何问题,日志记录框架(即使它是 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.

留一抹残留的笑 2024-09-07 02:37:38

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.

旧瑾黎汐 2024-09-07 02:37:38

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.

不疑不惑不回忆 2024-09-07 02:37:38

看一下,您就会了解 log4j 的强大功能:

log4j.properties 我在一个项目中使用过一次:

# ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF

# No appenders for rootLogger 
log4j.rootLogger=OFF

folder=..
prefix=
fileExtension=.log
htmlExtension=${fileExtension}.html
datestamp=yyyy-MM-dd/HH:mm:ss.SSS/zzz
layout=%d{${datestamp}} ms=%-4r [%t] %-5p %l %n%m %n%n

# myLogger logger
log4j.logger.myLogger=ALL, stdout, infoFile, infoHtml, errorFile

# stdout 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=${layout}
# infoFile 
log4j.appender.infoFile=org.apache.log4j.FileAppender
log4j.appender.infoFile.File=${folder}/${prefix}_info${fileExtension}
log4j.appender.infoFile.layout=org.apache.log4j.PatternLayout
log4j.appender.infoFile.layout.ConversionPattern=${layout}
# infoHtml 
log4j.appender.infoHtml=org.apache.log4j.FileAppender
log4j.appender.infoHtml.File=${folder}/${prefix}_info${htmlExtension}
log4j.appender.infoHtml.layout=org.apache.log4j.HTMLLayout
log4j.appender.infoHtml.layout.Title=Logs
log4j.appender.infoHtml.layout.LocationInfo=true
# errorFile 
log4j.appender.errorFile=org.apache.log4j.FileAppender
log4j.appender.errorFile.File=${folder}/${prefix}_error${fileExtension}
log4j.appender.errorFile.layout=org.apache.log4j.PatternLayout
log4j.appender.errorFile.layout.ConversionPattern=${layout}

# APPENDERS SETTINGS
log4j.appender.stdout.Threshold = ALL
log4j.appender.infoFile.Threshold = INFO
log4j.appender.infoHtml.Threshold = INFO
log4j.appender.errorFile.Threshold = WARN.

要更改 java 代码中的变量,您可以执行以下操作:

加载配置

如果配置存储在 Log4j 中,Log4j 将自动加载配置
文件名为“log4j.properties”,位于类路径下
“”(例如WEB-INF/classes/log4j.properties)。

我不喜欢这种方法,更喜欢加载配置
通过调用显式:

PropertyConfigurator.configure( Config.ETC + "/log4j.properties" );
这样我就可以随时重新加载配置,只要我的
应用程序仍在运行。我喜欢添加一个按钮
管理 jsp,“重新加载 Log4J”。

动态日志文件位置

许多人抱怨 Log4j 迫使您对位置进行硬编码
您的日志将保存在哪里。实际上,可以动态地
选择日志文件位置,特别是如果您使用 ${log.dir}
上面的属性替换技术。方法如下:

String dynamicLog = // log directory somehow chosen... 
Properties p = new Properties( Config.ETC + "/log4j.properties" ); 
p.put( "log.dir", dynamicLog ); // overwrite "log.dir" 
PropertyConfigurator.configure( p );

Take a look and you will understand the power of log4j :

log4j.properties I used once for a project :

# ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF

# No appenders for rootLogger 
log4j.rootLogger=OFF

folder=..
prefix=
fileExtension=.log
htmlExtension=${fileExtension}.html
datestamp=yyyy-MM-dd/HH:mm:ss.SSS/zzz
layout=%d{${datestamp}} ms=%-4r [%t] %-5p %l %n%m %n%n

# myLogger logger
log4j.logger.myLogger=ALL, stdout, infoFile, infoHtml, errorFile

# stdout 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=${layout}
# infoFile 
log4j.appender.infoFile=org.apache.log4j.FileAppender
log4j.appender.infoFile.File=${folder}/${prefix}_info${fileExtension}
log4j.appender.infoFile.layout=org.apache.log4j.PatternLayout
log4j.appender.infoFile.layout.ConversionPattern=${layout}
# infoHtml 
log4j.appender.infoHtml=org.apache.log4j.FileAppender
log4j.appender.infoHtml.File=${folder}/${prefix}_info${htmlExtension}
log4j.appender.infoHtml.layout=org.apache.log4j.HTMLLayout
log4j.appender.infoHtml.layout.Title=Logs
log4j.appender.infoHtml.layout.LocationInfo=true
# errorFile 
log4j.appender.errorFile=org.apache.log4j.FileAppender
log4j.appender.errorFile.File=${folder}/${prefix}_error${fileExtension}
log4j.appender.errorFile.layout=org.apache.log4j.PatternLayout
log4j.appender.errorFile.layout.ConversionPattern=${layout}

# APPENDERS SETTINGS
log4j.appender.stdout.Threshold = ALL
log4j.appender.infoFile.Threshold = INFO
log4j.appender.infoHtml.Threshold = INFO
log4j.appender.errorFile.Threshold = WARN.

To change the variables in your java code you can do :

Loading Configuration

Log4j will automatically load the configuration if it is stored in a
file called "log4j.properties" and is present on the classpath under
"" (e.g. WEB-INF/classes/log4j.properties).

I don't like that approach and prefer to load the configuration
explicitly by calling:

PropertyConfigurator.configure( Config.ETC + "/log4j.properties" );
This way I can reload the configuration at any time as long as my
application is still running. I like to add a button to an
administrative jsp, "Reload Log4J".

Dynamic Log File Location

Many people complain that Log4j forces you to hard-code the location
where your logs will be kept. Actually, it is possible to dynamically
choose the log-file location, especially if you use the ${log.dir}
property substitution technique above. Here's how:

String dynamicLog = // log directory somehow chosen... 
Properties p = new Properties( Config.ETC + "/log4j.properties" ); 
p.put( "log.dir", dynamicLog ); // overwrite "log.dir" 
PropertyConfigurator.configure( p );
尛丟丟 2024-09-07 02:37:38
  • 日志记录(记录发生的历史业务事件,您可以检查旧日志)
  • 跟踪应用程序(项目流程)
  • 调试应用程序(详细信息在粒度级别的方法中发生了什么//数据、值和所有内部方法)
  • 错误处理(信息关于发生的具体错误)
  • logging (Document historical business events that occur, you can check old logs)
  • track the application (project flow)
  • debugging the application (Detailed information what occurs in a method at granular level //data, value and all inside methods)
  • error handling (information about specific error that occur)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文