Over a million developers have joined DZone.

Log Scraping

· DevOps Zone

Discover how to optimize your DevOps workflows with our cloud-based automated testing infrastructure, brought to you in partnership with Sauce Labs

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();
}
}

Download “The DevOps Journey - From Waterfall to Continuous Delivery” to learn learn about the importance of integrating automated testing into the DevOps workflow, brought to you in partnership with Sauce Labs.

Topics:

Published at DZone with permission of Tim Spann, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}