Created
December 10, 2021 02:58
-
-
Save charneykaye/780b97305e0094b6126666859da08589 to your computer and use it in GitHub Desktop.
Java class to consume a stream and log it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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