log4j打印彩色日志和指定位置输出日志

Grep Console插件

SpringTest3.java

package cn.itcast.spring.AOP;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

//springjunit集成测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class SpringTest3 {
          
   
    //注入要测试bean
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private ProductService productService;

    //测试
    @Test
    public void test() {
          
   
        //基于接口
        customerService.save();
        customerService.find();
        //基于类的
        productService.save();
        productService.find();
    }

    private static Logger LOGGER = Logger.getLogger(SpringTest3.class);
    @Test
    public void printLog(){
          
   
        LOGGER.info("this is info log");
        LOGGER.debug("this is debug log");
        LOGGER.warn("this is warn log");
        LOGGER.error("this is error log");
        LOGGER.fatal("this is fatal log");
    }
}

指定位置输出日志

log4j.properties

log4j.rootLogger=INFO,stdout,file
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout.ConversionPattern=%p	%d{
          
   ISO8601}	%r	%c	[%t]	%m%n


log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.layout=org.apache.log4j.PatternLayout

#两种写法都可以
#log4j.appender.file.File=C:\Users\U100926\Desktop\my.log
log4j.appender.file.File=C:/Users/U100926/Desktop/my1.log

log4j.appender.file.layout.ConversionPattern=%p	%d{
          
   ISO8601}	%r	%c	[%t]	%m%n
经验分享 程序员 微信小程序 职场和发展