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();
} @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();
spring代码异常捕获到logback logging.config=logback-spring.xml文件中不能输出异常e.printStackTrace的更多相关文章
- Spring MVC框架下在java代码中访问applicationContext.xml文件中配置的文件(可以用于读取配置文件内容)
<bean id="propertyConfigurer" class="com.****.framework.core.SpringPropertiesUtil& ...
- 事务配置在applicationContext.xml文件中不起作用,控制不了异常回滚
一.博主在学习到整合ssm框架的时候,遇到一个奇葩的问题就是将 事务的控制 ,写在在applicationContext.xml文件中不起作用,在事务控制的方法中,即使出现了异常,但是事务不会回滚的坑 ...
- 死磕Spring之IoC篇 - BeanDefinition 的加载阶段(XML 文件)
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读 Spring 版本:5.1. ...
- Android中通过代码获取arrays.xml文件中的数据
android工程res/valuse文件夹下的arrays.xml文件中用于放各种数组数据,比如字符串数组.整型数组等,数组中的数据可能是具体的值,也有可能是对资源数据的引用,下面针对这两种情况通过 ...
- web.xml 文件中一般包括 servlet, spring, filter, listenr的配置的加载顺序
首先可以肯定 加载顺序与他们在web.xml 文件中的先后顺序无关. web.xml 中 listener 和 serverlet 的加载顺序为 先 listener 后serverlet最终得出结果 ...
- 当你的Spring IOC 容器(即applicationContext.xml文件)忘记配到web.xml 文件中时
当你的Spring IOC 容器忘记配到web.xml 文件中时,启动服务器就会报错. 部分错误如下: Caused by: org.springframework.beans.factory.NoS ...
- Spring 在xml文件中配置Bean
Spring容器是一个大工厂,负责创建.管理所有的Bean. Spring容器支持2种格式的配置文件:xml文件.properties文件,最常用的是xml文件. Bean在xml文件中的配置 < ...
- spring将service添加事务管理,在applicationContext.xml文件中的设置
在applicationContext.xml文件中的设置为: <beans> <bean id="sessionFactory" class="org ...
- Spring MVC静态资源处理(在applicationContex.xml文件中进行配置)
优雅REST风格的资源URL不希望带 .html 或 .do 等后缀.由于早期的Spring MVC不能很好地处理静态资源,所以在web.xml中配置DispatcherServlet的请求映射,往往 ...
随机推荐
- composer 自动加载类 通过psr
项目地址 git@github.com:brady-wang/composer.git "autoload":{ "classmap":[ "Lib ...
- 安装openssl
此方法安装原因: 由于我用是非企业版 redhat 没有注册 有很多的yum 不能安装 openssl是在其中. 开始安装: 1.虚拟机挂载ios 镜像文件 2.进入终端 cd /media/RH ...
- [转帖]SAP一句话入门:Plant Maintenance
SAP一句话入门:Plant Maintenance http://blog.vsharing.com/MilesForce/A618273.html PM就是Plant Maintenance(本文 ...
- Docker 给 故障停掉的 container 增加 restart 参数
操作过程见图: 执行的命令比较简单: docker container update --restart=always containername 即可.
- 模态框 modal data-toggle data-target
模态框 modal data-toggle data-target 1. Data-*属性 模态框(modal) 触发事件(data-toggle) 触发对象data-target(ID 或类) ...
- Python cmd库的简易使用
简单记录一下,竟然这么简单的方法就能在 python 里面实现一个简单的交互式命令行以前从来没有尝试过. 上一个完整的例子: import cmd import osimport readline r ...
- shit vue & shit iview
shit vue & shit iview <Switch> !== <i-switch> https://www.iviewui.com/components/swi ...
- python functools.wraps functools.partial实例解析
一:python functools.wraps 实例 1. 未使用wraps的实例 #!/usr/bin/env python # coding:utf-8 def logged(func): de ...
- ADO.NET工具类(三)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- D - Mayor's posters POJ - 2528 离散化+线段树 区间修改单点查询
题意 贴海报 最后可以看到多少海报 思路 :离散化大区间 其中[1,4] [5,6]不能离散化成[1,2] [2,3]因为这样破坏了他们的非相邻关系 每次离散化区间 [x,y]时 把y+1点也加入 ...