优化 日志输出和错误打印

This commit is contained in:
萌狼蓝天 2024-09-23 21:26:40 +08:00
parent 7dfb83cff9
commit 12887cc169
4 changed files with 50 additions and 12 deletions

View File

@ -4,6 +4,8 @@ import cn.dev33.satoken.util.SaResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.fusesource.jansi.Ansi;
import java.util.Arrays;
@RestControllerAdvice
@Slf4j
@ -11,8 +13,11 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
public class GlobalExceptionHandler {
// 全局异常拦截
@ExceptionHandler
public SaResult handlerException(Exception e) {
log.error(e.getMessage());
return SaResult.error(e.getMessage());
public void handlerException(Exception e) {
System.err.println(Ansi.ansi().fgRed().a("==========ERROR==========START==========").reset());
System.err.println(Ansi.ansi().fgRed().a("错误消息:").a(e.getMessage()).reset());
System.err.println(Ansi.ansi().fgRed().a("错误类型:").a(e.getLocalizedMessage()).reset());
System.err.println(Ansi.ansi().fgRed().a(e.getCause() == null ? "" : e.getCause().toString()).reset());
System.err.println(Ansi.ansi().fgRed().a("==========ERROR========== END ==========").reset());
}
}

View File

@ -0,0 +1,4 @@
比如xxxController里面的@RequestMapping就写@RequestMapping("/xxx")

View File

@ -1,4 +1,7 @@
domain 文件夹包含了
entity持久化对象 映射数据库数据表的字段
dtoData Transfer Object数据传输对象 接收前端参数的对象
voValue Object值对象 响应值对象

View File

@ -1,27 +1,53 @@
<configuration>
<include resource="org/springframework/boot/logging/logback/initial.xml"/>
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<!-- 控制台输出 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level 【 %logger{36} 】 %msg%n</pattern>
<pattern>%d{HH:mm:ss} %-5level %logger{36} - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
<!-- 使用 filter 控制 throwable 输出 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>DENY</onMatch>
<onMismatch>NEUTRAL</onMismatch>
</filter>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMatch>DENY</onMatch>
</filter>
</appender>
<!-- 文件输出 -->
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>logs/app.log</file>
<file>logs/application.log</file>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
<!-- 使用 filter 控制 throwable 输出 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>DENY</onMatch>
<onMismatch>NEUTRAL</onMismatch>
</filter>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMatch>DENY</onMatch>
</filter>
</appender>
<!-- 设置全局日志级别为 INFO -->
<root level="info">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
</root>
<!-- 更细粒度的日志控制 -->
<logger name="com.guaiguailang.harmony" level="DEBUG" additivity="false">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
</logger>
<!-- 如果有特定包的日志级别需求,可以在这里添加 -->
<logger name="com.guaiguailang.harmony" level="debug"/>
</configuration>