If for commons-loging, log4j, slf4j, Logback, etc. are already very clear, you can ignore this article. A few times to solve the log conflict problem when the simple summary of these concepts, I hope that the foundation does not understand the students can help, of course, if this piece has a deeper understanding of the students, but also contribute their knowledge and insight.
First, Concept
commons-logging : 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.
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 called simple Logging facade for Java:java simply 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 {} placeholder to replace, avoid old write logger.isxxxenabled this helpless judgment, bring performance improvement see: Http://www.slf4j.org/faq.html#logging_performance.
2.OSGI mechanism better compatible support
A picture wins thousands of words, a figure on the official website:
From can find, choose still a lot of.
Logback : As a general-purpose, fast and flexible log framework, Logback is 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. Specific advantages See:
Http://logback.qos.ch/reasonsToSwitch.html
Second, Common logging scenarios and considerations
1.commons-logging+log4j : A classic log implementation scenario. appear in various frameworks. such as spring, WEBX, Ibatis and so on. 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:
Import Org.apache.commons.logging.Log;
Import Org.apache.commons.logging.LogFactory;
private static Log logger = Logfactory.getlog (Commonsloggingtest.class);
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. Internally there is a Log4jlogger implementation class to the log interface that internally provides a 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 Classpah is scanned, If found then load the inside configuration, using the inside configuration.
3. Otherwise, look for the commons-logging.properties from the 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 Classpath configuration log4j.xml, then commons-logging will make log4j use Normal, and the code does not need to rely on any log4j code.
2.commons-logging+log4j+slf4j
If you are migrating to slf4j in an existing commons-logging system, you can do so by replacing commons-logging with SLF4J. The principle uses the 2nd of the above commons-logging loading. Need to introduce Org.slf4j.jcl-over-slf4j-1.5.6.jar. This jar package provides a bridging so that the underlying implementation is based on SLF4J. The principle is to store the configuration meta-inf/services/org.apache.commons.logging.logfactory in the jar package = Org.apache.commons.logging.impl.SLF4JLogFactory, and Commons-logging will find this serviceid when initializing, and use it as a logfactory.
After the bridge is finished, then how does the simple log façade slf4j internally load the appropriate log?
The principle is that SLF4J will bind the import org.slf4j.impl.StaticLoggerBinder at compile time; This class implements the binding access to the specific log scheme. Any kind of implementation based on SLF4J should have this class. Such as:
org.slf4j.slf4j-log4j12-1.5.6: Provides an adaptive implementation of the log4j.
org.slf4j.slf4j-simple-1.5.6: is a simple implementation that will directly hit the log to the console.
......
Then this place should be noticed: if there are any two implementations of the SLF4J package at the same time, it is possible to brew the tragedy, you may find that the log is missing, or all hit the console. The reason is that each of the two jar packages has its own org.slf4j.impl.StaticLoggerBinder, which is not deterministic at compile time. This place should pay special attention!! There have been several times because of this problem that causes log confusion.
3.slf4j+logback
SLF4J and log4j authors are all the same person.
Logback claims to have many advantages in various aspects of performance, but also very attractive.
Using SLF4J directly is also simple:
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
public class HelloWorld {
public static void Main (string[] args) {
Logger Logger = Loggerfactory.getlogger (Helloworld.class);
Logger.info ("Hello World");
}
}
The code also does not see any traces of the specific log implementation scheme.
Logback used to see some tempting introductions. Specific people can go to study. Logback.
Precautions
When using the log configuration, be sure to understand the requirements while avoiding conflicts.
When using SLF4J to avoid conflicts, be sure to ensure that only one implementation class jar package is inside.
When you encounter problems such as log confusion, you can troubleshoot these aspects
Original link: http://singleant.iteye.com/blog/934593
Java logs, (commons-loging, log4j, SLF4J, Logback Introduction)