日志框架的使用
1、先进行一个简单的测试;
@RunWith(SpringRunner.class)
@SpringBootTest
public class LoggerTest
{
private final Logger logger = LoggerFactory.getLogger(LoggerTest.class);
@Test
public void test1() {
String name = “snail”;
String password = “123456”;
logger.debug(“debug…..”);
logger.info(“info……”);
logger.error(“error…..”);
}
}
运行输出:
2018-07-12 19:34:50,963 - Started LoggerTest in 9.757 seconds (JVM running for 10.57)
2018-07-12 19:34:51,009 - info......
2018-07-12 19:34:51,009 - error.....
2018-07-12 19:34:51,009 - Closing org.springframework.web.context.support.GenericWebApplicationContext@36804139:
startup date [Thu Jul 12 19:34:41 CST 2018]; root of context hierarchy
2018-07-12 19:34:51,009 - Closing JPA EntityManagerFactory for persistence unit 'default'
由上面可知,打印了info、error。为什么没有打印debug呢?因为日志默认基本是基本是info,在其之下的级别就没有输出。
注意首先引入日志类:Logger,其中LoggerTest.class必须为当前类。但是每次都这样写有点麻烦,所有可是使用注解: @Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class LoggerTest
{
@Test
public void test1() {
String name = "snail";
String password = "123456";
log.debug("debug.....");
log.info("info......");
log.error("error.....");
}
}
2、日志中输入变量:
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class LoggerTest
{
@Test
public void test1() {
String name = "snail";
String password = "123456";
log.debug("debug.....");
log.info("info......");
log.error("error.....");
log.info("name:"+name+"password:"+password);
log.info("name: {} ,password: {}",name,password);
}
}
3、基于application.yml文件的配置(进行简单的配置)
logging:
pattern:
#配置日志输出格式--日期 - 信息,换行
console: "%d - %msg%n"
例如:
2018-07-12 19:49:23,440 - ClassTemplateLoader for Spring macros added to FreeMarker configuration
2018-07-12 19:49:23,652 - Started LoggerTest in 9.106 seconds (JVM running for 9.882)
2018-07-12 19:49:23,695 - info......
2018-07-12 19:49:23,695 - error.....
2018-07-12 19:49:23,695 - name:snailpassword:123456
2018-07-12 19:49:23,695 - name: snail ,password: 123456
2018-07-12 19:49:23,698 - Closing org.springframework.web.context.support.GenericWebApplicationContext@2235eaab: startup date [Thu Jul 12 19:49:15 CST 2018]; root of context hierarchy
2018-07-12 19:49:23,702 - Closing JPA EntityManagerFactory for persistence unit 'default'
logging:
pattern:
#配置日志输出格式--日期 - 信息,换行
console: "%d - %msg%n"
#日志输出的路径
path: /var/log/tomcat
logging:
pattern:
#配置日志输出格式--日期 - 信息,换行
console: "%d - %msg%n"
#指定名字和路径
file: /var/log/tomcat/sell.log
logging:
pattern:
#配置日志输出格式--日期 - 信息,换行
console: "%d - %msg%n"
#指定名字和路径
file: /var/log/tomcat/sell.log
#指定日志级别
level: debug
logging:
pattern:
#配置日志输出格式--日期 - 信息,换行
console: "%d - %msg%n"
#指定名字和路径
file: /var/log/tomcat/sell.log
#指定具体类日志级别
level:
com.snail.sell.service.LoggerTest: debug
4、基于logback-spring.xml的配置(可以进行较为复杂的配置):例如
①区分info和error日志;
②每天产生一个日志;
Ⅰ、在resources目录下新建一个配置文件:logback-spring.xml
Ⅱ、配置内容如下:
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<!--控制台输出-->
<appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
<!--日志展示-->
<layout class="ch.qos.logback.classic.PatternLayout">
<!--具体的展示格式-->
<pattern>
%d - %msg%n
</pattern>
</layout>
</appender>
<!--文件日志配置-->
<appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>DENY</onMatch>
<onMismatch>ACCEPT</onMismatch>
</filter>
<encoder>
<pattern>
%msg%n
</pattern>
</encoder>
<!--滚动策略-->
<!--按时间滚动,每天一个日志-->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--路径-->
<fileNamePattern>/var/log/tomcat/sell/info.%d.log</fileNamePattern>
</rollingPolicy>
</appender>
<!--错误日志-->
<appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--根据范围过滤日志,只输出error级别的日志-->
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<encoder>
<pattern>
%msg%n
</pattern>
</encoder>
<!--滚动策略-->
<!--按时间滚动,每天一个日志-->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--路径-->
<fileNamePattern>/var/log/tomcat/sell/error.%d.log</fileNamePattern>
</rollingPolicy>
</appender>
<!--使用配置-->
<root level="info">
<appender-ref ref="consoleLog" />
<appender-ref ref="fileInfoLog" />
<appender-ref ref="fileErrorLog" />
</root>
</configuration>