SpringBoot entry (B): the log and custom properties

This chapter said configuration log springboot, custom attributes configuration and read, yml profile sub-environments (such as local environment, test environment and production environment, etc.). In favor of the actual development, more practical, some basic created here is not the nonsense of the previous chapter.

1. springboot logging configuration

  In the actual development of our project, the log is indispensable. Using only the log line problems can be quickly identified and located the problem and solve online! Of course, also be used to log the usual local debugging, you can quickly locate code with debug up with a better Europe.

  Is used inside the springboot Logback to the frame, and output to the console INFO level ( log four highest to lowest priority level are ERROR, WARN, INFO, the DEBUG )

  1) add a log-dependent (in fact not)

1 <dependency>
2     <groupId>org.springframework.boot</groupId>
3     <artifactId>spring-boot-starter-logging</artifactId>
4     <version>2.1.6.RELEASE</version>
5     <scope>compile</scope>
6 </dependency>    

    but! Here we can find the spring-boot-starter-web dependent in pom.xml, the points go find spring-boot-starter dependence. On the right, is it, inside it has introduced a logger, so we do not have to manually add logger. Do not believe? Click to see

 

  

  2) log

    a) First, create a config package, create a class in LogConfig package for all other classes inherit and print logs.

1 public class LogConfig {
2     
3     protected static final Logger logger = LoggerFactory.getLogger(LogConfig.class);
4 }

    b) controller then creates a package, creating a class in LogController package, where the test print log. Here we are four levels of logging output, watch the console.

 1 @RestController
 2 public class LogController extends LogConfig {
 3     
 4     @RequestMapping("/log")
 5     public String showLog() {
 6         logger.debug("debug:Process in LogController.showLog method");
 7         logger.info("info:Process in LogController.showLog method");
 8         logger.warn("warn:Process in LogController.showLog method");
 9         logger.error("error:Process in LogController.showLog method");
10         return "springboot log";
11     }
12 }

  c) 启动之后,在地址栏输入localhost:8080/log回车,查看控制台,发现只输出了info、warn、error三条日志,并没有debug。其实细心的朋友已经发现了原因,因为我们发现springboot启动时打印的日志都是info级别的,这是因为默认是info级别的,debug优先级低于info,当然不显示了。

  

  3)日志的配置

    我们在线上就会发现它们的日志其实都是保存下来的,那么如何保存日志文件呢,就要打开配置文件yml做相关配置了。

    a) file可以对日志文件命名。也可以定义创建的位置,file也可以加相对路径,所以不用配path了,我的log文件这样配在了logger-resource项目下的log文件夹中命名为springboot-demo.log。重新启动一下程序,就会飞发现log文件已经生成了。

    b) level可以指定路径配置log级别,但它是指定某个包下的级别。这里我们指定了com.wlb下的所有都为debug级别,这样再去看控制台就会发现,原来的debug级别日志也输出了。

1 # 日志配置
2 logging:
3   path:           #日志路径
4   file: spring-boot-logger-resource\log\springboot-demo.log #日志名称,这里用相对路径创建。可以不用path直接用file创建任何地方 
5   level:
6     # 指定路径配置log级别
7     com:
8       wlb: debug

 

2. springboot的配置文件分环境

  在项目中一定是分环境的,不像我们自己写的小demo。一般分为本地环境、开发环境、测试环境、生产环境,这里我们举例两种只模拟本地环境、测试环境。

  1)首先在yml相同位置复制粘贴出两个,分别叫application-dev.yml 和 application-test.yml分别模拟本地环境和测试环境。如图:

  

  2)在 application-dev.yml中配置当前文件名为dev,代表本地环境

1 spring:
2   # 当前文件
3   profiles: dev

    在application-test.yml中配置当前文件名为test,代表测试环境

1 spring:
2   # 当前文件
3   profiles: test

    最后在application.yml中配置当前选定的配置文件名为dev,即代表当前项目使用dev文件中的所有配置。当在测试环境时只需要将dev改成test即可。

1 spring:
2   profiles:
3     # 当前选定的配置文件
4     active: dev

    就是这么简单就配置成功了,那么如何验证我们配置文件分环境是否生效呢,让我们结合下一个例子创建自定义属性一起使用。

 

3. springboot的自定义属性配置

  在实际项目中我们有一些类似于常量的值,但又不想写入代码中,这样不便于区分以及后期维护,那么一般都会写在专门的配置文件中,便于后期修改维护。比如mysql配置用户名密码、静态资源路径、以及一些业务需求的配置,他们跟代码的关系不大,所以一般不和开发代码放在一起。接下来教你怎么在springboot中写自定义属性的配置。

  1)首先创建entity包,在entity包下创建一个实体类ResourceEntity做自定义属性配置。这个类要用@Component注解被spring管理起来,在需要自定义的属性上加上@Value注解,这里我们用com.resource + 属性名做唯一区分。定义了资源url地址、资源端口、最大个数、最小个数四个属性做测试。

 1 @Component
 2 public class ResourceEntity {
 3     
 4     @Value("${com.resource.resourceUrl}")
 5     private String resourceUrl;
 6     
 7     @Value("${com.resource.resourcePort}")
 8     private String resourcePort;
 9     
10     @Value("${com.resource.maxNum}")
11     private Integer maxNum;
12     
13     @Value("${com.resource.minNum}")
14     private Integer minNum;
15 
16     public String getResourceUrl() {
17         return resourceUrl;
18     }
19 
20     public void setResourceUrl(String resourceUrl) {
21         this.resourceUrl = resourceUrl;
22     }
23 
24     public String getResourcePort() {
25         return resourcePort;
26     }
27 
28     public void setResourcePort(String resourcePort) {
29         this.resourcePort = resourcePort;
30     }
31 
32     public Integer getMaxNum() {
33         return maxNum;
34     }
35 
36     public void setMaxNum(Integer maxNum) {
37         this.maxNum = maxNum;
38     }
39 
40     public Integer getMinNum() {
41         return minNum;
42     }
43 
44     public void setMinNum(Integer minNum) {
45         this.minNum = minNum;
46     }
47 }

  2)然后在application-dev.yml文件中配置具体的自定义属性值。这是本地环境配置文件中的。

1 # 自定义配置
2 com:
3   resource:
4     resourceUrl: 192.168.0.1
5     resourcePort: 8888
6     maxNum: 100
7     minNum: 0

  3)再在application-test.yml文件中配置测试环境的属性值。这是测试环境配置文件中的。

1 # 自定义配置
2 com:
3   resource:
4     resourceUrl: 192.168.0.2
5     resourcePort: 8889
6     maxNum: 50
7     minNum: 1

  4)创建ResourceController,用于页面展示我们配置的属性值。

 1 @RestController
 2 public class ResourceController {
 3     
 4     @Autowired
 5     private ResourceEntity resourceEntity;
 6     
 7     @RequestMapping("/resource")
 8     public ResourceEntity getResource() {
 9         return resourceEntity;
10     }
11 }

  5)启动项目,在地址栏输入localhost:8080/resource并回车,这时看到页面打印的json数据,正是我们在本地环境配置的属性值。证实了自定义属性的使用!

  6)在application.yml文件中修改dev为test(将本地环境配置切换为了测试环境配置)接着启动项目,在地址栏输入localhost:8080/resource并回车,这时看到页面打印的json数据,是我们在测试环境配置的属性值。证实了配置文件分环境的使用!

就是这么简单,一个springboot 日志及自定义属性项目完成!最后附上github地址,下载源码。

Github地址

Guess you like

Origin www.cnblogs.com/wlb509727985/p/11278091.html