前几天同事突然问了个问题让我不大理解,先在这里记录下。
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代码如下:
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import org.apache.log4j.Logger; public class TestLogger { private Logger logger=Logger.getLogger(TestLogger.class); public static void main(String[] args) { File file=new File("d:\\adfasf.txt"); try { InputStream input=new FileInputStream(file); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); new TestLogger().getLogger().info(e.toString()); new TestLogger().getLogger().error(e); // new TestLogger().getLogger().error("error:",e); } } public Logger getLogger() { return logger; } public void setLogger(Logger logger) { this.logger = logger; } }
log4j的简单配置可以参考:http://blog.csdn.net/shuhuiguo0915/article/details/6442566
相关推荐
用于Log4j,apache Logger打印堆栈信息 由于网上没这一部分信息,自己就把它写出来了 其实也就短短几句代码 在捕捉异常的时候使用该类即可
主要介绍了浅谈log4j 不打印异常堆栈,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
log4j打印日志的专用的jar 里面有两个文件,一个.jar文件直接放在项目的lib里面就可以,另外的文件放在项目的src根目录的下面,打开里面就可以设置需要打印的日志信息,方便快捷查看日志信息和错误提示,欢迎下载。
log4j错误打印到控制台 并且输出sql语句
mybatis,log4j打印日志到后台和文件
针对Log4j 2 远程代码执行漏洞,需要用到的升级资源包,适用于maven资源库,包括log4j,log4j-core,log4j-api,log4j-1.2-api,log4j-jpa等全套2.15.0 maven资源库jar包。如果是maven本地仓库使用,需要将zip包解压...
apache-log4j-1.2.15.jar, apache-log4j-extras-1.0.jar, apache-log4j-extras-1.1.jar, apache-log4j.jar, log4j-1.2-api-2.0.2-javadoc.jar, log4j-1.2-api-2.0.2-sources.jar, log4j-1.2-api-2.0.2.jar, log4j-...
若依框架使用的log4j2.16.0,修复log4j漏洞log4j2下载最新log4j2.16.0下载
Apache log4j2零日漏洞,根据 log4j-2.15.0-rc2 版本编译生成log4j-api-2.15.0.jar 1.解压你的jar jar xvf XXX.jar 2. 删除旧版本jar cd ./BOOT-INF/lib rm -rf log4j-api-*.jar 3. 上传新版本log4j-api-2.15.0....
java利用log4j打印日志文件的示例demo,分享给大家
1.该工程为maven构建,要有maven环境 2.支持异步打印 3.支持多线程打印
Apache log4j2零日漏洞,根据 log4j-2.15.0-rc2 版本编译生成log4j-api-2.15.0.jar 1.解压你的jar jar xvf XXX.jar 2. 删除旧版本jar cd ./BOOT-INF/lib rm -rf log4j-api-*.jar 3. 上传新版本log4j-api-...
log4j、日志输出、自定义日志、多文件输出。
Log4j 是一个日志记录框架,Log4j 2 是对 Log4j 的升级,提供了重大改进,超越其前身 Log4j 1.x,并提供许多其它现代功能 ,例如对标记的支持、使用查找的属性替换、lambda 表达式与日志记录时无垃圾等。 Apache ...
下面小编就为大家带来老生常谈Log4j和Log4j2的区别(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
描述Log4j2的配置详情及相对Log4j的优点,包括效率测试程序
log4j.rootLogger=debug,CONSOLE,testfile,A1,MAIL ################### # Console Appender ################### log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.Target=...
apache下载太慢,特搬到国内下载。修复log4j漏洞log4j2下载最新log4j2.16.0下载
log4j+slf4j实现 log4j测试代码,log4j+slf4j实现 log4j测试代码,
log4j-api-2.12.4.jar和log4j-core-2.12.4.jar,该版本避免log4j漏洞问题。