Category:java
Article From:https://www.cnblogs.com/pegasus827/p/9124778.html
  • brief introduction

commons-loggingIt is a member of the Apache commons class library. The Apache commons class library is a generic class library, providing basic functions, such as commons-fileupload, commons-httpclient, comMons-io, commons-codes and so on.
commons-loggingYou can choose to use Log4j or JDK Logging, but he doesn’t rely on Log4j, API of JDK Logging. If the classpath of the project contains the class library of log4j, log4j will be used, otherwise JDK L will be used.Ogging. With commons-logging, you can choose to use those logging methods flexibly and do not need to modify the source code.

 

  • An example of using commons-logging

commons-loggingThe use is similar to that of Log4j, and their level and usage rules are exactly the same. Here comes a demo:
First, we need to add commons-logging’s Maven dependency to the project.

[html] view plain copy

 
  1. <dependency>  
  2.             <groupId>commons-logging</groupId>  
  3.             <artifactId>commons-logging</artifactId>  
  4.             <version>1.2</version>  
  5.         </dependency>  

The test code is as follows:

[java] view plain copy

 
  1. package org.linkinpark.commons.commonslogging;  
  2.   
  3.   
  4. import org.apache.commons.logging.Log;  
  5. import org.apache.commons.logging.LogFactory;  
  6. import org.junit.Test;  
  7.   
  8.   
  9. /** 
  10.  * @Create an author: LinkinPark
  11.  * @Creation time: February 26, 2016
  12.  * @Functional description: the test class of commons-logging
  13.  */  
  14. public class CommonsLoggingTest  
  15. {  
  16.     public static Log LOG = LogFactory.getLog(CommonsLoggingTest.class);  
  17.   
  18.   
  19.     @Test  
  20.     public void test()  
  21.     {  
  22.         LOG.debug(“debug()…”);  
  23.         LOG.info(“info()…”);  
  24.         LOG.error(“error()…”);  
  25.     }  
  26.   
  27.   
  28. }  

1,Now we do not add log4j dependency in the project to see the effect.

Run the above test, the JUnit green bar, and then the console output is as follows:

 

[plain] view plain copy

 
  1. February, 26, 2016, 10:34:23, morning, org.linkinpark.commons.commonslogging.CommonsLoggingTest, test,
  2. Information: info ()…
  3. February, 26, 2016, 10:34:23, morning, org.linkinpark.commons.commonslogging.CommonsLoggingTest, test,
  4. Serious: error ()…

As I said before, JDK’s Logging is actually a chicken ribs. There is no debug log level.

 

2,Now we add log4j dependency in the project to see the effect.

As long as we add log4j’s jar package to the project, commons-logging will automatically cut to log4j’s log output. So now we do not provide log4j.properties configuration files, so the console output is as follows:

 

 

[plain] view plain copy

 
  1. log4j:WARN No appenders could be found for logger (org.linkinpark.commons.commonslogging.CommonsLoggingTest).  
  2. log4j:WARN Please initialize the log4j system properly.  
  3. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.  

Now we add the log4j.properties configuration file:

 

[html] view plain copy

 
  1. log4j.rootLogger=DEBUG,console  
  2.   
  3. # The following is the configuration of rootLogger, the subclass inherits by default, but the subclass rewrite the following configuration =rootLogger+ itself configuration.
  4. #Output to the console
  5. log4j.appender.console=org.apache.log4j.ConsoleAppender    
  6. #Set output style
  7. log4j.appender.console.layout=org.apache.log4j.PatternLayout   
  8. #Log output information format
  9. log4j.appender.console.layout.ConversionPattern=[%-d{yyyy-MM-dd HH:mm:ss}]-[%t-%5p]-[%C-%M(%L)]: %m%n   

Run the test again, JUnit green bar, and then console the normal output log:

 

[plain] view plain copy

 
  1. [2016-02-26 10:47:13]-[main-DEBUG]-[org.linkinpark.commons.commonslogging.CommonsLoggingTest-test(19)]: debug()…  
  2.  [2016-02-26 10:47:13]-[main- INFO]-[org.linkinpark.commons.commonslogging.CommonsLoggingTest-test(20)]: info()…  
  3.  [2016-02-26 10:47:13]-[main-ERROR]-[org.linkinpark.commons.commonslogging.CommonsLoggingTest-test(21)]: error()…  
  4.    

 

3,Display configuration commons-logging to enable log4j

By default, common-logging automatically checks whether log4j is used or log4j enabled by configuration files. The configuration file is commons-logging.properties, placed under the classpath of the program.You can.
For example:

[html] view plain copy

 
  1. org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4J-Logger  
  2. org.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.LogFactoryImpl  

With regard to this configuration above, we know OK, comparison convention is better than configuration, commons-logging has supported automatic scanning, and we don’t need to artificially add these boring configuration files.

 

 

 

  • summary

Strictly speaking, commons-logging is not a log control, there is no log function, it only unifies the API of JDK Logging and Log4j, and gives the logging function to JDK Loggings or log4j. For undetermined daysCommons-logging is a good choice. Spring, Hibernate, Struts and so on are all commons-logging. Next, we’ll look at Commons-loggingThe source code, to further organize the next Commons-logging.

 

If there is Log4j, commons-logging will leave the output intact to log4j. Without log4j, commons-logging will transform the corresponding output into the output of JDK Logging.

Link of this Article: The use of commons-logging

Leave a Reply

Your email address will not be published. Required fields are marked *