The relationship between commons-logging \ log4j \ slf4j

Source: Internet
Author: User

One of the most recent Web projects to use is the log, but it's not very understandable to the usual logging tools (frameworks), Mark.

1.commons-logging.jar

  common-logging is a common log interface provided by Apache. The user is free to choose the third party's log component as a concrete implementation, like log4j, or the logging that comes with the JDK, common-logging will automatically find the log library that is actually used when the program runs by dynamic lookup mechanism . Of course, there is a simple logger implementation of common-logging inside, but the function is very weak. So using common-logging is usually used in conjunction with log4j. The advantage of using it is that the code dependency is common-logging rather than log4j, avoids direct coupling to the specific log scheme, and, if necessary, can change the third-party library of the log implementation.

How to use:

// Introducing JAR Packages <dependency> <groupId>commons-logging</groupId> < in the Pom.xml file    artifactid>commons-logging</artifactid>    <version>1.2</version></ Dependency>//call import Org.apache.commons.logging.Log in Java;  Import org.apache.commons.logging.LogFactory;      Public class A {      privatestatic Log logger = logfactory.getlog (this. GetClass ());  }  

Commons-logging, after all, just an external interface needs to specify its implementation class

#commons- logging.properties File configuration information, introducing the corresponding Jar # Org.apache.commons.logging.Log=org.apache.commons.logging.impl.simplelog# must be one of ("Trace","Debug","Info","warn","Error", or"Fatal"). #利用log4j为输出介质Org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JCategoryLog #JDK5 Logger#org. Apache.commons.logging.Log=org.apache.commons.logging.impl.jdk14logger

You also need to configure the Log4j.properties file to place it under the/SRC directory.

The author confirms that it is not necessary to configure <context-param> to load in the Web. xml file when using Commons-logging and log4j together. There is no need to configure the Commons-logging.properties

  1  <!--log--> 2  < ;d ependency> 3  <groupid>commons-logging</groupid> 4  <artifactid>commons-logging</artifactid> 5          <version>1.2  </version> 6  </dependency> <dependency> 8 <groupid>org.apache.logging.log4j</groupid> 9  <artifactid>log4j-core</artifactid>10  <version>2.5  </version>11  </dependency> 

  

  

2.log4j

An Apache open source project can be seen as an implementation of commons-logging. By using log4j, we can control the destination of log information delivery is console, file, GUI component, even socket server, NT Event recorder, UNIX Syslog daemon, etc. the user can also control the output format of each log, and by defining the level of each log information, the user can control the log generation process more carefully. These can be configured flexibly with a single configuration file without the need to modify the program code.

Compare detailed log4j.properties configuration files http://jiangzhengjun.iteye.com/blog/526364

Download Full profile:Http://pan.baidu.com/s/1qXHOULY

3.slf4j

slf4j is all called simple Logging facade for Java,java simply log façade. Similar to Apache Common-logging, is a façade package for different log frameworks that can be accessed with a log implementation without modifying any configuration at deployment time. However, he statically binds the real log library at compile time. When using SLF4J, if you need to use one of the log implementations, then you must select the correct set of SLF4J jar packages (various bridging packages).

  

So when using slf4j, we need to introduce the above package.

        <dependency>            <groupId>org.apache.logging.log4j</groupId>            <artifactId> log4j-core</artifactid>            <version>2.5</version>        </dependency>        < dependency>            <groupId>org.slf4j</groupId>            <artifactId>slf4j-api</artifactId>            <version>1.7. </version>        </dependency>        <dependency>            <groupid>org.slf4j </groupId>            <artifactId>slf4j-log4j12</artifactId>            <version>1.7.  </version>        </dependency>

Add log Listener. Log4jconfiglistener must be before spring's listener.

<!--set the log4j configuration file location loaded by sprng--><context-param>     <param-name>log4jconfiglocation</ param-name>     <param-value>WEB-INF/classes/log4j.properties</param-value> </context-param >   <!--Spring refreshes the interval for log4j configuration file changes in milliseconds--><context-param>     <param-name> log4jrefreshinterval</param-name>     <param-value>10000</param-value> </ context-param>     <listener>        <listener-class> org.springframework.web.util.log4jconfiglistener</listener-class>    </listener>
import Org.slf4j.logger;import org.slf4j.LoggerFactory; Private Static Logger Logger = Loggerfactory.getlogger (employeecontroller.  Class);

Comparison between 4.SLF4J and common-logging common-loggingThe dynamic lookup mechanism automatically finds the log library that is actually used when the program runs. Because it uses ClassLoader to find and load the underlying log library, a framework like OSGi does not work properly because OSGi's different plugins use their own classloader. This mechanism of OSGI ensures that plug-ins are independent of each other, but it makes Apache common-logging unable to work.
slf4jThe real log library is statically bound at compile time, so it can be used in OSGi. In addition, SLF4J supports parameterized log strings, avoiding the need to write the IF (logger.isdebugenable ()) that was previously written to reduce the performance loss of string concatenation, and now you can write directly: Logger.debug ("Current user is: {} ", user). The assembly message was postponed until it was able to determine whether to display the message, but the cost of acquiring the parameter was not spared.

Reference: http://zachary-guo.iteye.com/blog/361177

http://blog.csdn.net/woshixuye/article/details/20858239

The relationship between commons-logging \ log4j \ slf4j

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.