log4j contrast java.util.logging

Source: Internet
Author: User
Which library is better for you? Summary   Does your Java program mess with a lot of System.out.println statements and stack traces (for example, Ex.printstacktrace ()) that are immediately placed. When you add debug information to a class in your project, your information output is interspersed with a lot of information from other developers, making your information difficult to identify and read. Do you use a simple log API and worry that it may not provide the flexibility and ability you need when your program is already in the product? If the answer to all of these questions is yes, it's time for you to choose an industrial-level and powerful log API and use it.   This article will help you select the log API by evaluating two widely used Java log libraries: The Apache Group ' s log4j and java.util.logging packages (using June instead). This article investigates how each library implements the log, evaluates their differences and similarities, and provides a few simple guidelines to help you decide which library to choose.   Log4j's introduction log4j is an open source log library that was developed as a subproject of the Apache Software Foundation ' s Log service project. Based on the IBM development of the log Library as early as 1990, its first version appeared in 1999. Log4j is a widely used in open source community, including some large projects such as JBoss and hibernate.   LOG4J's architecture is built on three big concepts: loggers,appenders, and layouts. (log, append, layout). These concepts allow developers to record information based on their type and priority, and to control where the information ends and their form. Loggers is the first object that your application invokes to initialize the logging of the information. When loggers is given a piece of information to record, loggers generates logging-event objects to encapsulate the given information. The loggers at this point loggingevents the Appender to which they are associated. Appenders sends the information contained in the loggingevents to the specified output destination. For example, a consoleappender writes information to System.out or a fileappender attaches it to a file. Before sending loggingevent information to the final output destination, some appender use the layout to create text representations of the information formatted in the expected format. For example, log4j includes the Xmllayout class for formatting LoggingeveNTS the string in XML.   in log4j, Loggingevent is assigned a level to specify their priority. Default levels in log4j (from high to low): Off,fatal,error,warn,info,debug, and all. Loggers and Appenders are also assigned to a level and are executed only if the level of logging requests is equal and larger than their level. For example, if a appender his level is error, it is requested to output a warn level loggingevent,appender will not write the given logevent. All loggers in the   log4j have a name. LOG4J organizes logger instances into a tree-like structure, according to their name, as well as the organization of packages in the Java language. As Log4j's document succinctly stated: "A logger is considered an ancestor of another logger, if its name is followed by a point that is the prefix of the logger name of the descendant." "For example, a descendant named" Org.nrdc "is considered to be an" org "logger. The "org.nrdc.logging" logger is a descendant of the "ORG.NRDC" logger and is a heavy descendant of the "org" logger. If the logger is not explicitly specified, it uses the level of the nearest parent that has been given a level. Loggers inherits Appenders from their parent classes, although they can also be configured to use only directly assigned to their appender.   When a logger is requested to record information, it first checks to see if the requested level is the same or greater than its valid level. If it is, it creates loggingevent according to the given information and transmits loggingevent to its appenders, formats it, and sends it to the output destination.   June's introduction   Java.util.logging package, Sun introduced it in 2002 Java SDK 1.4, as JSR 47 came, the Logging API specification. June is very similar to log4j-it uses the same concepts more or less correctly, but renames them. For example, Appenders is called "handlers", layout is called "formatters", and Loggingevent is called "Logrecords". Picture 1, summarizing the names and concepts of log4j and June. The use level of June is the same as that of log4j, andJune Log Inheritance property properties from their parent loggers, sort of like log4j inherited properties from the parent hierarchy. The concept of log4j to June is almost one-to-one; Although the two libraries differ in detail, any developer familiar with log4j needs to adjust their thesaurus to understand the word.   features different points   although log4j and June have almost the same concept, they are functionally different. They can be summed up as, "no matter what June can do, log4j can do-and do more." "They are mainly different in several fields, Appender/handler implementations, useful formatter/layout implementations, and configuration flexibility."   June contains 4 specific handler implementations, while log4j includes more than 12 Appender implementations. The handler of June is enough for basic logging-they allow you to write to a buffer, a console, a socket, and a file. Log4j's appenders, on the other hand, probably covers all the logging output destinations you can think of. They can write to NT logs or Unix syslog, or even send email. Picture 2 provides a summary of the appenders of the handler and log4j.   June contains two formatting classes: Xmlformatter and Simpleformatter. log4j contains the corresponding layouts: Xmllayout and simplelayout.log4j also provide ttcclayout, which format loggingevents to rich content strings, and htmllayout, which can be Format the loggingevent into the HMTL table.   Ttcclayout and htmllayout are very useful, log4j is indeed ahead of June, in Formatter/handler respect, due to patternlayout. The Patternlayout instance can take an expression type that has a large number of flexible configurations, similar to the printf function in C, with a character-converted pattern. In Patternlayout conversion mode, a specific transformation character is used to specify the information that is formatted for the layout of the output. For example, "%t" is used to specify the thread at which information is to be logged, the name of the class of the object that "%c" is used to start recording information, and "%m" to specify the information. "%t:%m" will cause the output to resemble this way "main thread:this is I message." "%c-%T:%m "will cause the output to resemble the" Org.nrdc.my-class-main thread:this is I message. " Pattern-layout is very useful, but the two formatter classes of June have no place to match such versatility. For June users, customizing formatter classes is rare, whereas most log4j users often need to learn how to use patternlayout conversion mode.   LOG4J and June are also able to use configuration files for configuration, log4j allow a wider range of configuration possibilities relative to the profile used by June. June can use it. Properties file configuration, but before j2se5.0, the handlers configuration can only be for each class, not for each instance. This means that if you're going to use the tiger version of the SDK, you'll miss out on useful configuration options, such as setting up different Filehandler instances to send their output to different files.   It is important to note that the tiger version of June can easily be configured to write to multiple files by writing code rather than through the default configuration mechanism. Log4j can pass. Properties files or XML file configurations, and Appenders can be configured on a per-instance basis. At the same time, LOG4J allows developers to associate layout instances and Appender instances, and to configure the layout on a per-instance basis. This includes Patternlayout instances-you can set the conversion type for each mode of use in the configuration file. During the development process, it is not a problem to adjust the log configuration by recompiling, however, you may not want to change or completely reconfigure the log in the application by recompiling. At this point, log4j provides more flexibility, especially tiger use.   LOG4J offers many of the missing features of June, although June is catching up. June can be fully expanded to do the things log4j do-you can write more handlers, realize patternlayout for June, and update the June configuration mechanism, these are not difficult. But why do they do this in the years that log4j already exist?   Which library do you want to choose?   Such important decisions often make leaders insomnia or premature aging. Fortunately, this decision can be made easily by testing for simple problems.   1 You need handlers, such as Smtphandler, that are not log4j and are premature., Nteventloghandler, or any other very convenient filehandlers?   Question 2 Do you want to frequently switch the format of your log output? Do you need to do this in a simple way. In addition, do you need to log4j the patternlayout.   Question 3 Whether you explicitly need to have this ability in your application to change complex log configurations in your application even if they have been compiled and deployed to the product environment. Your configuration doesn't sound like: "A few messages from this class are sent to technical support workers by mail; a few of the information in the class subset is recorded in the syslog of our server, and the warning information for the class subset is recorded in a file on network disk A, and all information is recorded in a file on network disk B. Whether you have not been in 10 days, will change.   If you answer yes to all of the above questions, use log4j. If your answer is all clear no,jul enough, it already exists in the SDK.   conclusion log4j and June are very similar APIs. They differ only in a few details and end up doing the same thing, except that log4j has more features, but maybe you can't.   Remember, when you migrate to the log library of your choice, the log will affect the performance of the application. The way to minimize the impact is to reuse loggers references as much as possible, save static or reference pointers to loggers, instead of calling Logger.getlogger ("Loggername") every time you need a logger. Log expressions are placed in public areas, not loops.   This article is not an in-depth study of how to use log4j or June, and, in fact, demonstrates a number of useful features in two libraries, such as Mbeans support (j2se5.0, you can set the June record level remotely via JMX), and resourcebundle support. There are also many advanced features of log4j, such as the filter chain and object-renderers. The internet has a lot of tutorials on how to use them, as well as in JDJ, and must find and learn them before coding.       Resources   log4j ' s home page:http://logging.apache.org/log4j ' s home page:http://java.sun.com/j2s E/1.4.2/docs/guide/util/logging Aggarwal, V. " Third party Logging API. " Java Developer ' s Journal, vol. 5, Issue 11:http://sys-con.com/story/?storyid=36144 Banes, J. "Building the Ultimate Loggi ng Solution. " Java Developer ' s Journal, vol. 9, issue 5:http://sys-con.com/story/?storyid=44698 writing a sweet log4j appender that Sen DS Instant Messages:www.106.ibm.com/developerworks/java/library/j-instlog/for Those who don ' t like June or log4j, try the Logging Toolkit for Java from ibm:www.alphaworks.ibm.com/tech/loggingtoolkit4j

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.