(web日志)把后台的日志System.out输出到网页servlet

纯代码,待优化
把System.out的输出放到servlet中
我以为logback本身有这个功能,但是没有找到,就自己写了试验类的...
 *   获取logback日志流...
 *   方案1: 直接读取文件 分段读取  http://sunnylocus.iteye.com/blog/694666
 *   方案2: logback 配置apender到某个流...复杂
 *   方案3: 复制System.out流 
 *   方案4: 读取System.out流再保存(缓存)
后期可以添加 ajax实现 "在线日志" 这是后话了.....
 
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 

public class WebLogerServlet   extends HttpServlet{
static OutputStream life =null;
 
Logger logger = LoggerFactory.getLogger(PosConsumeServlet.class);
public void init()
{
System.setOut(new PrintStream(new BuffStream()));
System.setErr(new PrintStream(new BuffStream()));
System.out.println("100KB....");
}

private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException{
response.setContentType("application/json");
response.getWriter().write(BuffStream.getBuff());
}
}
class  BuffStream extends OutputStream{
static char[]  buff=new char[1024*100];  // 100KB内容
static   OutputStream cout=System.out; //把原来的out保存下来.
static    OutputStream cerr =System.err;
static int   index=0;
 
public static String getBuff()
{
//new ByteS
char[] bb=new char[index];
for(int i=0;i
{
bb[i]=buff[i];
}
return new String(bb);
}

@Override
public void write(int b) throws IOException {
cout.write(b);
if(index
{
buff[index]=(char) b;
index++;
}else{
index=0;
}
}
}

class DoubleStream extends OutputStream {
OutputStream output1;
OutputStream output2;

public DoubleStream(OutputStream output1, OutputStream output2) {
this.output1 = output1;
this.output2 = output2;
}

public void write(int b) throws IOException {
if(output1!=null)
output1.write(b);
if(output2!=null)
{
output2.write(b);
}
}
}

//Logger logger=LoggerFactory.getLogger(WebLogerServlet.class);
//LoggerFactory.getILoggerFactory();
//   LoggerContext lc = (LoggerContext)LoggerFactory.getILoggerFactory();
//   lc.
//    lc.getStatusManager();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值