java – 转换e.printStackTrace()以改为使用log4j

我对log4j很新.我不想在我的日志文件显示异常堆栈跟踪,例如
java.lang.IllegalArgumentException: nodeRef is a mandatory parameter
at org.alfresco.util.ParameterCheck.mandatory(ParameterCheck.java:42)

这些异常通过使用e.printstacktrace()直接写入控制台

try {
    // something
} catch(Exception e) {
    StringWriter stack = new StringWriter();
    e.printstacktrace(new PrintWriter(stack));
    logger.debug("Caught exception; decorating with appropriate status template : " + stack.toString());
}

我现在正在定制一个开源项目,而不是自己编写我的所有程序.
所以不可能删除e.printstacktrace();在所有的java文件中.

据我所知,log4j用logger打印的日志可以通过使用log4j错误级别来配置,例如info,debug,warn.But如何直接写入控制台或文件

如何配置log4j不打印只有异常堆栈跟踪但显示其他信息?有办法解决吗?

解决方法

代码
StringWriter stack = new StringWriter();
e.printstacktrace(new PrintWriter(stack));
logger.debug("Caught exception; decorating with appropriate status template : " + stack.toString());

只是Log4j API使用错误一个例子.但是,如果您坚持使用它,除了将日志级别提高到调试之外并且省略整个消息之外,您将无需执行任何操作.

可能有一种方法可以截断所有日志消息,这可以在一定程度上缓解您的问题.

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...