SSH Framework Integrated log processing spring combines log4j, slf4j

Source: Internet
Author: User
Tags log4j

1. Add log4j and slf4j jar packages

2. Web. xml:

1<context-param>2<!--log4j Configuration address--3<param-name>log4jConfigLocation</param-name>4<!--<param-value>/WEB-INF/classes/log4j.properties</param-value>--5<param-value>classpath:log4j.properties</param-value>6</context-param>7<context-param>8<!--log4j configuration scan refresh interval can be used without---9<param-name>log4jRefreshInterval</param-name>Ten<param-value>10000</param-value> One</context-param> A<!--spring LOG4J configuration Listener-- -<listener> -<listener-class>org.springframework.web.util.log4jconfiglistener</listener-class> the</listener>

3. Src/log4j.properties:

1 ImportOrg.slf4j.Logger; 2 Importorg.slf4j.LoggerFactory; 3   4 Importcom.stone.action.DefaultAction; 5   6  Public classLogutil {7     //use SLF4J to create log objects, and when you change to other log tools later, you do not have to modify the code until you modify the configuration file8     Private StaticLogger Logger = Loggerfactory.getlogger (logutil.class); 9     //Create a Log object using log4jTen //private Org.apache.log4j.Logger Logger = Org.apache.log4j.Logger.getLogger (GetClass ());  One        A      Public Static voiderror (String message) { - logger.error (message);  -     }   the      Public Static voidDebug (String message) { - logger.debug (message);  -     }   -      Public Static voidinfo (String message) { + logger.info (message);  - //Org.apache.log4j.Logger.getLogger (logutil.class). info (message);  +     }   A}

  

Configure the Info-level output to the console, and debug and error are output to the corresponding file.

4. Using the Log Object

1 ImportOrg.slf4j.Logger; 2 Importorg.slf4j.LoggerFactory; 3   4 Importcom.stone.action.DefaultAction; 5   6  Public classLogutil {7     //use SLF4J to create log objects, and when you change to other log tools later, you do not have to modify the code until you modify the configuration file8     Private StaticLogger Logger = Loggerfactory.getlogger (logutil.class); 9     //Create a Log object using log4jTen //private Org.apache.log4j.Logger Logger = Org.apache.log4j.Logger.getLogger (GetClass ());  One        A      Public Static voiderror (String message) { - logger.error (message);  -     }   the      Public Static voidDebug (String message) { - logger.debug (message);  -     }   -      Public Static voidinfo (String message) { + logger.info (message);  - //Org.apache.log4j.Logger.getLogger (logutil.class). info (message);  +     }   A}

SSH Framework Integrated log processing spring combines 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.