Skip to content

Instantly share code, notes, and snippets.

@charneykaye
Created December 10, 2021 02:58
Show Gist options
  • Save charneykaye/780b97305e0094b6126666859da08589 to your computer and use it in GitHub Desktop.
Save charneykaye/780b97305e0094b6126666859da08589 to your computer and use it in GitHub Desktop.
Java class to consume a stream and log it
// by Charney Kaye <charneykaye.com>
package com.charneykaye;
import org.slf4j.Logger;
import java.io.*;
public class StreamLogger implements Runnable {
private final Logger LOG;
private final InputStream _inputStream;
public StreamLogger(Logger LOG, InputStream is) {
this.LOG = LOG;
_inputStream = is;
}
public static void close(Reader reader) {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
InputStreamReader isr = null;
BufferedReader br = null;
try {
isr = new InputStreamReader(_inputStream);
br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
LOG.info("----> {}", line);
}
} catch (IOException e) {
LOG.error("Failed to gobble stream!", e);
} finally {
close(isr);
close(br);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment