Spring Boot 日志管理、项目打包、多环境配置

1、日志管理

Spring Boot 默认日志管理为 Slf4j,这里仅介绍 Slf4j 的简单使用。

1.1 设置日志级别

# 设置jopo包日志级别为warn
logging.level.com.bjsxt.springbootlogback.jopo=warn

# 屏蔽controller包日志
logging.level.com.bjsxt.springbootlogback.controller=off

1.2 设置日志文件

# 指定日志输出路径
logging.file.name=d:/log/applog.log

1.3 使用日志记录

// 添加脚注
@Slf4j
public class User {
}

// 记录信息
log.debug("debug message");
log.info("info message");
log.warn("warn message");
log.error("error message");

// 两种用法
log.info("this is a " + "book");
log.info("this is a {}", "clock");

2、项目打包

2.1 打包

image.png

2.2 拷贝文件

image.png

2.3 运行命令

# java -jar 项目名称
java -jar springbootlogback-0.0.1-SNAPSHOT.jar

3、多环境配置

语法结构:application-{profile}.properties/yml
profile:表明某个配置环境的标识java

示例:
application-dev.properties/yml 开发环境
application-test.properties/yml 测试环境
application-prod.properties/yml 生产环境spring

3.1 建立{profiles}.properties

image.png

# application.properties 配置
msg = Develop Environment

# application-dev.properties 配置
msg = Develop Environment

# application-test.properties 配置
msg = Test Environment

# application-prod.properties 配置
msg = Production Environment
@RestController
public class PageController {

    @Value("${msg}")
    private String msg;

    @RequestMapping("/")
    public String index() {
        return msg;
    }
}

3.2 Windows 环境下启动方式

java -jar xxx.jar --spring.profiles.active={profile}springboot

# 默认方式(dev)
java -jar xxx.jar

# 开发环境
java -jar xxx.jar --spring.profiles.active=dev

# 测试环境
java -jar xxx.jar --spring.profiles.active=test

# 生产环境
java -jar xxx.jar --spring.profiles.active=prod
相关文章
相关标签/搜索