Eclipse中结合Maven的Log日志配置

Log日志配置

  • 方法一:视频学习

按照操作在maven项目的pom.xml中添加如下log4j依赖

<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency> 

且在同级工程目录中创建log4j.properties文件,设置如下格式

### 设置 ###
log4j.rootLogger = debug,stdout,D,E
 
### 输出信息到控制台 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
 
### 输出DEBUG级别以上的日志 ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = /home/sheehan/Software/eclipse-workspace/second-maven/logs/log.log
log4j.appender.D.Append = true
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
 
### 输出ERROR级别以上的日志 ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File = /home/sheehan/Software/eclipse-workspace/second-maven/logs/error.log 
log4j.appender.E.Append = true
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

然后在程序中写入

public class TestClass {
	private Logger log = Logger.getLogger(TestClass.class); // 注意与方法二中声明的区别
	@Test
	public void method(){
		log.info("message");
		log.warn("!!!!!!!");
	}
}

添加如下依赖

 <!--log4j-->
 <dependency>
     <groupId>commons-logging</groupId>
     <artifactId>commons-logging-api</artifactId>
     <version>1.1</version>
 </dependency>

 <dependency>
     <groupId>org.slf4j</groupId>
     <artifactId>slf4j-api</artifactId>
     <version>1.7.26</version>
 </dependency>

 <dependency>
     <groupId>org.slf4j</groupId>
     <artifactId>slf4j-log4j12</artifactId>
     <version>1.7.26</version>
 </dependency>

然后在程序中写入

public class TestClass {
	private org.slf4j.Logger log = LoggerFactory.getLogger(Log4jTest.class); //注意与方法一中声明的区别
	@Test
	public void method(){
		log.info("message");
		log.warn("!!!!!!!");
	}
}
@Test
public void testError() {
    String s = null;
    try {
        s.length();
    } catch (Exception e) {
        log.error(s, e);
        //当然如果你懒得想提示信息的话,直接这样log.error("", e);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值