1. pom.xml
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.2</version> </dependency>
2. 在src/main/resources根目录下建一个log4j.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN" "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d %-5p %c.%M:%L - %m%n" /> </layout> </appender> <appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender"> <param name="file" value="${loggingRoot}/${server.name}/logs/${server.name}.log" /> <!-- Keep one backup file --> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d %-5p %c.%M:%L - %m%n" /> </layout> </appender> <!-- specify the logging level for loggers from other libraries --> <logger name="com.opensymphony"> <level value="${loggingLevel}" /> </logger> <logger name="org.apache.struts2"> <level value="${loggingLevel}" /> </logger> <logger name="org.springframework"> <level value="${loggingLevel}" /> </logger> <logger name="org.apache.ibatis"> <level value="${loggingLevel}" /> </logger> <logger name="com.opensymphony.module.sitemesh"> <level value="${loggingLevel}" /> </logger> <logger name="org.quartz"> <level value="${loggingLevel}"/> </logger> <logger name="net.spy.memcached"> <level value="${loggingLevel}"/> </logger> <logger name="org.dbunit"> <level value="INFO"/> </logger> <!-- for mybatis --> <logger name="com.vlt"> <level value="DEBUG"/> </logger> <logger name="org.apache.ibatis"> <level value="DEBUG"/> </logger> <!-- for all other loggers log only info and above log messages --> <root> <priority value="${loggingLevel}" /> <appender-ref ref="FILE"/> <appender-ref ref="STDOUT"/> </root> </log4j:configuration>
3. 在filter-dev.properties中指定log级别
## log configuration
loggingRoot=.
loggingLevel=WARN
级别顺序(低到高): DEBUG < INFO < WARN < ERROR < FATAL