SpringBoot(3) ------>整合log-back与Swagger
2021-03-06 23:30
标签:单位 contex rac doc 代码 tap profile build except 整合log-back与Swagger,soscw.com" target="_blank">SpringBoot(3) ------>整合log-back与Swagger 标签:单位 contex rac doc 代码 tap profile build except 原文地址:https://www.cnblogs.com/donleo123/p/14286948.html一、整合log-back
在resources下新建logback-spring.xml
xml version="1.0" encoding="UTF-8"?>
configuration scan="true" scanPeriod="10 seconds">
contextName>logbackcontextName>
property name="log.path" value="E:/liangd/logs/springboot-dept-logs" />
conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
filter class="ch.qos.logback.classic.filter.ThresholdFilter">
level>infolevel>
filter>
encoder>
Pattern>${CONSOLE_LOG_PATTERN}Pattern>
charset>UTF-8charset>
encoder>
appender>
appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
file>${log.path}/log_debug.logfile>
encoder>
pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
charset>UTF-8charset>
encoder>
rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
fileNamePattern>${log.path}/debug/log-debug-%d{yyyy-MM-dd}.%i.logfileNamePattern>
timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
maxFileSize>100MBmaxFileSize>
timeBasedFileNamingAndTriggeringPolicy>
maxHistory>15maxHistory>
rollingPolicy>
filter class="ch.qos.logback.classic.filter.LevelFilter">
level>debuglevel>
onMatch>ACCEPTonMatch>
onMismatch>DENYonMismatch>
filter>
appender>
appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
file>${log.path}/log_info.logfile>
encoder>
pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
charset>UTF-8charset>
encoder>
rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
fileNamePattern>${log.path}/info/log-info-%d{yyyy-MM-dd}.%i.logfileNamePattern>
timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
maxFileSize>100MBmaxFileSize>
timeBasedFileNamingAndTriggeringPolicy>
maxHistory>15maxHistory>
rollingPolicy>
filter class="ch.qos.logback.classic.filter.LevelFilter">
level>infolevel>
onMatch>ACCEPTonMatch>
onMismatch>DENYonMismatch>
filter>
appender>
appender name="WARN_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
file>${log.path}/log_warn.logfile>
encoder>
pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
charset>UTF-8charset>
encoder>
rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
fileNamePattern>${log.path}/warn/log-warn-%d{yyyy-MM-dd}.%i.logfileNamePattern>
timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
maxFileSize>100MBmaxFileSize>
timeBasedFileNamingAndTriggeringPolicy>
maxHistory>15maxHistory>
rollingPolicy>
filter class="ch.qos.logback.classic.filter.LevelFilter">
level>warnlevel>
onMatch>ACCEPTonMatch>
onMismatch>DENYonMismatch>
filter>
appender>
appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
file>${log.path}/log_error.logfile>
encoder>
pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
charset>UTF-8charset>
encoder>
rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
fileNamePattern>${log.path}/error/log-error-%d{yyyy-MM-dd}.%i.logfileNamePattern>
timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
maxFileSize>100MBmaxFileSize>
timeBasedFileNamingAndTriggeringPolicy>
maxHistory>15maxHistory>
rollingPolicy>
filter class="ch.qos.logback.classic.filter.LevelFilter">
level>ERRORlevel>
onMatch>ACCEPTonMatch>
onMismatch>DENYonMismatch>
filter>
appender>
springProfile name="dev">
logger name="com.donleo.mybatis" level="debug"/>
springProfile>
root level="info">
appender-ref ref="CONSOLE" />
appender-ref ref="DEBUG_FILE" />
appender-ref ref="INFO_FILE" />
appender-ref ref="WARN_FILE" />
appender-ref ref="ERROR_FILE" />
root>
configuration>
二、整合Swagger
1、向pom文件中添加依赖
dependency>
groupId>io.springfoxgroupId>
artifactId>springfox-swagger2artifactId>
version>2.7.0version>
dependency>
dependency>
groupId>io.springfoxgroupId>
artifactId>springfox-swagger-uiartifactId>
version>2.7.0version>
dependency>
2、swagger配置
package com.donleo.mybatis.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @author liangd
* date 2020-12-03 18:01
* code swagger配置
* 访问接口地址:http://localhost:端口号/swagger-ui.html
*/
@Configuration
@EnableSwagger2
public class Swagger2Config {
/**
* 1、@Configuration Spring底层注解,标识配置文件
* 2、@Bean 装进IOC容器中
*/
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//为当前包下controller生成API文档
.apis(RequestHandlerSelectors.basePackage("com.donleo.demo.controller"))
//为有@Api注解的Controller生成API文档
// .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
//为有@ApiOperation注解的方法生成API文档
// .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("人力资源管理系统")
.description("部门、用户等接口")
.contact("liangd")
.version("1.0")
.build();
}
}
3、model层
package com.donleo.mybatis.model;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* @author liangd
*/
@ApiModel(value = "部门",description = "部门实体")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Dept implements Serializable {
@ApiModelProperty(value = "id" ,example = "1")
private Integer id;
@ApiModelProperty(value = "deptName",example = "技术部")
private String deptName;
@ApiModelProperty(value = "deptCode",example = "donleo10001")
private String deptCode;
}
4、controller层
package com.donleo.mybatis.controller;
import com.donleo.mybatis.common.CommonResult;
import com.donleo.mybatis.model.Dept;
import com.donleo.mybatis.service.IDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
* @author liangd
* date 2020-12-03 11:08
* code
*/
@Api(tags = "部门管理")
@RestController
@RequestMapping("/dept")
public class DeptController {
@Resource
private IDeptService deptService;
@ApiOperation("添加部门")
@PostMapping("/add")
public CommonResult add(@RequestBody Dept dept){
Integer id = deptService.add(dept);
return CommonResult.success(id);
}
@ApiOperation("删除部门")
@DeleteMapping("/delete")
public CommonResult delete(Integer id){
Integer count = deptService.delete(id);
return CommonResult.success(count);
}
@ApiOperation("修改部门")
@PutMapping("/update")
public CommonResult update(@RequestBody Dept dept){
Integer count = deptService.update(dept);
return CommonResult.success(count);
}
@ApiOperation("根据主键Id查询单个1 --?")
@GetMapping("/findById1")
public CommonResult findById1(@RequestParam Integer id){
Dept dept = deptService.findById(id);
return CommonResult.success(dept);
}
@ApiOperation("根据主键Id查询单个2 --/")
@GetMapping("/findById2/{id}")
public CommonResult findById2(@PathVariable("id") Integer id){
Dept dept = deptService.findById(id);
return CommonResult.success(dept);
}
@ApiOperation("部门列表")
@GetMapping("/findAll")
public CommonResult findAll(){
return deptService.findAll();
}
}
5、访问swagger页面地址:http://localhost:端口号/swagger-ui.html
文章标题:SpringBoot(3) ------>整合log-back与Swagger
文章链接:http://soscw.com/index.php/essay/61083.html