springboot default log using logback, how Benpian use the default log records, how to use other logs
(A) a default configuration logs application.properties
(1) The default log level is info, log only the default print console
Posted pom.xml, without adding additional dependent
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.edu.spring</groupId> <artifactId>springboot_web</artifactId> <version>1.0.0</version> <name>springboot_web</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies> </project>
UserDao.java, test class
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.edu.spring.springboot.dao; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @Component public class UserDao { private Logger logger=LoggerFactory.getLogger(UserDao.class); public void log(){ logger.debug("UserDao log debug"); logger.info("UserDao log info"); logger.warn("UserDao log warn"); logger.error("UserDao log error"); } }
App.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.edu.spring.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication public class App { public static void main(String[] args) throws Exception{ ConfigurableApplicationContext context=SpringApplication.run(App.class, args); System.out.println("-----------UserDao.class------------"); context.getBean(UserDao.class).log(); context.close(); } }
Operating results can be seen: the default log level is info, log only the default print console
(2) change the log level, add the following properties in the application.properties, the log level of the modified packet com.edu.spring.springboot.dao debug
logging.level.com.edu.spring.springboot.dao=debug
Results are as follows:
(3) modify the log format: logging.pattern.console, modify the output format of the log and the content output time
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} %m%n
Results are as follows:
Note: logging.level.root = info indicates the level of logs in all packages is info
logging.level.root = off means closed in all packets log
A packet in each level can be defined individually, the individual is not defined according to the default level
(3) the configuration log file path, logging.file or logging.path; configuration log format, logging.pattern.file
logging.file=d:/springboottemp/logs/mylog.log
logging.pattern.file=%d{yyyy-MM-dd} %t %-4r %p %F[%L]: %m%n
Results are as follows:
Note: The console will still output log
If the configuration logging.path = d: / springboottemp / logs default file name is generated spring.log
(二)通过logback.xml或者logback-spring.xml配置默认日志
在resources中新建logback.xml 或者 logback-spring.xml,去掉application.properties中所有配置
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
<layout>
<pattern>%date{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</layout>
</appender>
<root level="debug">
<appender-ref ref="consoleLog"/>
</root>
</configuration>
运行结果如下:
注意:大小以10M分割日志
(三)如何使用其他日志,log4j2为例
首先排除掉默认的logback,然后引入log4j2的依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency>
其次在resources中新建log4j2.xml 去掉application.properties中所有配置
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="UTF-8" ?> <configuration> <appenders> <Console name="CONSOLE" target="system_out" follow="true"> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} |-%-5level[%thread] %c [%L] -| %msg%n" /> </Console> </appenders> <loggers> <root level="debug"> <appenderref ref="CONSOLE" /> </root> </loggers> </configuration>
运行结果如下: