java Logger.error prints the detailed stack information of the error exception

1. Problem scenario

When using the Logger.error method, only the exception type can be printed out, but the detailed stack information cannot be printed out, making it difficult and inconvenient to locate the problem.

Second, first release the conclusion

There are multiple different error methods under the Logger class, and different overload methods are automatically selected according to the number and type of incoming parameters.

When error(Object obj) only passes in one parameter, the exception object will be used as Object and finally printed out as String. When two parameters error(String message, Throwable t) are used, and the second parameter is Throwable, Only then will the complete exception stack be printed out.

code example

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
 * @PROJECT_NAME: 
 * @DESCRIPTION:
 */
public class test {
    
    
    public static final Logger LOGGER = LogManager.getLogger(test.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);
        }
    }
}

output:
insert image description here

Fourth, view the method description in the source code

According to the feature of method overloading, when only one parameter is input, the object will be printed out as Object, if it is Exception e, here directly toString().

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

According to the method overloading feature, when the second parameter is Throwable, the exception information will be printed out, and the exception stack information will be included.

/**
 * 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); 

According to the method overloading feature, when the second parameter is Object, it will be replaced according to the placeholder and the error log will be printed out.

/**
 * 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);

V. Conclusion

Use Logger.error(e), Logger.error(e.getMessage()), Logger.error("some msg" + e), Logger.error("some msg" + e.getMessage()) are all called error(Object message), this method will output the input parameter as an Object, and will not print the stack information.
Error(String message, Throwable t) will be called when Logger.error("first param ",e) is used, and this method will print out the error stack information completely.

Guess you like

Origin blog.csdn.net/doublepg13/article/details/128328160