application.properties(当前用的是生产环境)
#spring.profiles.active=dev
spring.profiles.active=prod
application-dev.properties
server.port=8089
logging.file=application-dev.log
#logging.level.zn.controller= debug
#logging.level.root=WARN
application-prod.properties
server.port=8090
logging.file=application-prod.log
#logging.level.zn.controller= WARN
logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<!-- 测试环境 -->
<springProfile name="dev">
<logger name="zn" level="INFO" additivity="false">
<!-- 控制台打印-->
<appender-ref ref="CONSOLE"/>
<!-- 存储日式文件 -->
<appender-ref ref="FILE"/>
</logger>
</springProfile>
<!-- 生产环境 -->
<springProfile name="prod">
<!-- 可以指定那个包下的日志级别 -->
<logger name="zn.controller" level="WARN" additivity="false">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</logger>
<!-- 默认的级别,除去上面配置的 -->
<root level="ERROR"></root>
</springProfile>
</configuration>
TestController.java
package zn.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@RequestMapping("log")
public Object writeLog()
{
logger.debug("This is a debug message");
logger.info("This is an info message");
logger.warn("This is a warn message");
logger.error("This is an error message");
return "OK";
}
}
TestController1.java
package zn.test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController1 {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@RequestMapping("log1")
public Object writeLog()
{
logger.debug("This is a debug message");
logger.info("This is an info message");
logger.warn("This is a warn message");
logger.error("This is an error message");
return "OK";
}
}
启动项目一次访问http://localhost:8090/log,http://localhost:8090/log1
日志文件:
项目目录结构: