spring代码异常捕获到logback logging.config=logback-spring.xml文件中不能输出异常e.printStackTrace
2018-01-16 11:05
726 查看
在spring中使用logging.config=logback-spring.xml将日志转存到了文件中。但是代码中的捕获的异常无法用 e.printStackTrace 打印到文件中。使用如下方法打印:
初始化时调用:
// initialize logging to go to rolling log file
LogManager.resetConfiguration();
// and output on the original stdout
System.out.println("Hello on old stdout");
Log4jStdOutErrProxy.bind();
main: catch(Exception e){ log.error("xxx",e); } 这里可以重新定向 system.out 和err的输出,到logback: https://stackoverflow.com/questions/1200175/log4j-redirect-stdout-to-dailyrollingfileappender 用于捕获运行时异常。
package com.italktv.platform.audioDist; import java.io.IOException; import java.io.OutputStream; import java.io.PrintStream; import org.apache.log4j.Level; import org.apache.log4j.Logger; public class Log4jStdOutErrProxy { public static void bind() { bind(Logger.getLogger("STDOUT"), Logger.getLogger("STDERR")); } @SuppressWarnings("resource") public static void bind(Logger loggerOut, Logger loggerErr) { // System.setOut(new PrintStream(new LoggerStream(loggerOut, Level.INFO, System.out), true)); System.setErr(new PrintStream(new LoggerStream(loggerErr, Level.ERROR, System.err), true)); } private static class LoggerStream extends OutputStream { private final Logger logger; private final Level logLevel; private final OutputStream outputStream; private StringBuilder sbBuffer; public LoggerStream(Logger logger, Level logLevel, OutputStream outputStream) { this.logger = logger; this.logLevel = logLevel; this.outputStream = outputStream; sbBuffer = new StringBuilder(); } @Override public void write(byte[] b) throws IOException { doWrite(new String(b)); } @Override public void write(byte[] b, int off, int len) throws IOException { doWrite(new String(b, off, len)); } @Override public void write(int b) throws IOException { doWrite(String.valueOf((char) b)); } private void doWrite(String str) throws IOException { sbBuffer.append(str); if (sbBuffer.charAt(sbBuffer.length() - 1) == '\n') { // The output is ready sbBuffer.setLength(sbBuffer.length() - 1); // remove '\n' if (sbBuffer.charAt(sbBuffer.length() - 1) == '\r') { sbBuffer.setLength(sbBuffer.length() - 1); // remove '\r' } String buf = sbBuffer.toString(); sbBuffer.setLength(0); outputStream.write(buf.getBytes()); outputStream.write('\n'); logger.log(logLevel, buf); } } } // inner class LoggerStream }
初始化时调用:
// initialize logging to go to rolling log file
LogManager.resetConfiguration();
// and output on the original stdout
System.out.println("Hello on old stdout");
Log4jStdOutErrProxy.bind();
相关文章推荐
- java代码将e.printStackTrace()写入log4j文件异常信息
- Spring-Boot--日志操作全局异常捕获消息处理☞日志控制台输出+日志文件记录
- JAVA Catch中异常信息的输出 System.out.println(e) e.printStackTrace()
- Spring-Boot--日志操作【全局异常捕获消息处理☞日志控制台输出+日志文件记录】
- Spring-Boot--日志操作【全局异常捕获消息处理☞日志控制台输出+日志文件记录】
- springMVC配置文件web.xml与spring-servlet.xml与spring-jdbc.xml与logback.xml与redis.properties与pom.xml
- Android中用LogCat输出e.printStackTrace
- Java异常的栈轨迹fillInStackTrace和printStackTrace的用法
- 【技术贴】MyEclipse打出syso代码不能自动补全补全不能输出system.out.print
- Android studio 怎么看输出的e.printStackTrace()
- XML输出中文时,无法用xsl查看(XML文件不能正常显示、中文显示乱码)
- Java如何将Exception.printStackTrace()转换为String输出
- maven+mybatis+spring xml文件没有编译到输出路径
- struts1和spring进行整合的时候struts-config.xml文件的配置顺序问题
- java中exception的printStackTrace()输出内容转成字符串
- struts异常不捕获也可以在控制台和日志文件输出
- XML输出中文时,无法用xsl查看(XML文件不能正常显示、中文显示乱码)
- Spring加载xml文件错误异常:Offending resource: class path resource [spring-context.xml];
- logback.xml配置如何按天输出日志文件
- Spring、Mybatis和Logback集成,用面向切面AOP方式捕获并打印异常Exception信息