Log4j 打印堆栈信息

编程入门 行业动态 更新时间:2024-10-27 10:31:30

Log4j 打印<a href=https://www.elefans.com/category/jswz/34/1771218.html style=堆栈信息"/>

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. }  

更多推荐

Log4j 打印堆栈信息

本文发布于:2024-03-15 03:11:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1737986.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:堆栈   信息   Log4j

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!