logback 比log4j更好的原因
Logback brings a very large number of improvements over log4j,big and small. They are too many to enumerate exhaustively. Based on our previous work on log4j,logback internals have been re-written to perform about ten times faster on certain critical execution paths. Not only are logback components faster,
Logback ships with detailed and constantly updated documentation. The traditional way of configuring logback is via an XML file. Most of the examples in the documentation use this XML syntax. However,as of logback version 0.9.22,configuration files written in Groovy are also supported. Compared to XML,Groovy-style configuration is more intuitive,consistent and has a shorter syntax. There is also a tool to automatically migrate your logback.xml files to logback.groovy. Logback-classic can automatically reload its configuration file upon modification. The scanning process is fast,contention-free,and dynamically scales to millions of invocations per second spread over hundreds of threads. It also plays well within application servers and more generally within the JEE environment as it does not involve the creation of a separate thread for scanning. Logback's FileAppender and all its sub-classes,including RollingFileAppender,can gracefully recover from I/O failures. Thus,if a file server fails temporarily,you no longer need to restart your application just to get logging working again. As soon as the file server comes back up,the relevant logback appender will transparently and quickly recover from the previous error condition. By setting the maxHistory property of TimeBasedRollingPolicy or SizeAndTimeBasedFNATP,you can control the maximum number of archived files. If your rolling policy calls for monthly rollover and you wish to keep one year's worth of logs,simply set the maxHistory property to 12. Archived log files older than 12 months will be automatically removed. RollingFileAppender can automatically compress archived log files during rollover. Compression always occurs asynchronously so that even for large log files,your application is not blocked for the duration of the compression. In prudent mode,multiple FileAppender instances running on multiple JVMs can safely write to the same log file. With certain limitations,prudent mode extends to RollingFileAppender. Lilith is a logging and access event viewer for logback. It is comparable to log4j's chainsaw,except that Lilith is designed to handle large amounts of logging data without flinching. Developers often need to juggle between several logback configuration files targeting different environments such as development,testing and production. These configuration files have substantial parts in common,differing only in a few places. To avoid duplication,logback supports conditional processing of configuration files with the help of Logback comes with a wide array of filtering capabilities going much further than what log4j has to offer. For example,let's assume that you have a business-critical application deployed on a production server. Given the large volume of transactions processed,logging level is set to WARN so that only warnings and errors are logged. Now imagine that you are confronted with a bug that can be reproduced on the production system but remains elusive on the test platform due to unspecified differences between those two environments (production/testing). With log4j,your only choice is to lower the logging level to DEBUG on the production system in an attempt to identify the problem. Unfortunately,this will generate large volume of logging data,making analysis difficult. More importantly,extensive logging can impact the performance of your application on the production system. With logback,you have the option of keeping logging at the WARN level for all users except for the one user,say Alice,who is responsible for identifying the problem. When Alice is logged on,she will be logging at level DEBUG while other users can continue to log at the WARN level. This feat can be accomplished by adding 4 lines of XML to your configuration file. Search for MDCFilter in the relevant section of the manual. SiftingAppender is an amazingly versatile appender. It can be used to separate (or sift) logging according to any given runtime attribute. For example,SiftingAppender can separate logging events according to user sessions,so that the logs generated by each user go into distinct log files,one log file per user. When logback prints an exception,the stack trace will include packaging data. Here is a sample stack trace generated by the logback-demo web-application. 14:28:48.835 [btpool0-7] INFO c.q.l.demo.prime.PrimeAction - 99 is not a valid value From the above,you can recognize that the application is using Struts version 1.2.9 and was deployed under jetty version 6.1.12. Thus,stack traces will quickly inform the reader about the classes intervening in the exception but also the package and package versions they belong to. When your customers send you a stack trace,as a developer you will no longer need to ask them to send you information about the versions of packages they are using. The information will be part of the stack trace. See "%xThrowable" conversion word for details. This feature can be quite helpful to the point that some users mistakenly consider it a feature of their IDE. Last but not least,the logback-access module,part of the logback distribution,integrates with Servlet containers such as Jetty or Tomcat to provide rich and powerful HTTP-access log functionality. Since logback-access was part of the initial design,all the logback-classic features you love are available in logback-access as well. We have listed a number of reasons for preferring logback over log4j. Given that logback builds upon on our previous work on log4j,simply put,logback is just a better log4j. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |