DevOps Zone is brought to you in partnership with:

I have been a programmer since I was a young child in the Gifted and Talent Program in Rutherford, NJ back in 1979. I started programming on Commodore PETs – 4 bit. And then Apple IIs, Commodore 64s and some others. Basic, then Pascal, Fortran, C, C++, Java, JavaScript and more. I am a Senior Field Engineer with Pivotal. Tim is a DZone MVB and is not an employee of DZone and has posted 58 posts at DZone. You can read more from them at their website. View Full User Profile

Log Scraping

06.05.2013
| 3163 views |
  • submit to reddit

A quick Java snippet for log scraping:

package com.agilemobiledeveloper.logcheck;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

/**
*
* @author spannt
*
*/
public class LogScraper {

/**
* @param args
*/
public static void main(String[] args) {
String SFTPHOST = "myunixsite.com";
int SFTPPORT = 22;
String SFTPUSER = "myunixid";
String SFTPPASS = "myunixpassword";
String SFTPWORKINGDIR = "/some/unix/directory";
String SERRORFILE = "SystemErr.log";
String SOUTFILE = "SystemOut.log";

Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;

StringBuilder out = new StringBuilder();

try {
JSch jsch = new JSch();
session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
session.setPassword(SFTPPASS);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp) channel;
channelSftp.cd(SFTPWORKINGDIR);

System.out.println("Error File");
out.append("Error File:").append(
LogScraper.parseStream(channelSftp.get(SERRORFILE)));

System.out.println("Output File");
out.append("Output File:").append(
LogScraper.parseStream(channelSftp.get(SOUTFILE)));

} catch (Exception ex) {
ex.printStackTrace();
out.append(ex.getLocalizedMessage());
}

System.out.println("Logs=" + out.toString());

}

/**
*
* line.contains("Exception") ||
*
* @param file
* @return String of error data
*/
public static String parseStream(InputStream inputFileStream) {
if ( null == inputFileStream ) { return "Log Empty"; }
StringBuilder out = new StringBuilder();

BufferedReader br = new BufferedReader(new InputStreamReader(
inputFileStream));

String line = null;
try {
while ((line = br.readLine()) != null) {
if (line.contains("OutOfMemoryError")) {
out.append(line).append(System.lineSeparator());
}
}
} catch (IOException e) {
e.printStackTrace();
out.append(e.getLocalizedMessage());
}

return out.toString();
}
}
Published at DZone with permission of Tim Spann, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Andy Jefferson replied on Wed, 2013/06/05 - 1:26pm

Perhaps you mean "Log Scraping". I've often felt it would be nice to scrap a log ;-)

Tim Spann replied on Wed, 2013/06/05 - 1:43pm in response to: Andy Jefferson

Good catch.  I like your second idea better though.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.