mirror of
https://github.com/201206030/novel-cloud.git
synced 2025-08-24 17:42:42 +00:00
refactor: 基于 novel 项目 & Spring Cloud 2022 & Spring Cloud Alibaba 2022 重构
This commit is contained in:
37
novel-resource/pom.xml
Normal file
37
novel-resource/pom.xml
Normal file
@@ -0,0 +1,37 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>novel-cloud</artifactId>
|
||||
<groupId>io.github.xxyopen</groupId>
|
||||
<version>2.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>novel-resource</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.github.xxyopen</groupId>
|
||||
<artifactId>novel-config</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
@@ -0,0 +1,16 @@
|
||||
package io.github.xxyopen.novel.resource;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
@SpringBootApplication(scanBasePackages = {"io.github.xxyopen.novel"})
|
||||
@EnableDiscoveryClient
|
||||
public class NovelResourceApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(NovelResourceApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,32 @@
|
||||
package io.github.xxyopen.novel.resource.config;
|
||||
|
||||
import io.github.xxyopen.novel.common.constant.SystemConfigConsts;
|
||||
import io.github.xxyopen.novel.resource.interceptor.FileInterceptor;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
/**
|
||||
* Spring Web Mvc 相关配置不要加 @EnableWebMvc 注解,否则会导致 jackson 的全局配置失效。因为 @EnableWebMvc 注解会导致 WebMvcAutoConfiguration 自动配置失效
|
||||
*
|
||||
* @author xiongxiaoyang
|
||||
* @date 2022/5/18
|
||||
*/
|
||||
@Configuration
|
||||
@RequiredArgsConstructor
|
||||
public class WebConfig implements WebMvcConfigurer {
|
||||
|
||||
private final FileInterceptor fileInterceptor;
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
|
||||
// 文件访问拦截
|
||||
registry.addInterceptor(fileInterceptor)
|
||||
.addPathPatterns(SystemConfigConsts.IMAGE_UPLOAD_DIRECTORY + "**")
|
||||
.order(1);
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,49 @@
|
||||
package io.github.xxyopen.novel.resource.controller;
|
||||
|
||||
import io.github.xxyopen.novel.common.constant.ApiRouterConsts;
|
||||
import io.github.xxyopen.novel.common.resp.RestResp;
|
||||
import io.github.xxyopen.novel.resource.dto.resp.ImgVerifyCodeRespDto;
|
||||
import io.github.xxyopen.novel.resource.service.ResourceService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 前台门户-资源(图片/视频/文档)模块 API 控制器
|
||||
*
|
||||
* @author xiongxiaoyang
|
||||
* @date 2022/5/17
|
||||
*/
|
||||
@Tag(name = "ResourceController", description = "前台门户-资源模块")
|
||||
@RestController
|
||||
@RequestMapping(ApiRouterConsts.API_FRONT_RESOURCE_URL_PREFIX)
|
||||
@RequiredArgsConstructor
|
||||
public class ResourceController {
|
||||
|
||||
private final ResourceService resourceService;
|
||||
|
||||
/**
|
||||
* 获取图片验证码接口
|
||||
*/
|
||||
@Operation(summary = "获取图片验证码接口")
|
||||
@GetMapping("img_verify_code")
|
||||
public RestResp<ImgVerifyCodeRespDto> getImgVerifyCode() throws IOException {
|
||||
return resourceService.getImgVerifyCode();
|
||||
}
|
||||
|
||||
/**
|
||||
* 图片上传接口
|
||||
*/
|
||||
@Operation(summary = "图片上传接口")
|
||||
@PostMapping("/image")
|
||||
RestResp<String> uploadImage(
|
||||
@Parameter(description = "上传文件") @RequestParam("file") MultipartFile file) {
|
||||
return resourceService.uploadImage(file);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,28 @@
|
||||
package io.github.xxyopen.novel.resource.dto.resp;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 图像验证码 响应DTO
|
||||
* @author xiongxiaoyang
|
||||
* @date 2022/5/18
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
public class ImgVerifyCodeRespDto {
|
||||
|
||||
/**
|
||||
* 当前会话ID,用于标识改图形验证码属于哪个会话
|
||||
* */
|
||||
@Schema(description = "sessionId")
|
||||
private String sessionId;
|
||||
|
||||
/**
|
||||
* Base64 编码的验证码图片
|
||||
* */
|
||||
@Schema(description = "Base64 编码的验证码图片")
|
||||
private String img;
|
||||
|
||||
}
|
@@ -0,0 +1,45 @@
|
||||
package io.github.xxyopen.novel.resource.interceptor;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
* 文件 拦截器
|
||||
*
|
||||
* @author xiongxiaoyang
|
||||
* @date 2022/5/22
|
||||
*/
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class FileInterceptor implements HandlerInterceptor {
|
||||
|
||||
@Value("${novel.file.upload.path}")
|
||||
private String fileUploadPath;
|
||||
|
||||
@SuppressWarnings("NullableProblems")
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
|
||||
Object handler) throws Exception {
|
||||
// 获取请求的 URI
|
||||
String requestUri = request.getRequestURI();
|
||||
// 缓存10天
|
||||
response.setDateHeader("expires", System.currentTimeMillis() + 60 * 60 * 24 * 10 * 1000);
|
||||
try (OutputStream out = response.getOutputStream(); InputStream input = new FileInputStream(
|
||||
fileUploadPath + requestUri)) {
|
||||
byte[] b = new byte[4096];
|
||||
for (int n; (n = input.read(b)) != -1; ) {
|
||||
out.write(b, 0, n);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,38 @@
|
||||
package io.github.xxyopen.novel.resource.manager.redis;
|
||||
|
||||
import io.github.xxyopen.novel.common.constant.CacheConsts;
|
||||
import io.github.xxyopen.novel.resource.util.ImgVerifyCodeUtils;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.Duration;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 验证码 管理类
|
||||
*
|
||||
* @author xiongxiaoyang
|
||||
* @date 2022/5/12
|
||||
*/
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class VerifyCodeManager {
|
||||
|
||||
private final StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
/**
|
||||
* 生成图形验证码,并放入 Redis 中
|
||||
*/
|
||||
public String genImgVerifyCode(String sessionId) throws IOException {
|
||||
String verifyCode = ImgVerifyCodeUtils.getRandomVerifyCode(4);
|
||||
String img = ImgVerifyCodeUtils.genVerifyCodeImg(verifyCode);
|
||||
stringRedisTemplate.opsForValue().set(CacheConsts.IMG_VERIFY_CODE_CACHE_KEY + sessionId,
|
||||
verifyCode, Duration.ofMinutes(5));
|
||||
return img;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,31 @@
|
||||
package io.github.xxyopen.novel.resource.service;
|
||||
|
||||
import io.github.xxyopen.novel.common.resp.RestResp;
|
||||
import io.github.xxyopen.novel.resource.dto.resp.ImgVerifyCodeRespDto;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 资源(图片/视频/文档)相关服务类
|
||||
*
|
||||
* @author xiongxiaoyang
|
||||
* @date 2022/5/17
|
||||
*/
|
||||
public interface ResourceService {
|
||||
|
||||
/**
|
||||
* 获取图片验证码
|
||||
*
|
||||
* @throws IOException 验证码图片生成失败
|
||||
* @return Base64编码的图片
|
||||
*/
|
||||
RestResp<ImgVerifyCodeRespDto> getImgVerifyCode() throws IOException;
|
||||
|
||||
/**
|
||||
* 图片上传
|
||||
* @param file 需要上传的图片
|
||||
* @return 图片访问路径
|
||||
* */
|
||||
RestResp<String> uploadImage(MultipartFile file);
|
||||
}
|
@@ -0,0 +1,79 @@
|
||||
package io.github.xxyopen.novel.resource.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
||||
import io.github.xxyopen.novel.common.constant.ErrorCodeEnum;
|
||||
import io.github.xxyopen.novel.common.constant.SystemConfigConsts;
|
||||
import io.github.xxyopen.novel.common.resp.RestResp;
|
||||
import io.github.xxyopen.novel.config.exception.BusinessException;
|
||||
import io.github.xxyopen.novel.resource.dto.resp.ImgVerifyCodeRespDto;
|
||||
import io.github.xxyopen.novel.resource.manager.redis.VerifyCodeManager;
|
||||
import io.github.xxyopen.novel.resource.service.ResourceService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 资源(图片/视频/文档)相关服务实现类
|
||||
*
|
||||
* @author xiongxiaoyang
|
||||
* @date 2022/5/17
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class ResourceServiceImpl implements ResourceService {
|
||||
|
||||
private final VerifyCodeManager verifyCodeManager;
|
||||
|
||||
@Value("${novel.file.upload.path}")
|
||||
private String fileUploadPath;
|
||||
|
||||
@Override
|
||||
public RestResp<ImgVerifyCodeRespDto> getImgVerifyCode() throws IOException {
|
||||
String sessionId = IdWorker.get32UUID();
|
||||
return RestResp.ok(ImgVerifyCodeRespDto.builder()
|
||||
.sessionId(sessionId)
|
||||
.img(verifyCodeManager.genImgVerifyCode(sessionId))
|
||||
.build());
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public RestResp<String> uploadImage(MultipartFile file) {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
String savePath =
|
||||
SystemConfigConsts.IMAGE_UPLOAD_DIRECTORY
|
||||
+ now.format(DateTimeFormatter.ofPattern("yyyy")) + File.separator
|
||||
+ now.format(DateTimeFormatter.ofPattern("MM")) + File.separator
|
||||
+ now.format(DateTimeFormatter.ofPattern("dd"));
|
||||
String oriName = file.getOriginalFilename();
|
||||
assert oriName != null;
|
||||
String saveFileName = IdWorker.get32UUID() + oriName.substring(oriName.lastIndexOf("."));
|
||||
File saveFile = new File(fileUploadPath + savePath, saveFileName);
|
||||
if (!saveFile.getParentFile().exists()) {
|
||||
boolean isSuccess = saveFile.getParentFile().mkdirs();
|
||||
if (!isSuccess) {
|
||||
throw new BusinessException(ErrorCodeEnum.USER_UPLOAD_FILE_ERROR);
|
||||
}
|
||||
}
|
||||
file.transferTo(saveFile);
|
||||
if (Objects.isNull(ImageIO.read(saveFile))) {
|
||||
// 上传的文件不是图片
|
||||
Files.delete(saveFile.toPath());
|
||||
throw new BusinessException(ErrorCodeEnum.USER_UPLOAD_FILE_TYPE_NOT_MATCH);
|
||||
}
|
||||
return RestResp.ok(savePath + File.separator + saveFileName);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,112 @@
|
||||
package io.github.xxyopen.novel.resource.util;
|
||||
|
||||
import lombok.experimental.UtilityClass;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Base64;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* 图片验证码工具类
|
||||
*
|
||||
* @author xiongxiaoyang
|
||||
* @date 2022/5/17
|
||||
*/
|
||||
@UtilityClass
|
||||
public class ImgVerifyCodeUtils {
|
||||
|
||||
/**
|
||||
* 随机产生只有数字的字符串
|
||||
*/
|
||||
private final String randNumber = "0123456789";
|
||||
|
||||
/**
|
||||
* 图片宽
|
||||
*/
|
||||
private final int width = 100;
|
||||
|
||||
/**
|
||||
* 图片高
|
||||
*/
|
||||
private final int height = 38;
|
||||
|
||||
private final Random random = new Random();
|
||||
|
||||
/**
|
||||
* 获得字体
|
||||
*/
|
||||
private Font getFont() {
|
||||
return new Font("Fixed", Font.PLAIN, 23);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 生成校验码图片
|
||||
*/
|
||||
public String genVerifyCodeImg(String verifyCode) throws IOException {
|
||||
// BufferedImage类是具有缓冲区的Image类,Image类是用于描述图像信息的类
|
||||
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
|
||||
// 产生Image对象的Graphics对象,改对象可以在图像上进行各种绘制操作
|
||||
Graphics g = image.getGraphics();
|
||||
//图片大小
|
||||
g.fillRect(0, 0, width, height);
|
||||
//字体大小
|
||||
//字体颜色
|
||||
g.setColor(new Color(204, 204, 204));
|
||||
// 绘制干扰线
|
||||
// 干扰线数量
|
||||
int lineSize = 40;
|
||||
for (int i = 0; i <= lineSize; i++) {
|
||||
drawLine(g);
|
||||
}
|
||||
// 绘制随机字符
|
||||
drawString(g, verifyCode);
|
||||
g.dispose();
|
||||
//将图片转换成Base64字符串
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
ImageIO.write(image, "JPEG", stream);
|
||||
return Base64.getEncoder().encodeToString(stream.toByteArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* 绘制字符串
|
||||
*/
|
||||
private void drawString(Graphics g, String verifyCode) {
|
||||
for (int i = 1; i <= verifyCode.length(); i++) {
|
||||
g.setFont(getFont());
|
||||
g.setColor(new Color(random.nextInt(101), random.nextInt(111), random
|
||||
.nextInt(121)));
|
||||
g.translate(random.nextInt(3), random.nextInt(3));
|
||||
g.drawString(String.valueOf(verifyCode.charAt(i - 1)), 13 * i, 23);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 绘制干扰线
|
||||
*/
|
||||
private void drawLine(Graphics g) {
|
||||
int x = random.nextInt(width);
|
||||
int y = random.nextInt(height);
|
||||
int xl = random.nextInt(13);
|
||||
int yl = random.nextInt(15);
|
||||
g.drawLine(x, y, x + xl, y + yl);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取随机的校验码
|
||||
*/
|
||||
public String getRandomVerifyCode(int num) {
|
||||
int randNumberSize = randNumber.length();
|
||||
StringBuilder verifyCode = new StringBuilder();
|
||||
for (int i = 0; i < num; i++) {
|
||||
String rand = String.valueOf(randNumber.charAt(random.nextInt(randNumberSize)));
|
||||
verifyCode.append(rand);
|
||||
}
|
||||
return verifyCode.toString();
|
||||
}
|
||||
|
||||
}
|
26
novel-resource/src/main/resources/application.yml
Normal file
26
novel-resource/src/main/resources/application.yml
Normal file
@@ -0,0 +1,26 @@
|
||||
server:
|
||||
port: 9040
|
||||
spring:
|
||||
profiles:
|
||||
include: common
|
||||
active: dev
|
||||
servlet:
|
||||
multipart:
|
||||
# 上传文件最大大小
|
||||
max-file-size: 5MB
|
||||
|
||||
novel:
|
||||
file:
|
||||
# 文件上传配置
|
||||
upload:
|
||||
# 上传路径
|
||||
path: /Users/xiongxiaoyang/upload
|
||||
|
||||
management:
|
||||
# 端点启用配置
|
||||
endpoint:
|
||||
logfile:
|
||||
# 启用返回日志文件内容的端点
|
||||
enabled: true
|
||||
# 外部日志文件路径
|
||||
external-file: logs/novel-resource-service.log
|
6
novel-resource/src/main/resources/bootstrap.yml
Normal file
6
novel-resource/src/main/resources/bootstrap.yml
Normal file
@@ -0,0 +1,6 @@
|
||||
spring:
|
||||
application:
|
||||
name: novel-resource-service
|
||||
profiles:
|
||||
include: common
|
||||
|
79
novel-resource/src/main/resources/logback-spring.xml
Normal file
79
novel-resource/src/main/resources/logback-spring.xml
Normal file
@@ -0,0 +1,79 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<!-- 彩色日志依赖的渲染类 -->
|
||||
<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}}"/>
|
||||
|
||||
<!-- %m输出的信息,%p日志级别,%t线程名,%d日期,%c类的全名,%i索引【从数字0开始递增】,,, -->
|
||||
<!-- appender是configuration的子节点,是负责写日志的组件。 -->
|
||||
<!-- ConsoleAppender:把日志输出到控制台 -->
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
|
||||
<!-- 控制台也要使用UTF-8,不要使用GBK,否则会中文乱码 -->
|
||||
<charset>UTF-8</charset>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<!-- RollingFileAppender:滚动记录文件,先将日志记录到指定文件,当符合某个条件时,将日志记录到其他文件 -->
|
||||
<!-- 以下的大概意思是:1.先按日期存日志,日期变了,将前一天的日志文件名重命名为XXX%日期%索引,新的日志仍然是demo.log -->
|
||||
<!-- 2.如果日期没有发生变化,但是当前日志的文件大小超过1KB时,对当前日志进行分割 重命名 -->
|
||||
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
|
||||
<File>logs/novel-resource-service.log</File>
|
||||
<!-- rollingPolicy:当发生滚动时,决定 RollingFileAppender 的行为,涉及文件移动和重命名。 -->
|
||||
<!-- TimeBasedRollingPolicy: 最常用的滚动策略,它根据时间来制定滚动策略,既负责滚动也负责出发滚动 -->
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<!-- 活动文件的名字会根据fileNamePattern的值,每隔一段时间改变一次 -->
|
||||
<!-- 文件名:logs/demo.2017-12-05.0.log -->
|
||||
<fileNamePattern>logs/debug.%d.%i.log</fileNamePattern>
|
||||
<!-- 每产生一个日志文件,该日志文件的保存期限为30天 -->
|
||||
<maxHistory>30</maxHistory>
|
||||
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
|
||||
<!-- maxFileSize:这是活动文件的大小,默认值是10MB,测试时可改成1KB看效果 -->
|
||||
<maxFileSize>10MB</maxFileSize>
|
||||
</timeBasedFileNamingAndTriggeringPolicy>
|
||||
</rollingPolicy>
|
||||
<encoder>
|
||||
<!-- pattern节点,用来设置日志的输入格式 -->
|
||||
<pattern>
|
||||
%d %p (%file:%line\)- %m%n
|
||||
</pattern>
|
||||
<!-- 记录日志的编码:此处设置字符集 - -->
|
||||
<charset>UTF-8</charset>
|
||||
</encoder>
|
||||
</appender>
|
||||
<springProfile name="dev">
|
||||
<!-- ROOT 日志级别 -->
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT"/>
|
||||
<appender-ref ref="FILE"/>
|
||||
</root>
|
||||
<!-- 指定项目中某个包,当有日志操作行为时的日志记录级别 -->
|
||||
<!-- com.maijinjie.springboot 为根包,也就是只要是发生在这个根包下面的所有日志操作行为的权限都是DEBUG -->
|
||||
<!-- 级别依次为【从高到低】:FATAL > ERROR > WARN > INFO > DEBUG > TRACE -->
|
||||
<logger name="io.github.xxyopen" level="DEBUG" additivity="false">
|
||||
<appender-ref ref="STDOUT"/>
|
||||
<appender-ref ref="FILE"/>
|
||||
</logger>
|
||||
</springProfile>
|
||||
|
||||
<springProfile name="prod">
|
||||
<!-- ROOT 日志级别 -->
|
||||
<root level="INFO">
|
||||
<appender-ref ref="FILE"/>
|
||||
</root>
|
||||
<!-- 指定项目中某个包,当有日志操作行为时的日志记录级别 -->
|
||||
<!-- com.maijinjie.springboot 为根包,也就是只要是发生在这个根包下面的所有日志操作行为的权限都是DEBUG -->
|
||||
<!-- 级别依次为【从高到低】:FATAL > ERROR > WARN > INFO > DEBUG > TRACE -->
|
||||
<logger name="io.github.xxyopen" level="ERROR" additivity="false">
|
||||
<appender-ref ref="FILE"/>
|
||||
</logger>
|
||||
</springProfile>
|
||||
</configuration>
|
Reference in New Issue
Block a user