优化 日志输出和错误打印

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 lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.fusesource.jansi.Ansi;
import java.util.Arrays;
@RestControllerAdvice @RestControllerAdvice
@Slf4j @Slf4j
@ -11,8 +13,11 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
public class GlobalExceptionHandler { public class GlobalExceptionHandler {
// 全局异常拦截 // 全局异常拦截
@ExceptionHandler @ExceptionHandler
public SaResult handlerException(Exception e) { public void handlerException(Exception e) {
log.error(e.getMessage()); System.err.println(Ansi.ansi().fgRed().a("==========ERROR==========START==========").reset());
return SaResult.error(e.getMessage()); 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 文件夹包含了 domain 文件夹包含了
entity持久化对象 映射数据库数据表的字段 entity持久化对象 映射数据库数据表的字段
dtoData Transfer Object数据传输对象 接收前端参数的对象 dtoData Transfer Object数据传输对象 接收前端参数的对象
voValue Object值对象 响应值对象 voValue Object值对象 响应值对象

View File

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