Java-based open source log Library log4j Research notes

Source: Internet
Author: User
Tags naming convention hadoop ecosystem

Since the Hadoop ecosystem is basically Java-developed, log4j is often seen in many open source projects about big data processing.
The purpose of this article is to make a basic description of its usage.

1. What is log4j?
From the name of the log4j (log for Java) It is not difficult to see that it is a log library for Java. Specifically, it is an open source project under the Apache Foundation, which provides flexible log printing when the program is running without modifying the application code (of course, the application must also call log4j in code to print, but after the program is published, You can control the log output destination and the log level that allows output through the configuration file.
Regardless of which language is being developed, a large number of verbose logs will always degrade program performance, but Log4j has done a lot of optimizations in design/implementation to minimize the performance impact on applications, with the aim of "speed first , flexibility Second", for log4j performance considerations, refer to the instructions here.
For more features on log4j, you can see the log4j FAQ to learn: What is the features of log4j?

2. Three elements of log4j
log4j There are 3 important concepts: loggers, appenders and layouts, these 3 elements enable developers to introduce log4j libraries to flexibly control print behavior, such as log levels, in-loglog format and print destination (such as local or remote printing), and so on.
2.1 Loggers
in fact, loggers are some case-sensitive named entities that are named following a hierarchical naming convention (details can be viewed in document short introduction to log4j):
A logger is said to being an ancestor of another logger if it name followed by a dot is a prefix of the descendant Logge Rname. A logger is said to being a parent of a child logger if there is no ancestors between itself and the descendant logger.
in the loggers hierarchy, root logger is at the top level. When using the log4j library, root logger must be created and only through class static methods Logger.getrootlogger, and other logger can be instantiated by invoking the class static method Logger.getlogger by passing in the logger name.
You can specify a log level for logger instances (such as through the Setlevel interface) and currently support these common levels: TRACE, DEBUG, INFO, WARN, ERROR, FATAL. Do not considerTRACE, the remaining levels are important in the following order:DEBUG < INFO < WARN < ERROR < FATAL.
If a logger instance does not explicitly specify a level, it inherits the rank of the parent logger instance that is closest to it and is explicitly specified by the log level.
Once a level is specified for a logger instance, when the instance is called to print the log, only logs with a log level of not less than the specified level will be printed, and logs below the specified level will not bePrint it out. This rule is the core rule of the LOG4J library, which guarantees a flexible control of the log level .
2.2 appenders
Appenders is actually the purpose of the log printing address (inlog4j speak, an output destination is called an Appender), currently supported appenders include: console, files, GUI components, remote socket servers, JMS, NT Event loggers, and remote UNIX Syslog daemons.
A logger instance can have multiple appender (s), that is, the same log can be printed to multiple destinations at the same time.
by default, a log print request for a logger instance is printed to all appenders that have been added to the logger instance, and the log print request also follows the logger instance The hierarchical inheritance chain propagates upward to all appenders of its ancestor logger.
For example, after adding the console type of appender to root logger, the log print request for root logger is output to the console, which is easy to understand. Now suppose logger instance Cinherits from Root, and adds the file type of Appender to instance C, the ancestor of instance C, in addition to the file Appender output log when the instance C is called for log printing, is this exampleRoot logger also receives the log print request, and the console Appender also outputs the log because the console Appender is added to root logger. This tacitThe Logger additive field can beSet to False to close.
For more instructions and examples of Appender additivity, refer to the introduction to LOG4J document for a description of the Appender section.
2.3 Layouts
layouts can specify the format of the log, the supported Patternlayout in the document Log4j-class patternlayout detailed description, here do not repeat.

3. Configuration of the log4j
The configuration of the Log4j log library can be specified in the Java Program through code, or it can be specified by a configuration file and initialized with the start of the Java application. Obviously the latter is more flexible.
The following is a typical log4j configuration file:
Log4j.rootlogger=debug, stdout, rlog4j.appender.stdout= org.apache.log4j.consoleappenderlog4j.appender.stdout.layout=org.apache.log4j.patternlayout# Pattern to output the Caller ' s file name and line number.log4j.appender.stdout.layout.conversionpattern=%5p [%t] (%f:%l)-%m% nlog4j.appender.r=org.apache.log4j.rollingfileappenderlog4j.appender.r.file= example.loglog4j.appender.r.maxfilesize=100kb# Keep One backup filelog4j.appender.r.maxbackupindex= 1log4j.appender.r.layout=org.apache.log4j.patternlayoutlog4j.appender.r.layout.conversionpattern=%p%t%c-%m%n
In this configuration file, only 1 logger instances (that is, root logger) are specified, with a minimum log level of Debug and 2 Appender (named stdout and R respectively). Then, for the two appender name, the log4j truly supported Appender (Consoleappender and Rollingfileappender respectively) are specified. Each appender can specify its own log format through layout.

4. log4j Library Initialization process
LOG4J does not make any assumptions about its usage scenarios, so it does not have a default appender, that is, Appender must be explicitly configured by the consumer. When the java process that references the LOG4J library is started, the classloader mechanism of the JVM loads the logger class that the application refers to. The static initialization function of the class attempts to automatically configure log4j. LOG4J library default initialization process in document short introduction to log4j "Default initialization Procedure" Part of the detailed description, interested students can to see.
Note: The mechanism for the JVM to load a class (such as a System package or a 3rd party expansion pack) can be understood by the following documents:
1) Understanding the Java classloading mechanism
2) Understanding Extension class Loading
3) internals of Java Class Loading

Resources
1. Log4j Faq:what is the features of log4j?
2. Short Introduction to Log4j:ceki Gülcü, March 2002
3. log4j Docs:class Patternlayout
4. Understanding the Java classloading mechanism
5. Java docs:understanding Extension Class Loading
6. Internals of Java Class Loading

========================= EOF ========================


Java-based open source log Library log4j Research notes

Related Article

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.