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