1. Add log4j and slf4j jar packages
2. Web. xml:
[HTML]View Plaincopyprint?
- <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>
- <!--spring LOG4J configuration Listener--
- <listener>
- <listener-class>org.springframework.web.util.log4jconfiglistener</ Listener-class>
- </listener>
3. Src/log4j.properties:
[Plain]View Plaincopyprint?
- # # # set log levels # # #
- Log4j.rootlogger = Debug,stdout,d,e
- # # # Output to console # # #
- Log4j.appender.stdout = Org.apache.log4j.ConsoleAppender
- Log4j.appender.stdout.Target = System.out
- Log4j.appender.stdout.Threshold = INFO
- Log4j.appender.stdout.layout = Org.apache.log4j.PatternLayout
- Log4j.appender.stdout.layout.ConversionPattern =%d{absolute}%5p%c{1}:%l-%m%n
- Log4j.appender.stdout.encoding=utf-8
- # # # Output to log file # # #
- LOG4J.APPENDER.D = Org.apache.log4j.DailyRollingFileAppender
- Log4j.appender.d.file = ${ssh}web-inf/logs/debug.log
- log4j.appender.d.file.maxfilesize=500kb
- Log4j.appender.d.append = True
- # #输出DEBUG级别以上的日志
- Log4j.appender.d.threshold = DEBUG
- Log4j.appender.d.layout = Org.apache.log4j.PatternLayout
- Log4j.appender.d.layout.conversionpattern =%-d{yyyy-mm-dd HH:mm:ss} [%t:%r]-[%p]%m%n
- Log4j.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=500kb
- Log4j.appender.e.append = True
- # # Only output logs above the error level!!!
- Log4j.appender.e.threshold = ERROR
- Log4j.appender.e.layout = Org.apache.log4j.PatternLayout
- Log4j.appender.e.layout.conversionpattern =%-d{yyyy-mm-dd HH:mm:ss} [%t:%r]-[%p]%m%n
- Log4j.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
[Java]View Plaincopyprint?
- Import Org.slf4j.Logger;
- Import Org.slf4j.LoggerFactory;
- Import com.stone.action.DefaultAction;
- Public class Logutil {
- //Use SLF4J to create log objects, when the benefits are changed later to other log tools, just modify the configuration file 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 ());
- public 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 [go]