SpringBoot学习 - 日志

Posted Ivyvivid

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot学习 - 日志相关的知识,希望对你有一定的参考价值。

 1 package com.study.spring_boot_log;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.boot.autoconfigure.websocket.WebSocketAutoConfiguration;
 6 import org.springframework.context.ConfigurableApplicationContext;
 7 
 8 import com.study.spring_boot_log.dao.UserDao;
 9 import com.study.spring_boot_log.service.UserService;
36 @SpringBootApplication(exclude=WebSocketAutoConfiguration.class)
37 public class App {
38     public static void main(String[] args) {
39         ConfigurableApplicationContext context = SpringApplication.run(App.class,args);
40         context.getBean(UserDao.class).log();
41         System.out.println("===================");
42         context.getBean(UserService.class).log();
43         
44         context.close();
45     }
46 }

 pom.xml:

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 3     <modelVersion>4.0.0</modelVersion>
 4 
 5     <groupId>com.study.springboot</groupId>
 6     <artifactId>spring-boot-log</artifactId>
 7     <version>1.0.0</version>
 8     <packaging>jar</packaging>
 9 
10     <name>spring-boot-log</name>
11     <url>http://maven.apache.org</url>
12 
13     <properties>
14         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15     </properties>
16 
17     <dependencyManagement>
18         <dependencies>
19             <dependency>
20                 <groupId>org.springframework.boot</groupId>
21                 <artifactId>spring-boot-dependencies</artifactId>
22                 <version>1.5.3.RELEASE</version>
23                 <scope>import</scope>
24                 <type>pom</type>
25             </dependency>
26         </dependencies>
27     </dependencyManagement>
28 
29     <dependencies>
30         <dependency>
31             <groupId>org.springframework.boot</groupId>
32             <artifactId>spring-boot-starter</artifactId>
33         </dependency>        
34     </dependencies>
35 </project>

 

 1 package com.study.spring_boot_log.dao;
 2 
 3 import org.slf4j.Logger;
 4 import org.slf4j.LoggerFactory;
 5 import org.springframework.stereotype.Component;
 6 
 7 @Component
 8 public class UserDao {
 9     private Logger log = LoggerFactory.getLogger(UserDao.class);
10     public void log() {
11         log.debug("user dao debug log");
12         log.info("user dao info log");
13         log.warn("user dao warn log");
14         log.error("user dao error log");
15     }
16 }

 

 1 package com.study.spring_boot_log.service;
 2 
 3 import org.slf4j.Logger;
 4 import org.slf4j.LoggerFactory;
 5 import org.springframework.stereotype.Component;
 6 
 7 @Component
 8 public class UserService {
 9     private Logger log = LoggerFactory.getLogger(UserService.class);
10     public void log() {
11         log.debug("user service debug log");
12         log.info("user service info log");
13         log.warn("user service warn log");
14         log.error("user service error log");
15     }
16 }

 

springboot默认 的日志级别是info
可以通过logging.level.*=debug配置项设置,*可以是包,也可以是某个类。

也可以在Run Configurations中配置 --debug,或者SpringApplication.run(App.class,“--debug=true”)。这两种debug只会是springboot默认,自定义的类不会debug。


日志级别有:TRACE,DEBUG,INFO,WARN,ERROR,FATA,OFF
日志级别配置成OFF,表示关闭日志输出


logging.file 指定日志文件路径名字
logging.path 指定日志目录(此时的日志名字为spring.log)
日志文件输出,文件的大小10M之后,就会分割

logging.pattern.console 配置控制台输出日志的pattern
logging.file.console 配置日志文件输出日志的pattern

application.properties

1 logging.level.com.study.spring_boot_log.dao.UserDao=off
2 logging.level.com.study.spring_boot_log.service.UserService=DEBUG
3 
4 logging.file=D:/ivy/mylog
5 logging.path=D:/ivy/mylogs
6 
7 logging.pattern.console=%-20(%d{yyyy-MM-dd} [%thread]) %-5level %logger{80} - %msg%n
8 logging.file.console=%-20(%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread]) %-5level %logger{80} - %msg%n

 

springboot 默认支持logback
也就是说,只需要在classpath下放一个logback.xml,logback-spring.xml的文件,即可定制日志的输出

logback-spring.xml或logback.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>  
 2 <configuration>  
 3   <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">  
 4     <layout class="ch.qos.logback.classic.PatternLayout">  
 5       <pattern>%-20(%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread]) %-5level %logger{80} - %msg%n</pattern>  
 6     </layout>  
 7   </appender>  
 8   
 9   
10   <root level="debug">  
11     <appender-ref ref="consoleLog" />  
12   </root>  
13 </configuration>

 

使用其他的日志组件的步骤:
1.排除默认的日志组件:spring-boot-starter-logging
2.加入新的日志路径依赖
3.把相应的配置文件放在classpath下

log4j2.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>  
 2 <configuration>
 3     <appenders>  
 4         <Console name="console" target="SYSTEM_OUT" follow="true">  
 5           <PatternLayout pattern="%d{yyyy-MM-dd} [%thread] %-5level %logger{80} - %msg%n"/>  
 6         </Console> 
 7    </appenders>
 8    <loggers>  
 9     <root level="DEBUG">  
10       <appender-ref ref="console"/> 
11     </root>  
12   </loggers> 
13 </configuration>

 














以上是关于SpringBoot学习 - 日志的主要内容,如果未能解决你的问题,请参考以下文章

argparse 代码片段只打印部分日志

常用python日期日志获取内容循环的代码片段

mybatis学习日志二

Springboot学习~4:日志

SpringBoot学习

springboot学习日志-- thymeleaf学习