When springboot introduces spring-boot-starter-web, the logback log package is imported by default, and no additional introduction is required in the project.
Log levels from low to high are divided into:
TRACE
<DEBUG
<INFO
<WARN
<ERROR
<FATAL
。
Add logback-spring.xml in the resource directory
content:
<?xml version="1.0" encoding="UTF-8"?> <configuration scan="true" scanPeriod="60 seconds" debug="false"> <contextName>logback</contextName> <!--输出到控制台--> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d %contextName [%thread] %-5level %logger - %msg%n</pattern> </encoder> </appender> 30Output to file Generate a log file every day, save<!-- 天的日志文件。--> <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_PATH}/demo-%d.log</fileNamePattern> <maxHistory>30</maxHistory> </rollingPolicy> <encoder> <pattern>%d %contextName [%thread] %-5level %logger - %msg%n</pattern> </encoder> </appender> <root level="info"> <appender-ref ref ="console" /> <appender-ref ref ="file" /> </root> <!-- Test environment + development environment . Use commas to separate multiple ones . --> <springProfile name = "test,dev" > <logger name ="com.example.springboot" level ="info" /> </springProfile> <!-- Define the log output level of the production environment --> <springProfile name ="pro" > < logger name ="com.example.springboot" level="info"/> </springProfile> <!--输出执行的sql语句--> <logger name="com.example.springboot.dao" level="debug"/> </configuration>
说明:该文件定义了一个控制台输出 一个文件输出(最长保存日志为30天)。指定了不同环境下日志输出级别。最后一句输出执行的输出SQL信息 。
${LOG_PATH} 该变量会获取 logging.path的值
二、定义日志文件输出位置
application.yaml 文件中添加如下内容:
logging: path: e:\\logs\\springboot\\logs
三、定义测试Controller
LogController
package com.example.springboot.controller; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/log") public class LogController { private final Logger log = LoggerFactory.getLogger(this.getClass()); @RequestMapping("/test") public void log() { log.debug("debug"); log.info("info"); log.warn("warn"); log.error("error"); } }
可以修改日志配置文件日志级别, 进行测试。就不再具体展示.
详细内容参考:Spring Boot 日志配置