SpringBoot入门(二):日志及自定义属性
- 2019 年 10 月 3 日
- 筆記
??????springboot???????????????????????yml??????????????????????????????????????????????????????????
1. springboot?????
?????????????????????????????????????????????????????????????????????????????????debug????????
???springboot??????Logback????,??INFO????????????????????????? ERROR?WARN?INFO?DEBUG?
??1?????????????
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>
??????????????pom.xml??? spring-boot-starter-web ???????? spring-boot-starter ???????????????????logger???????????logger????????
?
??
??2??????
????a) ??????config?????????LogConfig?????????????????
1 public class LogConfig { 2 3 protected static final Logger logger = LoggerFactory.getLogger(LogConfig.class); 4 }
????b) ??????controller?????????LogController??????????????????????????????????
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-resourcelogspringboot-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????????