Simple collation of Java logs

Source: Internet
Author: User
Tags throwable

Recently began to learn the Java Log System management, found that there are a lot of log system jar package, feel very messy, do not know the basic functions of these jar packages. Search the Internet after a few articles, do a bit of sorting, easy to see later.

The current log system mainly include: Commons-loging, log4j, slf4j, Logback.

commons-loging: Apache provides the earliest log façade interface. Avoid direct coupling to specific log schemes. Similar to the JDBC API interface, the specific JDBC driver implementation is implemented by the respective database providers. It is decoupled through the unified interface, but it also implements some simple log schemes internally.

We can look at the Org.apache.commons.logging.Log.java source code in Commons-logging:

1  Packageorg.apache.commons.logging; 2  Public InterfaceLog {3      Public Booleanisdebugenabled (); 4      Public Booleaniserrorenabled (); 5      Public Booleanisfatalenabled (); 6      Public Booleanisinfoenabled (); 7      Public Booleanistraceenabled (); 8      Public Booleaniswarnenabled (); 9      Public voidTrace (Object message); Ten      Public voidTrace (Object message, Throwable t);  One      Public voidDebug (Object message);  A      Public voidDebug (Object message, Throwable t);  -      Public voidinfo (Object message);  -      Public voidinfo (Object message, Throwable t);  the      Public voidwarn (Object message);  -      Public voidwarn (Object message, Throwable t);  -      Public voiderror (Object message);  -      Public voiderror (Object message, Throwable t);  +      Public voidFatal (Object message);  -      Public voidFatal (Object message, Throwable t);  +}
View Code

Obviously, as long as the Log interface is implemented, it is a veritable Logger component. This is similar to the JDBC API interface, which sets a standard specification.

in commons-logging, there are already simple implementation classes, such as Log4jlogger,jdk14logger.

log4j : A classic journaling solution. Inside the log system abstraction encapsulated into logger, appender, pattern and other implementations. We can easily implement the management and diversification of the log system through configuration files.

slf4j : All referred to as simple Logging facade for Java, is also Java plain log façade. is a façade package that is provided for different log frameworks. You can access a log implementation by not modifying any configuration at the time of deployment. And commons-loging should have the same intention. Personal feeling is better from the meter, there is no Commons so many unspoken rules. There are also two additional features:

1. Can support multiple parameters, and through the {} placeholder to replace, avoid old write logger.isxxxenabled this helpless judgment, bring performance improvement.

2.OSGI mechanism better compatible support.

logback : As a general-purpose reliable, fast and flexible log framework, it will be a complete implementation of the new log system as an alternative to log4j and slf4j. The official network says that it has excellent performance and that execution speed on critical paths is 10 times times log4j and memory consumption is less. At present, I know less about it, online is also relatively rare, so still to learn.

There may be doubts here, Org.apache.commons.logging.Log and Org.apache.log4j.Logger These two classes, through the package name we can find that they are Apache projects, since the following, Why do you move such a big action to make two things (referring to commons-logging and log4j) out? In fact, before Sun developed the logger, the Apache project had developed a powerful log4j logging tool and recommended it to sun as part of the JDK, but Sun rejected Apache's proposal, and Sun later developed a set of logging tools. But now the open source projects are using log4j,log4j has become the de facto standard, but because a part of the developers are using sun's logger, so Apache introduced commons-logging, so we do not have to focus on what logging tool we are using.

Common log configuration scenarios and considerations:

1.commons-logging+log4j:

A classic log implementation scenario. appear in various frameworks. Use log4j directly to meet our logging scenarios. But generally in order to avoid directly relying on specific log implementations, is generally combined with commons-logging to achieve. The common code is as follows:

1 Import Org.apache.commons.logging.Log; 2 Import org.apache.commons.logging.LogFactory; 3 Private Static Log logger = Logfactory.getlog (commonsloggingtest.  Class);
View Code

On the code, there is no dependency on any of the LOG4J internal classes. So how did the log4j get loaded?

Log is an interface declaration. The inside of the logfactory will load the specific log system and obtain the implementation class that implements the log interface. The internal Log4jlogger implementation class implements the log interface and provides an internal proxy for the log4j logger. Logfactory internal Load Log system flow:

(1) First, look for the org.apache.commons.logging.LogFactory attribute configuration

(2) Otherwise, using the service discovery mechanism provided by JDK1.3, the Meta-inf/services/org.apache.commons.logging.logfactory file under Classpath will be scanned, If found then load the inside configuration, using the inside configuration.

(3) Otherwise, look for commons-logging.properties from Classpath, and find the load according to the configuration inside.

(4) Otherwise, use the default configuration: If you can find log4j, the default is to use the log4j implementation, and if not, use the Jdk14logger implementation, and then use the Simplelog implementation provided internally by Commons-logging.

From the above loading process, if you do not have any configuration, as long as the introduction of LOG4J and configured in Classpath log4j.xml or log4j.propertites, then commons-logging will make log4j use normal, The code does not need to rely on any log4j code.

2.log4j+slf4j:

Now the more popular collocation, its implementation code and the previous combination of code similar:

1 Import Org.slf4j.Logger; 2 Import org.slf4j.LoggerFactory; 3 Logger Logger = Loggerfactory.getlogger (String name);
View Code

3.Slf4j+logback:

Logbacklogback is divided into three modules: Logback-core,logback-classic,logback-access. Logback-core is the core; Logback-classic improves the log4j and implements the SLF4J API itself, so using logback you can still use other log implementations, such as the original log4j,java.util.logging, etc. Logback-access makes it easy for you to access log information, such as through HTTP. It is also important to mention thatLogback can be better placed in an OSGI environment.

Configuration file is generally logback.xml or logback-test.xml (similar to Log4j.properties/log4j.xml)

Logback first Find Logback-test.xml, no then find Logback.xml file, can not find the use of Basicconfigurator basic configuration, Basicconfigurator is equivalent to wait will be posted Logback.xml the configuration of the file contents. Here we use the Logback.xml configuration file, the previous Log4j.properties file can be converted to Logback.xml file content with Propertiestranslator.

Simple collation of Java logs

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.