Log4j 打印堆栈信息


log4j 

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

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

     

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

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


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

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

   

Java代码   收藏代码
  1. /** 
  2.    Log a message object with the {@link Level#ERROR ERROR} Level. 
  3.  
  4.    This method first checks if this category is ERROR 
  5.    enabled by comparing the level of this category with {@link 
  6.    Level#ERROR ERROR} Level. If this category is ERROR 
  7.    enabled, then it converts the message object passed as parameter 
  8.    to a string by invoking the appropriate {@link 
  9.    org.apache.log4j.or.ObjectRenderer}. It proceeds to call all the 
  10.    registered appenders in this category and also higher in the 
  11.    hierarchy depending on the value of the additivity flag. 
  12.  
  13.    WARNING Note that passing a {@link Throwable} to this 
  14.    method will print the name of the Throwable but no 
  15.    stack trace. To print a stack trace use the {@link #error(Object, 
  16.    Throwable)} form instead. 
  17.  
  18.    @param message the message object to log */  
  19.  public  
  20.  void error(Object message) {  
  21.    if(repository.isDisabled(Level.ERROR_INT))  
  22.      return;  
  23.    if(Level.ERROR.isGreaterOrEqual(this.getEffectiveLevel()))  
  24.      forcedLog(FQCN, Level.ERROR, message, null);  
  25.  }  
  26.   
  27.  /** 
  28.   Log a message object with the ERROR level including 
  29.   the stack trace of the {@link Throwable} t passed as 
  30.   parameter. 
  31.  
  32.   See {@link #error(Object)} form for more detailed information. 
  33.  
  34.   @param message the message object to log. 
  35.   @param t the exception to log, including its stack trace.  */  
  36.  public  
  37.  void error(Object message, Throwable t) {  
  38.    if(repository.isDisabled(Level.ERROR_INT))  
  39.      return;  
  40.    if(Level.ERROR.isGreaterOrEqual(this.getEffectiveLevel()))  
  41.      forcedLog(FQCN, Level.ERROR, message, t);  
  42.   
  43.  }  




   具体的demo代码如下:

  

Java代码   收藏代码
  1. import java.io.File;  
  2. import java.io.FileInputStream;  
  3. import java.io.FileNotFoundException;  
  4. import java.io.InputStream;  
  5.   
  6. import org.apache.log4j.Logger;  
  7.   
  8.   
  9. public class TestLogger {  
  10.   
  11.     private Logger logger=Logger.getLogger(TestLogger.class);  
  12.       
  13.       
  14.     public static void main(String[] args) {  
  15.           
  16.         File file=new File("d:\\adfasf.txt");  
  17.         try {  
  18.             InputStream input=new FileInputStream(file);  
  19.         } catch (FileNotFoundException e) {  
  20.             // TODO Auto-generated catch block  
  21.             e.printStackTrace();  
  22.             new TestLogger().getLogger().info(e.toString());  
  23.             new TestLogger().getLogger().error(e);  
  24. //          new TestLogger().getLogger().error("error:",e);  
  25.         }  
  26.           
  27.           
  28.     }  
  29.   
  30.   
  31.     public Logger getLogger() {  
  32.         return logger;  
  33.     }  
  34.   
  35.   
  36.     public void setLogger(Logger logger) {  
  37.         this.logger = logger;  
  38.     }  
  39.       
  40.       
  41.       
  42. }  
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值