【java】打印错误日志推荐使用logger.error("异常", e)

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

  • 13
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值