jar包依赖:
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
log4j.xml配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="console" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="[%d{yyyy-MM-dd HH:mm:ss}][%-5p]%F:%L - %m%n"/> </layout> </appender> <appender name="file-info" class="org.apache.log4j.RollingFileAppender"> <param name="File" value="/home/www/logs/demoProject/demoProject-info.log" /> <param name="MaxFileSize" value="512KB" /> <param name="MaxBackupIndex" value="3" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d %p [%c] - %m%n"/> </layout> <filter class="org.apache.log4j.varia.LevelRangeFilter"> <param name="LevelMin" value="trace"/> <param name="LevelMax" value="info"/> </filter> </appender> <appender name="file-error" class="org.apache.log4j.RollingFileAppender"> <param name="File" value="/home/www/logs/demoProject/demoProject-error.log" /> <param name="MaxFileSize" value="512KB" /> <param name="MaxBackupIndex" value="3" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d %p [%c] - %m%n"/> </layout> <filter class="org.apache.log4j.varia.LevelRangeFilter"> <param name="LevelMin" value="WARN"/> <param name="LevelMax" value="ERROR"/> </filter> </appender> <root> <priority value="info"/> <appender-ref ref="console"/> <appender-ref ref="file-info"/> <appender-ref ref="file-error"/> </root> </log4j:configuration>
测试类:
import org.apache.log4j.Logger; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * Created by yangjiachang on 2016/7/6. */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="classpath:spring-context.xml") public class JUnitDemoTest extends AbstractJUnit4SpringContextTests { private static final Logger logger = Logger.getLogger(JUnitDemoTest.class); @Test public void test1(){ logger.trace("trace"); logger.debug("debug"); logger.info("info"); logger.warn("warn"); logger.error("error"); System.out.println("JUnit测试方法1"); } }执行后控制台输出:
[2016-07-19 15:16:01][INFO ]JUnitDemoTest.java:22 - info
[2016-07-19 15:16:01][WARN ]JUnitDemoTest.java:23 - warn
[2016-07-19 15:16:01][ERROR]JUnitDemoTest.java:24 - error
JUnit测试方法1
输出日志文件:
D:\home\www\logs\demoProject\demoProject-error.log
2016-07-19 15:16:01,030 WARN [JUnitDemoTest] - warn
2016-07-19 15:16:01,030 ERROR [JUnitDemoTest] - error
D:\home\www\logs\demoProject\demoProject-info.log
2016-07-19 15:16:00,665 INFO [org.springframework.test.context.TestContextManager] - Could not instantiate TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [javax/servlet/ServletContext]
2016-07-19 15:16:00,826 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from class path resource [spring-context.xml]
2016-07-19 15:16:00,932 INFO [org.springframework.context.support.GenericApplicationContext] - Refreshing org.springframework.context.support.GenericApplicationContext@15761df8: startup date [Tue Jul 19 15:16:00 CST 2016]; root of context hierarchy
2016-07-19 15:16:00,996 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@37271612: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
2016-07-19 15:16:01,030 INFO [JUnitDemoTest] - info
2016-07-19 15:16:01,037 INFO [org.springframework.context.support.GenericApplicationContext] - Closing org.springframework.context.support.GenericApplicationContext@15761df8: startup date [Tue Jul 19 15:16:00 CST 2016]; root of context hierarchy
2016-07-19 15:16:01,037 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@37271612: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
处理通过log4j.xml配置以外,还可以通过log4j.properties来完成