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

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



日志文件:




项目目录结构:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值