I'm trying to write log tool, that will connect to few servers via ssh, open specified log file and print the result to System.out.print. for now, I've achieved getting logs from one source. Starting at SSHManager class that just use Jsch to achieve that.
public void tailLogFile() {
System.out.println("Starting to monitor logs for " + server.getIp());
String command = "tail -f " + server.getLogFilePath();
try {
Channel channel = getSession().openChannel("exec");
((ChannelExec)channel).setCommand(command);
InputStream commandOutput = channel.getInputStream();
channel.connect();
int readByte = commandOutput.read();
while(readByte != 0xffffffff) {
readByte = commandOutput.read();
System.out.print(server.getFontColor().toString() + (char)readByte);
}
channel.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
I'm guessing the rest is irrevelant here, it prints coloured logs from SSH to my System.out. But, main purpouse of this program is to log multiple files into one place. So I'v tried following
for(SSHManager sshManager : getSshManagers()) {
sshManager.tailLogFile();
}
And it is not working now, it starts to print logs from first iteration of for-loop and since while inside SSHManager.tailLogFile() doesn't terminate, it keeps printing logs from the first source. As you can imagine, I'd like those n instances of SSHManager to share System.out and give me output from all sources at same time. I'm wondering what's the easiest way to achieve that? I need to dive into concurrency?