日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring代码异常捕获到logback logging.config=logback-spring.xml文件中不能输出异常e.printStackTrace...

發布時間:2024/9/5 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring代码异常捕获到logback logging.config=logback-spring.xml文件中不能输出异常e.printStackTrace... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在spring中使用logging.config=logback-spring.xml將日志轉存到了文件中。但是代碼中的捕獲的異常無法用 e.printStackTrace 打印到文件中。使用如下方法打印:

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();}@Overridepublic void write(byte[] b) throws IOException {doWrite(new String(b));}@Overridepublic void write(byte[] b, int off, int len) throws IOException {doWrite(new String(b, off, len));}@Overridepublic 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 readysbBuffer.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();

轉載于:https://www.cnblogs.com/bigben0123/p/8295508.html

總結

以上是生活随笔為你收集整理的spring代码异常捕获到logback logging.config=logback-spring.xml文件中不能输出异常e.printStackTrace...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。