• 统一日志处理


    1. 配置日志级别

      (1)日志记录器(Logger)的行为是分等级的。分为:OFF、FATAL、ERROR、WARN、INFO、DEBUG、ALL

      (2)默认情况下,spring boot从控制台打印出来的日志级别只有INFO及以上级别,可以配置日志级别

      (3)在application.properties中配置日志级别

          # 设置日志级别
          logging.level.root=WARN
      
      • 1
      • 2
    2. Logback日志工具

      (1)spring boot内部也是使用Logback作为日志实现的框架

      (2)日志不仅能输出到控制台,也可以输出到文件中,可以使用Logback日志工具来实现

    3. 配置logback日志

      (1)删除application.properties中的日志配置

          #mybatis日志
          mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
          #设置日志级别
          logging.level.root=INFO
      
      • 1
      • 2
      • 3
      • 4

      (2)在resources中创建logback-spring.xml,里面的写法是固定的

          
          <property name="log.path" value="E:/work/guli_log/edu" />
      
      • 1
      • 2
    4. 如果程序运行出现异常,把异常信息输出到文件中

      (1)在统一异常处理类GlobalExceptionHandler中添加注解 @Slf4j

          @ControllerAdvice
          @Slf4j
          public class GlobalExceptionHandler {
              ...
              @ExceptionHandler(GuliException.class) // 自定义异常
              @ResponseBody
              public R error(GuliException e) {
                  log.error(e.getMessage()); // 异常输出语句,e.getMessage()写到error里面去
                  e.printStackTrace();
                  return R.error().code(e.getCode()).message(e.getMsg());
              }
          }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
    5. 可以定义工具类将日志堆栈信息输出到文件,得到更详细的信息

      (1)com.guli.common下创建util包,创建ExceptionUtil.java工具类

          public class ExceptionUtil {
              public static String getMessage(Exception e) {
                  StringWriter sw = null;
                  PrintWriter pw = null;
                  try {
                      sw = new StringWriter();
                      pw = new PrintWriter(sw);
                      e.printStackTrace(pw); // 将出错的栈信息输出到printWriter中
                      pw.flush();
                      sw.flush();
                  } finally {
                      if (sw != null) {
                          try {
                              sw.close();
                          } catch (IOException e1) {
                              e1.printStackTrace();
                          }
                      }
                      if (pw != null) {
                          pw.close();
                      }
                  }
                  return sw.toString();
              }
          }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25

      (2)GuliException中创建toString方法

          @Override
          public String toString() {
              return "GuliException{" +
                  "message=" + this.getMessage() +
                  ", code=" + code +
                  '}';
          }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7

      (3)调用

          @ExceptionHandler(GuliException.class) // 自定义异常
          @ResponseBody
          public R error(GuliException e) {
              log.error(ExceptionUtil.getMessage(e)); // 异常输出语句
              e.printStackTrace();
              return R.error().code(e.getCode()).message(e.getMsg());
          }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
  • 相关阅读:
    ElastaticSearch ---es客户端 TransportClient
    电脑重装系统c盘如何备份资料
    RabbitMQ(六)仲裁队列、流式队列、异地容灾(联邦队列Federation Queue)
    grep和vim查找日志文件信息
    基于Eigen求解线性方程组Ax=b的性能分析
    SAP如何批量标记生产订单的TECO状态
    Web前端:CSS篇(二)背景,文本,链接
    基于Springboot+Vue开发建筑工地用料管理系统
    RabbitMQ学习笔记:4369、5672、15672、25672默认端口号修改
    自然语言处理实战10-文本处理过程与输入bert模型后的变化
  • 原文地址:https://blog.csdn.net/qq_41829337/article/details/126893605