2012-09-11 6 views
0

http 연결에서 스트림 데이터를 가져오고 있습니다. log4j를 사용하여 로그 파일에 스트림을 기록하고 싶습니다. log4j를 사용하여 로그 스트림 데이터

나는 (유지되어야 함)

내가 어떻게 할 수있는 몇 가지 다른 작업을 수행 할 더이 스트림이 필요하십니까? http://www.java2s.com/Open-Source/Java/Testing/jacareto/jacareto/toolkit/log4j/LogOutputStream.java.htm

에서

StreamUtils.copy(xml, new LogOutputStreamUtil(log, Level.INFO));

곳 LogOutputStreamUtil 그러나 즉시가 기록했기 때문에 :

HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
connection.setRequestMethod("GET"); 
connection.setRequestProperty("Accept", "application/xml"); 
InputStream xml = connection.getInputStream(); 

나는이 일을 시도했다. 스트림이 닫혀지고 :(

답변

2

간단한 솔루션은

뭔가처럼 자신의 InputStream 래퍼를 작성하는 것입니다 :.

class LogingInputStream extends BufferedStream { 
    ByteArrayOutputStream copy4log = new ByteArrayOutputStream(); 
    InputStream source; 

    public LogingInputStream(InputStream source) { this.source = source; } 

    public int read() { 
     int value = source.read() 
     logCopy.write(value); 
     return value; 
    } 

    public void close() { 
     source.close(); 
     StreamUtils.copy(xml, new LogOutputStreamUtil(copy4log, Level.INFO)); 
     copy4log.close(); 
    } 

    .....more code here 
} 

어쨌든, 일반적인 생각은 당신이 방법을 읽을의 InputStream 가로 챌 필요가있다

소스 입력 스트림을 버퍼 (bytearray, string 등)에 복사하고 버퍼를 로깅하고 버퍼 주위에 다른 inputStream을 작성하여 호출자에게 반환하는 등의 다른 방법이 있습니다 입력 스트림 그것은 간단하지만, 올바른 해결책은 당신의 유스 케이스에 달려 있습니다.

관련 문제