1. Add log4j and slf4j jar packages
2. Web. xml:
<context-param><!--log4j Configuration address--><param-name>log4jconfiglocation</param-name><!--< Param-value>/web-inf/classes/log4j.properties</param-value>--><param-value>classpath: log4j.properties</param-value></context-param><context-param><!--log4j Configuration Scan refresh interval can be used without--- <param-name>log4jrefreshinterval</param-name><param-value>10000</param-value></ context-param><!--The log4j configuration listener in spring--><listener><listener-class> Org.springframework.web.util.log4jconfiglistener</listener-class></listener>
3. Src/log4j.properties:
# # # set log Levels # # #log4j. Rootlogger = debug,stdout,d,e### Output to console # # #log4j. appender.stdout = Org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target = System.outlog4j.appender.stdout.Threshold = INFOlog4j.appender.stdout.layout = Org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern = %d{absolute}%5p%c{1}:%l-%m%nlog4j.appender.stdout.encoding=utf-8### output to log file # # #log4j. APPENDER.D = Org.apache.log4j.dailyrollingfileappenderlog4j.appender.d.file = ${ssh}web-inf/logs/ Debug.loglog4j.appender.d.file.maxfilesize=500kblog4j.appender.d.append = true## Output debug level above the log log4j.appender.d.threshold = Debuglog4j.appender.d.layout = Org.apache.log4j.patternlayoutlog4j.appender.d.layout.conversionpattern =%-d{yyyy-mm-dd HH:mm:ss} [%t:%r]-[%p]%m %nlog4j.appender.d.encoding=utf-8### Save exception information to a separate file # # #log4j. APPENDER.E = org.apache.log4j.dailyrollingfileappender## Exception Log file name Log4j.appender.e.file = ${ssh}web-inf/logs/error.log log4j.appender.e.file.maxfilesize=500kblog4j.aPpender. E.append = true## Only output logs above the error level!!! Log4j.appender.e.threshold = Errorlog4j.appender.e.layout = Org.apache.log4j.patternlayoutlog4j.appender.e.layout.conversionpattern =%-d{yyyy-mm-dd HH:mm:ss} [%t:%r]-[%p]%m %nlog4j.appender.e.encoding=utf-8
Configure the Info-level output to the console, and debug and error are output to the corresponding file.
4. Using the Log Object
Import Org.slf4j.logger;import Org.slf4j.loggerfactory;import Com.stone.action.defaultaction;public class LogUtil {/ /use SLF4J to create a log object, when the benefit is replaced later as the other log tool, as long as the configuration file is modified without modifying the code private static Logger Logger = Loggerfactory.getlogger (logutil.class );//Use log4j to create a log object//private Org.apache.log4j.Logger Logger = Org.apache.log4j.Logger.getLogger (GetClass ());p ublic static void error (String message) {logger.error (message);} public static void Debug (String message) {logger.debug (message);} public static void info (String message) {logger.info (message);//org.apache.log4j.logger.getlogger (Logutil.class). info (message);}}
Java EE SSH Framework Integration (iv) Log processing spring combines log4j, slf4j