您的位置:首页 > 编程语言 > Java开发

spring-boot的日志管理(logback)之生产测试环境的切换

2017-04-10 20:23 786 查看
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





日志文件:





项目目录结构:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐