zoukankan      html  css  js  c++  java
  • Logger.error方法之打印错误异常的详细堆栈信息

    一、问题场景

    使用Logger.error方法时只能打印出异常类型,无法打印出详细的堆栈信息,使得定位问题变得困难和不方便。

    二、先放出结论

    Logger类下有多个不同的error方法,根据传入参数的个数及类型的不同,自动选择不同的重载方法。

    当error(Object obj)只传入一个参数时会将异常对象作为Object使用,并最终当做String打印出来,当使用两个参数error(String message, Throwable t),且第二个参数为Throwable时,才会将完整的异常堆栈打印出来。

    三、代码示例

    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    
    public class TestLogError {
        public static final Logger LOGGER = LogManager.getLogger(TestLogError.class);
    
        public static void main(String[] args) {
            try{
                // 模拟空指针异常
                //Integer nullInt = Integer.valueOf(null);
                int[] array = {1,2,3,4,5};
                int outBoundInt = array[5];
            }catch (Exception e){
                // 直接打印,则只输出异常类型
                LOGGER.error(e);
                // 使用字符串拼接
                LOGGER.error("使用 + 号连接直接输出 e : " + e);
                LOGGER.error("使用 + 号连接直接输出 e.getMessage() : " + e.getMessage());
                LOGGER.error("使用 + 号连接直接输出 e.toString() : " + e.toString());
                // 使用逗号分隔,调用两个参数的error方法
                LOGGER.error("使用 , 号 使第二个参数作为Throwable : ", e);
                // 尝试使用分隔符,第二个参数为Throwable,会发现分隔符没有起作用,第二个参数的不同据,调用不同的重载方法
                LOGGER.error("第二个参数为Throwable,使用分隔符打印 {} : ", e);
                // 尝试使用分隔符,第二个参数为Object,会发现分隔符起作用了,根据第二个参数的不同类型,调用不同的重载方法
                LOGGER.error("第二个参数为Object,使用分隔符打印 {} ",123);
            }
        }
    }

    信息输出:

     四、查看源码中的方法描述

    根据方法重载特性,当只输入一个参数时,此对象会被当做Object进行打印输出,如果是Exception e的话,这里直接就toString()。

    /**
     * Logs a message object with the {@link Level#ERROR ERROR} level.
     *
     * @param message the message object to log.
     */
    void error(Object message);

    根据方法重载特性,当第二个参数为Throwable时,会打印出异常信息,并且包含异常堆栈信息。

    /**
     * Logs a message at the {@link Level#ERROR ERROR} level including the stack trace of the {@link Throwable}
     * <code>t</code> passed as parameter.
     *
     * @param message the message object to log.
     * @param t the exception to log, including its stack trace.
     */
    void error(String message, Throwable t);  

    根据方法重载特性,当第二个参数为Object时,会根据占位符进行替换并打印出错误日志。

    /**
     * Logs a message with parameters at error level.
     *
     * @param message the message to log; the format depends on the message factory.
     * @param p0 parameter to the message.
     */
    void error(String message, Object p0);

    五、结论

    • 使用Logger.error(e)、Logger.error(e.getMessage())、Logger.error("some msg" + e)、Logger.error("some msg" + e.getMessage()) 都是调用的error(Object message),这个方法都会将入参当作Object输出,不会打印堆栈信息。
    • 在使用Logger.error("first param ",e)时会调用error(String message, Throwable t),此方法会完整的打印出错误堆栈信息。
  • 相关阅读:
    matplotlib 进阶之origin and extent in imshow
    Momentum and NAG
    matplotlib 进阶之Tight Layout guide
    matplotlib 进阶之Constrained Layout Guide
    matplotlib 进阶之Customizing Figure Layouts Using GridSpec and Other Functions
    matplotlb 进阶之Styling with cycler
    matplotlib 进阶之Legend guide
    Django Admin Cookbook-10如何启用对计算字段的过滤
    Django Admin Cookbook-9如何启用对计算字段的排序
    Django Admin Cookbook-8如何在Django admin中优化查询
  • 原文地址:https://www.cnblogs.com/lingyejun/p/9366533.html
Copyright © 2011-2022 走看看