Log4j 打印堆栈信息

本文探讨了Log4j与`e.printStackTrace()`的区别,解释了logger.error(exception)与logger.error("", exception)在打印堆栈信息上的差异。在错误日志记录中,直接使用logger.error(exception)只会输出异常的toString(),而logger.error("", exception)会打印完整的堆栈信息。作者邀请了解详情的读者分享答案。" 106026378,8204070,Android开发:打造自定义圆形ListView,"['Android开发', 'UI设计', '自定义组件', '适配器']
摘要由CSDN通过智能技术生成

 前几天同事突然问了个问题让我不大理解,先在这里记录下。

     1.log4j.error和e.printstacktrace()有什么区别?

     

      我的理解当然很简单,e.printstacktrace()是在控制台输出来的,logger4j是在日志中输出来的。

  后来同事打了个哑谜还有一个是关系到buffer上的区别,对于这点其实我还是没有怎么搞明白,有知道的小伙伴可以来解答下。


    2.logger.error(exception)和logger.error("",exception) 看很多人都是后者的写法,为什么就不能直接用logger.error(exception)呢?

    对于这个问题我们可以对比下输出结果就知道了,发现前者只打印一行报错信息,后者却可以打印出堆栈信息。其实这个问题可以在源码中探索出来。原来前者只把excetion.toString()当成message,异常信息设置成null了。

 

 /**
    Log a message object with the {@link Level#ERROR ERROR} Level.

    This method first checks if this category is ERROR
    enabled by comparing the level of this category with {@link
    Level#ERROR ERROR} Level. If this category is ERROR
    enabled, then it converts the message object passed as parameter
    to a string by invoking the appropriate {@link
    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 {@link Throwable} to this
    method will print the name of the Throwable but no
    stack trace. To print a stack trace use the {@link #error(Object,
    Throwable)} form instead.

    @param message the message object to log */
  public
  void error(Object message) {
    if(repository.isDisabled(Level.ERROR_INT))
      return;
    if(Level.ERROR.isGreaterOrEqual(this.getEffectiveLevel()))
      forcedLog(FQCN, Level.ERROR, message, null);
  }

  /**
   Log a message object with the ERROR level including
   the stack trace of the {@link Throwable} t passed as
   parameter.

   See {@link #error(Object)} form for more detailed information.

   @param message the message object to log.
   @param t the exception to log, including its stack trace.  */
  public
  void error(Object message, Throwable t) {
    if(repository.isDisabled(Level.ERROR_INT))
      return;
    if(Level.ERROR.isGreaterOrEqual(this.getEffectiveLevel()))
      forcedLog(FQCN, Level.ERROR, message, t);

  }

 

 

具体的demo代码如下:

 

 

 

 

package tools;

import org.apache.log4j.Logger;

import entity.User;

public class Log4jTest {
	private static Logger logger=Logger.getLogger(Log4jTest.class);
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try
		{
			User user = null;
			Integer.parseInt(user.getContent_append());
		}catch(Exception ex)
		{			
			//logger.debug("debug");
			logger.error("",ex);			
		}		
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值