ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Springboot组件:日志:log.error()源码解析

2022-06-08 00:35:09  阅读:295  来源: 互联网

标签:Springboot format arg1 arg2 Object 源码 error String


目录

Springboot组件:日志的使用

1 log.error("请求地址'{}',发生未知异常.", requestURI, e)的解析

在全局异常处理类中,有下面的一段代码,其中重点看 log.error() 方法:
(log的类型是 slf4j-api-1.7.36.jar 中的 org.slf4j.Logger类)

@ExceptionHandler(RuntimeException.class)
    public AjaxResult handleRuntimeException(RuntimeException e, HttpServletRequest request)
    {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',发生未知异常.", requestURI, e);
        return AjaxResult.error(e.getMessage());
    }

我想要看看源码中对应的方法,找到3个方法比较相似:

    /**
     * Log an exception (throwable) at the ERROR level with an
     * accompanying message.
     *
     * @param msg the message accompanying the exception
     * @param t   the exception (throwable) to log
     */
    public void error(String msg, Throwable t);

   /**
     * Log a message at the ERROR level according to the specified format
     * and arguments.
     * <p/>
     * <p>This form avoids superfluous object creation when the logger
     * is disabled for the ERROR level. </p>
     *
     * @param format the format string
     * @param arg1   the first argument
     * @param arg2   the second argument
     */
    public void error(String format, Object arg1, Object arg2);
	
   /**
     * Log a message at the ERROR level according to the specified format
     * and arguments.
     * <p/>
     * <p>This form avoids superfluous string concatenation when the logger
     * is disabled for the ERROR level. However, this variant incurs the hidden
     * (and relatively small) cost of creating an <code>Object[]</code> before invoking the method,
     * even if this logger is disabled for ERROR. The variants taking
     * {@link #error(String, Object) one} and {@link #error(String, Object, Object) two}
     * arguments exist solely in order to avoid this hidden cost.</p>
     *
     * @param format    the format string
     * @param arguments a list of 3 or more arguments
     */
    public void error(String format, Object... arguments);

但此处的 log.error("请求地址'{}',发生未知异常.", requestURI, e); ,看上去是归属于 public void error(String format, Object arg1, Throwable t); 这种方法签名类型,而该方法签名在源码中找不到,于是debug源码查看究竟,因为使用了 logback,所以实际使用了 org.slf4j.Logger 的实现类 ch.qos.logback.classic.Logger类,然后进一步debug源码,发现是属于该方法签名:public void error(String format, Object arg1, Object arg2);。然后往深了debug,最终发现了它的实现方式:

  1. 查看末尾的arg2是否属于 Throwable 类型,如果是,则将他从参数数组 argArray (由arg1 和 arg2 组成)中去掉;
  2. arg2单独拎出来作为一个 Throwable 对象;
  3. 使用 format和arg1 拼接出完整的消息字符串;
  4. 打印format和arg1拼接成的字符串;
  5. 打印arg2(是打印一个异常链条)

拓展:
如果除format之外的arg参数大于2个,则是使用这个重载方法 public void error(String format, Object... arguments); ,则可能有超过3个的参数,则参数数组 argArray 由所有的 arguments 组成。

过程是类似的,但是要注意2点:

  1. 只判断最后一个arg是否属于 Throwable类型,如果是,则将他从参数数组 argArray (由arg1 和 arg2 组成)中去掉,其他的 arg 参数,即使是 Throwable类型,也当成普通的字符串来使用(自动调用 ThrowabletoString() 方法);
  2. 拼接format和args时,format参数字符串中有多少个 {},就从 argArray 数组中使用多少个arg拼接进去,如果argArray中还有多的未使用的arg,丢弃;如果 argArray 中的 arg不够,则会有一些 {},不被替换,导致拼接后的字符串中仍然保留这些 {}

2

标签:Springboot,format,arg1,arg2,Object,源码,error,String
来源: https://www.cnblogs.com/mediocreWorld/p/16353959.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有