springBoot⽣成⽇志⽂件⼀、安装lombok
说明:
安omlok后model可以不⽤写get、set⽅法,slf4j⽇志直接使⽤log打印1. Maven Repository中下载lombok.jar
2. 将lombok.jar放到myeclipse安装⽬录
3.在myeclipse安装⽬录下打开dos窗⼝,并运⾏java -jar lombok.jar,选择将eclipse安装⽬录填⼊
4. 点击 Install / Update 按钮,成功之后重启eclipse
5. l⽂件添加bomlok依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
log4j2 console <version>1.16.20</version>
<scope>provided</scope>
</dependency>
Lombok⽇志打印
1.例⼦
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j//使⽤lombok
public class LoggerTest {
private final Logger logger = Logger(LoggerTest.class);
public void test1(){
logger.debug("..debug..");
logger.info("..info..");
("..error..");
}
/**
* 使⽤了lombok之后直接⽤于log对⽇志进⾏打印
*/
@Test
public void test2(){
String name = "tom";
String id = "123456";
log.debug("..debug..");
//使⽤{} 占位符进⾏⽇志动态打印
log.info("name:{},id:{}",name,id+" ..info..");
("..error..");
}
2.查看⽇志级别
Log4j+Logback⽇志⽂件输出
1. l配置⽇志⽂件输出
logging:
pattern: #配置⽇志格式 %d:⽇期 , %msg:⽇志信息,%n换⾏
console: "%d - %msg%n" #设置控制台打印格式
path: E:/work2/SpringBoot_log/folder #设置log⽂件路径默认名称:spring.log
file: E:/work2/SpringBoot_log/folder/cell.log #设置log⽂件路径设置⽇志⽂件名称cell.log level:
com.springboot.service.LoggerTest: error #指定输出某个类的⽇志级别
l配置⽇志⽂件输出
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- 控制台⽇志打印格式 -->
<appender name="consoleLog" class="ch.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>
%d - %msg%n
</pattern>
</layout>
</appender>
<!-- 配置⽇志⽂件输出 info级别-->
<appender name="fileInfoLog" class="ch.olling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter">
<!-- 根据LevelFilter 查看FilterReply枚举类型,还有⼀种NEUTRAL(中⽴) -->
<!-- NEUTRAL:忽略并继续往下判断,ACCEPT:接受,不往判断⾛ -->
<level>ERROR</level><!-- 禁⽌ -->
<onMatch>DENY</onMatch><!-- 接受 -->
<onMismatch>ACCEPT</onMismatch>
</filter>
<encoder>
<pattern>
</pattern>
</encoder>
<!--滚动策略-->
<rollingPolicy class="ch.olling.TimeBasedRollingPolicy">
<!--路径-->
<fileNamePattern>E:/work2/SpringBoot_log/folder/info_%d.log</fileNamePattern>
</rollingPolicy>
</appender>
<!-- 配置⽇志⽂件输出 error级别-->
<appender name="fileErrorLog" class="ch.olling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<encoder>
<pattern>
%msg%n
</pattern>
</encoder>
<!--滚动策略-->
<rollingPolicy class="ch.olling.TimeBasedRollingPolicy">
<!--路径-->
<fileNamePattern>E:/work2/SpringBoot_log/folder/error_%d.log</fileNamePattern>
</rollingPolicy>
</appender>
<root level="info"><!-- ⽇志作⽤域:root(整个项⽬) -->
<appender-ref ref="consoleLog" />
<appender-ref ref="fileInfoLog" />
<appender-ref ref="fileErrorLog" />
</root>
</configuration>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论