概述
全局异常处理和日志打点统计
全剧自定义异常
在开发过程中通常抛出异常,如果不进行全局打点,很不友好。
前端很难知道具体发送问题的原因。
public class CustomException extends RuntimeException{
//状态码
private int code;
//异常消息
private String msg;
public CustomException(int code,String msg){
super(msg);
this.code=code;
this.msg=msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
@ControllerAdvice
public class CustomExceptionHandler {
/**
* 出现异常会进入到相应到handler中。先看那种类型到异常
* @ResponseBody Json返回给前端
*
* @param e
* @return
*/
@ExceptionHandler(value = Exception.class)
@ResponseBody
public JsonData handler(Exception e){
if(e instanceof CustomException){
CustomException customException=(CustomException)e;
return JsonData.buildError(customException.getMsg(),customException.getCode());
}else {
return JsonData.buildError("全剧异常");
}
}
}
value:为处理到错误类型
这里如果为 value=CustomException 则不用进行下面到逻辑到判断,因为只能处理CustomException
1、自定义异常类
2、配置使用
logback打点日志
1、日志不仅用于排查问题,查看应用运行情况
2、更可以用于统计,虽然统计可以通过数据库进行统计,但是存在风险,如果用日志,并且建立一个日志系统用于分析
这样就方便产品和运营人员进行查看分析数据
3、写日志,可以加缓冲buffer,也可也进行异步
异步实时写 缓冲只有到达某一临界才刷(存在数据丢失)
参考资料:https://blog.csdn.net/zhuyucheng123/article/details/21524549
1、resouces下添加配置
logback-spring.xml
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<!--输出到日志 appender搬运工把日志存储到哪去-->
<appender name="consoleApp" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>
%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level[%thread]%logger{56}.%method:%L -%msg%n
</pattern>
</layout>
</appender>
<appender name="dataApp" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<encoder>
<pattern>
`%date{yyyy-MM-dd HH:mm:ss.SSS}`%msg`%n
</pattern>
</encoder>
<!-- 滚动策略 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 路径 -->
<fileNamePattern>app_log/log/app.data.%d.log</fileNamePattern>
<MaxHistory>1</MaxHistory>
</rollingPolicy>
</appender>
<appender name="fileInfoApp" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>DENY</onMatch>
<onMismatch>ACCEPT</onMismatch>
</filter>
<encoder>
<pattern>
%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level[%thread]%logger{56}.%method:%L -%msg%n
</pattern>
</encoder>
<!-- 滚动策略 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 路径 -->
<fileNamePattern>app_log/log/app.info.%d.log</fileNamePattern>
<MaxHistory>1</MaxHistory>
</rollingPolicy>
</appender>
<appender name="fileErrorApp" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<encoder>
<pattern>
%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level[%thread]%logger{56}.%method:%L -%msg%n
</pattern>
</encoder>
<!-- 设置滚动策略 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 路径 -->
<fileNamePattern>app_log/log/app.err.%d.log</fileNamePattern>
<!-- 控制保留的归档文件的最大数量,超出数量就删除旧文件,假设设置每个月滚动,
且<maxHistory> 是1,则只保存最近1个月的文件,删除之前的旧文件 -->
<MaxHistory>1</MaxHistory>
</rollingPolicy>
</appender>
<!--用于统计数据-->
<logger name="dataLogger">
<level value="INFO" />
<appender-ref ref="dataApp" />
</logger>
<root level="INFO">
<appender-ref ref="consoleApp"/>
<appender-ref ref="fileInfoApp"/>
<appender-ref ref="fileErrorApp"/>
</root>
</configuration>
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@RestController
//不用拦截器
//@RequestMapping("/user/api/v1/order")
@RequestMapping("/api/v1/order")
public class OrderController {
//root级别的logger
private Logger logger = LoggerFactory.getLogger(this.getClass());
//用于统计
private Logger dateLogger = LoggerFactory.getLogger("dataLogger");
@Autowired
private VideoOrderService videoOrderService;
在service中进行统计
@Service
public class VideoOrderServiceImpl implements VideoOrderService {
//命中root级别的logger
private Logger logger = LoggerFactory.getLogger(this.getClass());
//用于统计
private Logger dateLogger = LoggerFactory.getLogger("dataLogger");
........
//隔离级别
//如果没有事务就开启一个事务
//
@Override
@Transactional(propagation = Propagation.REQUIRED)
public String save(VideoOrderDto videoOrderDto) throws Exception {
dateLogger.info("module=video_order`api=save`user_id={}",videoOrderDto.getUserId());
//查找视频信息
Video video = videoMapper.findById(videoOrderDto.getVideoId());
`2020-02-27 16:12:09.083`module=video_order`api=save`user_id=1` `2020-02-27 16:12:18.871`module=video_order`api=save`user_id=1` `2020-02-27 16:13:13.893`module=video_order`api=save`user_id=1`
dateLogger.info("module=video_order`api=save`user_id={}`vider_id={}",videoOrderDto.getUserId(),videoOrderDto.getVideoId());
`2020-02-27 16:12:09.083`module=video_order`api=save`user_id=1` `2020-02-27 16:12:18.871`module=video_order`api=save`user_id=1` `2020-02-27 16:13:13.893`module=video_order`api=save`user_id=1` `2020-02-27 16:16:32.605`module=video_order`api=save`user_id=1`vider_id=9`
dateLogger.info("module=video_order`api=save`user_id={}`vider_id={}`sum={}",videoOrderDto.getUserId(),videoOrderDto.getVideoId(),++sum);
`2020-02-27 16:12:09.083`module=video_order`api=save`user_id=1` `2020-02-27 16:12:18.871`module=video_order`api=save`user_id=1` `2020-02-27 16:13:13.893`module=video_order`api=save`user_id=1` `2020-02-27 16:16:32.605`module=video_order`api=save`user_id=1`vider_id=9` `2020-02-27 16:19:23.770`module=video_order`api=save`user_id=1`vider_id=9`sum=1` `2020-02-27 16:19:37.995`module=video_order`api=save`user_id=1`vider_id=9`sum=1` `2020-02-27 16:19:48.666`module=video_order`api=save`user_id=1`vider_id=9`sum=1` `2020-02-27 16:21:23.301`module=video_order`api=save`user_id=1`vider_id=9`sum=1` `2020-02-27 16:21:24.710`module=video_order`api=save`user_id=1`vider_id=9`sum=2` `2020-02-27 16:21:25.760`module=video_order`api=save`user_id=1`vider_id=9`sum=3` `2020-02-27 16:21:51.138`module=video_order`api=save`user_id=1`vider_id=5`sum=4`
全局的sum dateLogger.info("module=video_order`api=save`user_id={}`vider_id={},sum=",videoOrderDto.getUserId(),videoOrderDto.getVideoId(),++sum);
`2020-02-27 16:22:54.171`module=video_order`api=save`user_id=1`vider_id=5,sum=`
统一的容易进行分割便于后续的统计等处理
dateLogger.info("module=video_order`api=save`user_id={}`vider_id={},sum={}",videoOrderDto.getUserId(),videoOrderDto.getVideoId(),++sum);
`2020-02-27 16:24:11.073`module=video_order`api=save`user_id=1`vider_id=5,sum=1` `2020-02-27 16:24:12.204`module=video_order`api=save`user_id=1`vider_id=5,sum=2`
每一个类都要定义一个logger的
最后
以上就是默默夏天为你收集整理的全局异常处理和日志打点统计的全部内容,希望文章能够帮你解决全局异常处理和日志打点统计所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复