最近在项目中发现在打印错误异常时,大家的写法五花八门,貌似不清楚各写法的区别,这会导致打印的异常日志丢失或不全,进而无法迅速定位异常原因,因此写了demo以示各写法区别。

先看代码

import org.apache.log4j.Logger;

public class Test {
	private static Logger logger = Logger.getLogger(Test.class);
	public static void main(String[] args) {

		try{
			int a = 10 / 0;
		}catch(Exception e){
			logger.info("==============[e]=====================");
			logger.error(e);
			logger.info("============[\"异常\" + e]=======================");
			logger.error("异常" + e);
			logger.info("============[e.toString()]=======================");
			logger.error(e.toString());
			logger.info("============[e.getMessage()]=======================");
			logger.error(e.getMessage());
			logger.info("=============[\"异常\", e]======================");
			logger.error("异常", e);

		}
		
	}

}

再看结果

2019-12-12 09:57:13.539 INFO - [ main:0 ] - [Test.main: 10] - ==============[e]=====================
2019-12-12 09:57:13.542 ERROR - [ main:3 ] - [Test.main: 11] - java.lang.ArithmeticException: / by zero
2019-12-12 09:57:13.542 INFO - [ main:3 ] - [Test.main: 12] - ============["异常" + e]=======================
2019-12-12 09:57:13.542 ERROR - [ main:3 ] - [Test.main: 13] - 异常java.lang.ArithmeticException: / by zero
2019-12-12 09:57:13.542 INFO - [ main:3 ] - [Test.main: 14] - ============[e.toString()]=======================
2019-12-12 09:57:13.542 ERROR - [ main:3 ] - [Test.main: 15] - java.lang.ArithmeticException: / by zero
2019-12-12 09:57:13.542 INFO - [ main:3 ] - [Test.main: 16] - ============[e.getMessage()]=======================
2019-12-12 09:57:13.543 ERROR - [ main:4 ] - [Test.main: 17] - / by zero
2019-12-12 09:57:13.544 INFO - [ main:5 ] - [Test.main: 18] - =============["异常", e]======================
2019-12-12 09:57:13.545 ERROR - [ main:6 ] - [Test.main: 19] - 异常
java.lang.ArithmeticException: / by zero
	at Test.main(Test.java:8)

 

经观察结果发现,只有logger.error("异常", e)这种写法可以详细的打印栈跟踪信息,这种方式能够定位到具体异常发生位置,能够让我们迅速定位到错误原因。

强烈不推荐logger.error(e.getMessage())这种方式,因为e.getMessage()可能为空,导致看不到任何错误信息。

下面是不同error方法参数的用法具体说明

error(Object message)


  Open Declaration   void org.apache.log4j.Category.error(Object message)


Log a message object with the ERROR Level. 

This method first checks if this category is ERROR enabled by comparing the level of this category with ERROR Level. If this category is ERROR enabled, then it converts the message object passed as parameter to a string by invoking the appropriate org.apache.log4j.or.ObjectRenderer. It proceeds to call all the registered appenders in this category and also higher in the hierarchy depending on the value of the additivity flag. 

WARNING Note that passing a Throwable to this method will print the name of the Throwable but no stack trace. To print a stack trace use the error(Object, Throwable) form instead.
Parameters:message the message object to log 

上面的警告已说明打印栈跟踪信息要用error(Object, Throwable)

error(Object, Throwable)


  Open Declaration   void org.apache.log4j.Category.error(Object message, Throwable t)


Log a message object with the ERROR level including the stack trace of the Throwable t passed as parameter. 

See error(Object) form for more detailed information.
Parameters:message the message object to log.t the exception to log, including its stack trace. 

上面详细说明了该方法打印日志,其中日志中包含栈跟踪信息

 

详细了解用法后,强烈推荐使用error(Object message, Throwable t)

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐