纯代码,待优化
把System.out的输出放到servlet中
我以为logback本身有这个功能,但是没有找到,就自己写了试验类的...
* 方案1: 直接读取文件 分段读取 http://sunnylocus.iteye.com/blog/694666
* 方案2: logback 配置apender到某个流...复杂
* 方案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();